revert entity to abstract class, implement repository __construct and parcial insert
This commit is contained in:
parent
05eefafb87
commit
6e619d7431
|
@ -7,19 +7,16 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
|
|||
/**
|
||||
* Class that represetns the Collection entity
|
||||
*/
|
||||
class Collection {
|
||||
use \Tainacan\Traits\Entity;
|
||||
class Collection extends Entity {
|
||||
|
||||
const POST_TYPE = 'tainacan-collections';
|
||||
protected static $post_type = 'tainacan-collections';
|
||||
protected $repository = 'Tainacan_Collections';
|
||||
|
||||
/**
|
||||
* Create an instance of Collection
|
||||
* @param integer|\WP_Post optional $which Collection ID or a WP_Post object for existing collections. Leave empty to create a new collection.
|
||||
*/
|
||||
function __construct($which = 0) {
|
||||
|
||||
$this->repository = 'Tainacan_Collections';
|
||||
|
||||
if (is_numeric($which) && $which > 0) {
|
||||
$post = get_post($which);
|
||||
|
||||
|
@ -42,7 +39,7 @@ class Collection {
|
|||
* This method register the post type for a collection, so that items can be created.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
*
|
||||
function tainacan_register_post_type() {
|
||||
$cpt_labels = array(
|
||||
'name' => 'Item',
|
||||
|
@ -86,7 +83,7 @@ class Collection {
|
|||
unregister_post_type($this->get_db_identifier());
|
||||
|
||||
register_post_type($cpt_slug, $args);
|
||||
}
|
||||
}*/
|
||||
|
||||
/**
|
||||
* Get the collection ID
|
||||
|
|
|
@ -1,13 +1,18 @@
|
|||
<?php
|
||||
|
||||
namespace Tainacan\Traits;
|
||||
namespace Tainacan\Entities;
|
||||
|
||||
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
|
||||
|
||||
trait Entity {
|
||||
|
||||
private $repository;
|
||||
class Entity {
|
||||
protected $repository;
|
||||
private $errors = [];
|
||||
protected static $post_type = 'post';
|
||||
/**
|
||||
*
|
||||
* @var \WP_Post
|
||||
*/
|
||||
public $WP_Post;
|
||||
|
||||
public function get_mapped_property($prop) {
|
||||
|
||||
|
@ -104,6 +109,10 @@ trait Entity {
|
|||
return $this->errors;
|
||||
}
|
||||
|
||||
public static function get_post_type() {
|
||||
return static::$post_type;
|
||||
}
|
||||
|
||||
public function add_error($type, $message) {
|
||||
$this->errors[] = [$type => $message];
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Tainacan\Entities;
|
||||
use Tainacan\Entities;
|
||||
|
||||
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
|
||||
|
||||
|
@ -9,8 +8,7 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
|
|||
* Representa a entidade Filter
|
||||
*
|
||||
*/
|
||||
class Filter {
|
||||
use \Tainacan\Traits\Entity;
|
||||
class Filter extends Entity {
|
||||
use \Tainacan\Traits\Entity_Collection_Relation;
|
||||
|
||||
const POST_TYPE = 'tainacan-filters';
|
||||
|
@ -72,11 +70,11 @@ class Filter {
|
|||
/**
|
||||
* Retorna o metadado
|
||||
*
|
||||
* @return Entities\Metadata
|
||||
* @return Metadata
|
||||
*/
|
||||
function get_metadata() {
|
||||
$id = $this->get_mapped_property('metadata');
|
||||
return new Entities\Metadata( $id );
|
||||
return new Metadata( $id );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -144,7 +142,7 @@ class Filter {
|
|||
* @return void
|
||||
*/
|
||||
function set_metadata( $value ){
|
||||
$id = ( $value instanceof Entities\Metadata ) ? $value->get_id() : $value;
|
||||
$id = ( $value instanceof Metadata ) ? $value->get_id() : $value;
|
||||
|
||||
$this->set_mapped_property('metadata', $id);
|
||||
}
|
||||
|
|
|
@ -7,8 +7,7 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
|
|||
/**
|
||||
* Representa a entidade Item Metadata
|
||||
*/
|
||||
class Item_Metadata_Entity {
|
||||
use \Tainacan\Traits\Entity;
|
||||
class Item_Metadata_Entity extends Entity {
|
||||
function __construct(Item $item, Metadata $metadata) {
|
||||
|
||||
$this->repository = 'Tainacan_Item_Metadata';
|
||||
|
|
|
@ -7,8 +7,7 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
|
|||
/**
|
||||
* Representa a entidade Item
|
||||
*/
|
||||
class Item {
|
||||
use \Tainacan\Traits\Entity;
|
||||
class Item extends Entity {
|
||||
use \Tainacan\Traits\Entity_Collection_Relation;
|
||||
|
||||
function __construct($which = 0) {
|
||||
|
|
|
@ -7,8 +7,7 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
|
|||
/**
|
||||
* Representa a entidade Log
|
||||
*/
|
||||
class Log {
|
||||
use \Tainacan\Traits\Entity;
|
||||
class Log extends Entity {
|
||||
const POST_TYPE = 'tainacan-logs';
|
||||
|
||||
function __construct($which = 0) {
|
||||
|
|
|
@ -7,8 +7,7 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
|
|||
/**
|
||||
* Representa a entidade Metadata e extende a super classe Entity
|
||||
*/
|
||||
class Metadata {
|
||||
use \Tainacan\Traits\Entity;
|
||||
class Metadata extends Entity {
|
||||
const POST_TYPE = 'tainacan-metadata';
|
||||
|
||||
use \Tainacan\Traits\Entity_Collection_Relation;
|
||||
|
|
|
@ -4,8 +4,7 @@ namespace Tainacan\Entities;
|
|||
|
||||
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
|
||||
|
||||
class Taxonomy {
|
||||
use \Tainacan\Traits\Entity;
|
||||
class Taxonomy extends Entity {
|
||||
use \Tainacan\Traits\Entity_Collections_Relation;
|
||||
|
||||
const POST_TYPE = 'tainacan-taxonomies';
|
||||
|
|
|
@ -4,8 +4,7 @@ namespace Tainacan\Entities;
|
|||
|
||||
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
|
||||
|
||||
class Term {
|
||||
use \Tainacan\Traits\Entity;
|
||||
class Term extends Entity {
|
||||
function __construct($which = 0, $taxonomy = '' ) {
|
||||
|
||||
$this->repository = 'Tainacan_Terms';
|
||||
|
|
|
@ -7,12 +7,8 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
|
|||
|
||||
use \Respect\Validation\Validator as v;
|
||||
|
||||
class Collections implements Repository {
|
||||
|
||||
function __construct() {
|
||||
add_action('init', array(&$this, 'tainacan_register_post_type'));
|
||||
}
|
||||
|
||||
class Collections extends Repository {
|
||||
protected $entities_type = '\Tainacan\Entities\Collection';
|
||||
public function get_map() {
|
||||
return [
|
||||
'id' => [
|
||||
|
@ -47,7 +43,7 @@ class Collections implements Repository {
|
|||
];
|
||||
}
|
||||
|
||||
public function tainacan_register_post_type() {
|
||||
public function register_post_type() {
|
||||
$labels = array(
|
||||
'name' => 'Collections',
|
||||
'singular_name' => 'Collections',
|
||||
|
@ -80,59 +76,9 @@ class Collections implements Repository {
|
|||
'rewrite' => true,
|
||||
'capability_type' => 'post',
|
||||
);
|
||||
register_post_type(Entities\Collection::POST_TYPE, $args);
|
||||
register_post_type(Entities\Collection::get_post_type(), $args);
|
||||
}
|
||||
|
||||
public function insert($collection) {
|
||||
|
||||
// validate
|
||||
if (!$collection->validate()){
|
||||
return $collection->get_errors();
|
||||
}
|
||||
// TODO: Throw Warning saying you must validate object before insert()
|
||||
|
||||
$map = $this->get_map();
|
||||
|
||||
// First iterate through the native post properties
|
||||
foreach ($map as $prop => $mapped) {
|
||||
if ($mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi') {
|
||||
$collection->WP_Post->{$mapped['map']} = $collection->get_mapped_property($prop);
|
||||
}
|
||||
}
|
||||
|
||||
// save post and geet its ID
|
||||
$collection->WP_Post->post_type = Entities\Collection::POST_TYPE;
|
||||
$collection->WP_Post->post_status = 'publish';
|
||||
|
||||
// TODO verificar se salvou mesmo
|
||||
$id = wp_insert_post($collection->WP_Post);
|
||||
|
||||
// reset object
|
||||
$collection->WP_Post = get_post($id);
|
||||
|
||||
// Now run through properties stored as postmeta
|
||||
foreach ($map as $prop => $mapped) {
|
||||
if ($mapped['map'] == 'meta') {
|
||||
update_post_meta($id, $prop, $collection->get_mapped_property($prop));
|
||||
} elseif ($mapped['map'] == 'meta_multi') {
|
||||
$values = $collection->get_mapped_property($prop);
|
||||
|
||||
delete_post_meta($id, $prop);
|
||||
|
||||
if (is_array($values)){
|
||||
foreach ($values as $value){
|
||||
add_post_meta($id, $prop, $value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$collection->tainacan_register_post_type();
|
||||
|
||||
// return a brand new object
|
||||
return new Entities\Collection($collection->WP_Post);
|
||||
}
|
||||
|
||||
public function update($object){
|
||||
|
||||
}
|
||||
|
@ -152,7 +98,7 @@ class Collections implements Repository {
|
|||
return new Entities\Collection($object);
|
||||
} elseif(is_array($object)) {
|
||||
$args = array_merge([
|
||||
'post_type' => Entities\Collection::POST_TYPE,
|
||||
'post_type' => Entities\Collection::get_post_type(),
|
||||
'posts_per_page' => -1,
|
||||
'post_status' => 'publish',
|
||||
], $object);
|
||||
|
|
|
@ -5,12 +5,7 @@ use Tainacan\Entities;
|
|||
|
||||
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
|
||||
|
||||
class Filters implements Repository {
|
||||
|
||||
function __construct(){
|
||||
add_action('init', array(&$this, 'tainacan_register_post_type'));
|
||||
}
|
||||
|
||||
class Filters extends Repository {
|
||||
public function get_map() {
|
||||
return [
|
||||
'id' => [
|
||||
|
@ -52,7 +47,7 @@ class Filters implements Repository {
|
|||
];
|
||||
}
|
||||
|
||||
public function tainacan_register_post_type(){
|
||||
public function register_post_type(){
|
||||
$labels = array(
|
||||
'name' => 'Filter',
|
||||
'singular_name' => 'Filter',
|
||||
|
|
|
@ -5,7 +5,7 @@ use Tainacan\Entities;
|
|||
|
||||
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
|
||||
|
||||
class Item_Metadata implements Repository {
|
||||
class Item_Metadata extends Repository {
|
||||
|
||||
public function insert($item_metadata) {
|
||||
|
||||
|
@ -65,5 +65,5 @@ class Item_Metadata implements Repository {
|
|||
return get_post_meta($object->item->get_id(), $object->metadata->get_id(), $unique);
|
||||
}
|
||||
}
|
||||
|
||||
public function register_post_type() { }
|
||||
}
|
|
@ -5,11 +5,7 @@ use Tainacan\Entities;
|
|||
|
||||
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
|
||||
|
||||
class Items implements Repository {
|
||||
|
||||
function __construct() {
|
||||
add_action('init', array(&$this, 'register_post_types'));
|
||||
}
|
||||
class Items extends Repository {
|
||||
|
||||
public function get_map() {
|
||||
return [
|
||||
|
@ -34,7 +30,7 @@ class Items implements Repository {
|
|||
];
|
||||
}
|
||||
|
||||
public function register_post_types() {
|
||||
public function register_post_type() {
|
||||
|
||||
global $Tainacan_Collections, $Tainacan_Taxonomies;
|
||||
|
||||
|
|
|
@ -10,11 +10,7 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
|
|||
* @author medialab
|
||||
*
|
||||
*/
|
||||
class Logs implements Repository {
|
||||
|
||||
function __construct() {
|
||||
add_action('init', array(&$this, 'tainacan_register_post_type'));
|
||||
}
|
||||
class Logs extends Repository {
|
||||
|
||||
public function get_map() {
|
||||
return [
|
||||
|
@ -57,7 +53,7 @@ class Logs implements Repository {
|
|||
];
|
||||
}
|
||||
|
||||
public function tainacan_register_post_type() {
|
||||
public function register_post_type() {
|
||||
$labels = array(
|
||||
'name' => 'logs',
|
||||
'singular_name' => 'logs',
|
||||
|
|
|
@ -8,12 +8,8 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
|
|||
/**
|
||||
* Class Metadatas
|
||||
*/
|
||||
class Metadatas implements Repository {
|
||||
class Metadatas extends Repository {
|
||||
|
||||
function __construct() {
|
||||
add_action('init', array(&$this, 'tainacan_register_post_type'));
|
||||
}
|
||||
|
||||
public function get_map() {
|
||||
return [
|
||||
'id' => [
|
||||
|
@ -83,7 +79,7 @@ class Metadatas implements Repository {
|
|||
];
|
||||
}
|
||||
|
||||
public function tainacan_register_post_type() {
|
||||
public function register_post_type() {
|
||||
$labels = array(
|
||||
'name' => 'Metadata',
|
||||
'singular_name' => 'Metadata',
|
||||
|
|
|
@ -5,20 +5,22 @@ namespace Tainacan\Repositories;
|
|||
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
|
||||
|
||||
abstract class Repository {
|
||||
protected $entity_type = '\Tainacan\Traits\Entity';
|
||||
protected $entities_type = '\Tainacan\Entities\Entity';
|
||||
|
||||
public function get_map()
|
||||
{
|
||||
function __construct() {
|
||||
add_action('init', array(&$this, 'register_post_type'));
|
||||
}
|
||||
|
||||
public function get_map() {
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param \Tainacan\Traits\Entity $obj
|
||||
* @return \Tainacan\Traits\Entity
|
||||
* @param \Tainacan\Entities\Entity $obj
|
||||
* @return \Tainacan\Entities\Entity
|
||||
*/
|
||||
public function insert($obj) {
|
||||
|
||||
// validate
|
||||
if (!$obj->validate()){
|
||||
return $obj->get_errors();
|
||||
|
@ -35,7 +37,7 @@ abstract class Repository {
|
|||
}
|
||||
|
||||
// save post and geet its ID
|
||||
$obj->WP_Post->post_type = $obj->get_post_type();//$this->ObjectType::POST_TYPE;
|
||||
$obj->WP_Post->post_type = $obj::get_post_type();
|
||||
$obj->WP_Post->post_status = 'publish';
|
||||
|
||||
// TODO verificar se salvou mesmo
|
||||
|
@ -62,13 +64,14 @@ abstract class Repository {
|
|||
}
|
||||
|
||||
// return a brand new object
|
||||
return new $this->entity_type($obj->WP_Post);
|
||||
return new $this->entities_type($obj->WP_Post);
|
||||
}
|
||||
|
||||
public abstract function delete($object);
|
||||
public abstract function fetch($object);
|
||||
public abstract function update($object);
|
||||
|
||||
public abstract function register_post_type();
|
||||
|
||||
}
|
||||
|
||||
?>
|
|
@ -8,12 +8,8 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
|
|||
/**
|
||||
* Class Tainacan_Taxonomies
|
||||
*/
|
||||
class Taxonomies implements Repository {
|
||||
class Taxonomies extends Repository {
|
||||
|
||||
function __construct() {
|
||||
add_action('init', array(&$this, 'tainacan_register_post_type'));
|
||||
}
|
||||
|
||||
public function get_map() {
|
||||
return [
|
||||
'id' => [
|
||||
|
@ -47,7 +43,7 @@ class Taxonomies implements Repository {
|
|||
];
|
||||
}
|
||||
|
||||
public function tainacan_register_post_type() {
|
||||
public function register_post_type() {
|
||||
$labels = array(
|
||||
'name' => 'Taxonomy',
|
||||
'singular_name' => 'Taxonomy',
|
||||
|
@ -147,7 +143,7 @@ class Taxonomies implements Repository {
|
|||
return new Entities\Taxonomy($taxonomy->WP_Post);
|
||||
}
|
||||
|
||||
public function register_tainacan_taxonomy( $taxonomy_name ){
|
||||
public function tainacan_taxonomy( $taxonomy_name ){
|
||||
$labels = array(
|
||||
'name' => __( 'Taxonomies', 'textdomain' ),
|
||||
'singular_name' => __( 'Taxonomy','textdomain' ),
|
||||
|
|
|
@ -8,7 +8,7 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
|
|||
/**
|
||||
* Class Tainacan_Terms
|
||||
*/
|
||||
class Terms implements Repository {
|
||||
class Terms extends Repository {
|
||||
|
||||
public function get_map() {
|
||||
return [
|
||||
|
@ -92,4 +92,6 @@ class Terms implements Repository {
|
|||
public function delete($object){
|
||||
|
||||
}
|
||||
|
||||
public function register_post_type() { }
|
||||
}
|
|
@ -27,6 +27,8 @@ class Collections extends \WP_UnitTestCase {
|
|||
global $Tainacan_Collections;
|
||||
$col = $Tainacan_Collections->insert($x);
|
||||
|
||||
$this->assertEquals('Tainacan\Entities\Collection', get_class($col));
|
||||
|
||||
//
|
||||
|
||||
$test = $Tainacan_Collections->fetch($col->get_id());
|
||||
|
|
Loading…
Reference in New Issue