diff --git a/src/classes/class-tainacan-repository.php b/src/classes/class-tainacan-repository.php index 36c4fc31d..ff32e0805 100644 --- a/src/classes/class-tainacan-repository.php +++ b/src/classes/class-tainacan-repository.php @@ -1,16 +1,16 @@ map; +// $map = $this->map; - if (!key_exists($prop, $map)){ - return null; - } - } +// if (!key_exists($prop, $map)){ +// return null; +// } +// } -} \ No newline at end of file +// } \ No newline at end of file diff --git a/src/classes/entities/class-tainacan-collection.php b/src/classes/entities/class-tainacan-collection.php index 31a43f0cf..bb8cd875c 100644 --- a/src/classes/entities/class-tainacan-collection.php +++ b/src/classes/entities/class-tainacan-collection.php @@ -9,6 +9,7 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); * @author */ class Collection extends \Tainacan\Entity { + const POST_TYPE = 'tainacan-collections'; function __construct($which = 0) { @@ -28,12 +29,12 @@ class Collection extends \Tainacan\Entity { } } - /** + /** * Registra novo tipo de post (post type) * * @return void */ - function register_post_type() { + function tainacan_register_post_type() { $cpt_labels = array( 'name' => 'Item', 'singular_name' => 'Item', diff --git a/src/classes/entities/class-tainacan-metadata.php b/src/classes/entities/class-tainacan-metadata.php index dbb7aaa9e..58aa1a98a 100644 --- a/src/classes/entities/class-tainacan-metadata.php +++ b/src/classes/entities/class-tainacan-metadata.php @@ -2,10 +2,11 @@ namespace Tainacan\Entities; -if ( ! defined( 'ABSPATH' ) ) { - exit; -} +defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); +/** + * Representa a entidade Metadata e extende a super classe Entity + */ class Metadata extends \Tainacan\Entity { use \Tainacan\Traits\Entity_Collection_Relation; @@ -28,133 +29,266 @@ class Metadata extends \Tainacan\Entity { } - // Getters + /** + * Retorna o ID do metadado + * + * @return integer + */ function get_id() { return $this->get_mapped_property('id'); } + /** + * Retorna o nome do metadado + * + * @return string + */ function get_name() { return $this->get_mapped_property('name'); } + /** + * Retorna a forma de ordenação do metadado + * + * @return string + */ function get_order() { return $this->get_mapped_property('order'); } + /** + * Retorna o parent do metadado + * + * @return string + */ function get_parent() { return $this->get_mapped_property('parent'); } + /** + * Retorna a descrição do metado + * + * @return string + */ function get_description() { return $this->get_mapped_property('description'); } + /** + * Retorna se é metadado obrigatório + * + * @return boolean + */ function get_required(){ return $this->get_mapped_property('required'); } + /** + * Retorna se é metado multiplo + * + * @return boolean + */ function get_multiple(){ return $this->get_mapped_property('multiple'); } + /** + * Retorna a cardinalidade + * + * @return string + */ function get_cardinality(){ return $this->get_mapped_property('cardinality'); } + /** + * Retorna se é metadado chave + * + * @return boolean + */ function get_collection_key(){ return $this->get_mapped_property('collection_key'); } + /** + * Retorna a máscara + * + * @return string + */ function get_mask(){ return $this->get_mapped_property('mask'); } + /** + * Retorna o nível de privacidade + * + * @return string + */ function get_privacy(){ return $this->get_mapped_property('privacy'); } + /** + * Retorna valor padrão do metadado + * + * @return string || integer + */ function get_default_value(){ return $this->get_mapped_property('default_value'); } + /** + * Retorna o objeto Metadado + * + * @return array || object + */ function get_field_type_object(){ return unserialize(base64_decode( $this->get_mapped_property('field_type_object') ) ); } + /** + * Retorna o objeto field type + * + * @return array || object + */ function get_field_type(){ return base64_decode($this->get_mapped_property('field_type')); } - // Setters + /** + * Atribui nome + * + * @param [string] $value + * @return void + */ function set_name($value) { - return $this->set_mapped_property('name', $value); - } - - function set_order($value) { - return $this->set_mapped_property('order', $value); - } - - function set_parent($value) { - return $this->set_mapped_property('parent', $value); - } - - function set_description($value) { - return $this->set_mapped_property('description', $value); - } - - function set_required( $value ){ - return $this->set_mapped_property('required', $value); - } - - function set_multiple( $value ){ - return $this->set_mapped_property('multiple', $value); - } - - function set_cardinality( $value ){ - return $this->set_mapped_property('cardinality', $value); - } - - function set_collection_key( $value ){ - return $this->set_mapped_property('collection_key', $value); - } - - function set_mask( $value ){ - return $this->set_mapped_property('mask', $value); - } - - function set_privacy( $value ){ - return $this->set_mapped_property('privacy', $value); - } - - function set_default_value( $value ){ - return $this->set_mapped_property('default_property', $value); - } - - function set_field_type_object(\Tainacan\Field_Types\Field_Type $value){ - $this->set_field_type( get_class( $value ) ); - return $this->set_mapped_property('field_type_object', base64_encode( serialize($value) ) ); // Encode to avoid backslaches removal + $this->set_mapped_property('name', $value); } /** - * este metodo eh privado pois eh setado automaticamente pelo metodo set_field_type_object + * Atribui o tipo de ordenação + * + * @param [string] $value + * @return void + */ + function set_order($value) { + $this->set_mapped_property('order', $value); + } + + /** + * Atribui ID do parent + * + * @param [integer] $value + * @return void + */ + function set_parent($value) { + $this->set_mapped_property('parent', $value); + } + + /** + * Atribui descrição + * + * @param [string] $value + * @return void + */ + function set_description($value) { + $this->set_mapped_property('description', $value); + } + + /** + * Define se é obrigatório + * + * @param [boolean] $value + * @return void + */ + function set_required( $value ){ + $this->set_mapped_property('required', $value); + } + + /** + * Define se é multiplo + * + * @param [boolean] $value + * @return void + */ + function set_multiple( $value ){ + $this->set_mapped_property('multiple', $value); + } + + /** + * Define a cardinalidade + * + * @param [string] $value + * @return void + */ + function set_cardinality( $value ){ + $this->set_mapped_property('cardinality', $value); + } + + /** + * Define se é chave + * + * @param [string] $value + * @return void + */ + function set_collection_key( $value ){ + $this->set_mapped_property('collection_key', $value); + } + + /** + * Atribui máscara + * + * @param [string] $value + * @return void + */ + function set_mask( $value ){ + $this->set_mapped_property('mask', $value); + } + + /** + * Define o nível de privacidade + * + * @param [string] $value + * @return void + */ + function set_privacy( $value ){ + $this->set_mapped_property('privacy', $value); + } + + /** + * Define o valor padrão + * + * @param [string || integer] $value + * @return void + */ + function set_default_value( $value ){ + $this->set_mapped_property('default_property', $value); + } + + function set_field_type_object(\Tainacan\Field_Types\Field_Type $value){ + $this->set_field_type( get_class( $value ) ); + $this->set_mapped_property('field_type_object', base64_encode( serialize($value) ) ); // Encode to avoid backslaches removal + } + + /** + * Este metodo é privado, porque é utilizado apenas neste contexto pelo @method set_field_type_object() * * @param $value - * */ private function set_field_type($value){ - return $this->set_mapped_property('field_type', base64_encode($value) ) ; // Encode to avoid backslaches removal + $this->set_mapped_property('field_type', base64_encode($value) ) ; // Encode to avoid backslaches removal } // helpers function is_multiple() { - return $this->get_multiple() === 'yes'; + $this->get_multiple() === 'yes'; } function is_collection_key() { - return $this->get_collection_key() === 'yes'; + $this->get_collection_key() === 'yes'; } function is_required() { - return $this->get_required() === 'yes'; + $this->get_required() === 'yes'; } } \ No newline at end of file diff --git a/src/classes/entities/class-tainacan-taxonomy.php b/src/classes/entities/class-tainacan-taxonomy.php index cddd0aa19..d4ed38bbb 100644 --- a/src/classes/entities/class-tainacan-taxonomy.php +++ b/src/classes/entities/class-tainacan-taxonomy.php @@ -55,8 +55,8 @@ class Taxonomy extends \Tainacan\Entity { $tax_cpts = []; - if (is_array($this->get_collections())){ - foreach ($this->get_collections() as $tax_col){ + if (is_array($this->fetch())){ + foreach ($this->fetch() as $tax_col){ $tax_cpts[] = $tax_col->get_db_identifier(); } } diff --git a/src/classes/repositories/class-tainacan-collections.php b/src/classes/repositories/class-tainacan-collections.php index d1b147e6d..eaa9bdc3c 100644 --- a/src/classes/repositories/class-tainacan-collections.php +++ b/src/classes/repositories/class-tainacan-collections.php @@ -9,13 +9,12 @@ if ( ! defined( 'ABSPATH' ) ) { use \Respect\Validation\Validator as v; -class Collections { +class Collections implements Repository { - const POST_TYPE = 'tainacan-collections'; var $map; function __construct() { - add_action('init', array(&$this, 'register_post_type')); + add_action('init', array(&$this, 'tainacan_register_post_type')); } function get_map() { @@ -52,12 +51,12 @@ class Collections { ]; } - function register_post_type() { + function tainacan_register_post_type() { $labels = array( 'name' => 'Collections', 'singular_name' => 'Collections', 'add_new' => 'Adicionar Novo', - 'add_new_item' =>'Adicionar Collections', + 'add_new_item' => 'Adicionar Collections', 'edit_item' => 'Editar', 'new_item' => 'Novo Collections', 'view_item' => 'Visualizar', @@ -85,14 +84,15 @@ class Collections { 'rewrite' => true, 'capability_type' => 'post', ); - register_post_type(self::POST_TYPE, $args); + register_post_type(Entities\Collection::POST_TYPE, $args); } - function insert(Entities\Collection $collection) { + function insert($collection) { // validate - if (!$collection->validate()) + if (!$collection->validate()){ return $collection->get_errors(); + } // TODO: Throw Warning saying you must validate object before insert() $map = $this->get_map(); @@ -105,7 +105,7 @@ class Collections { } // save post and geet its ID - $collection->WP_Post->post_type = self::POST_TYPE; + $collection->WP_Post->post_type = Entities\Collection::POST_TYPE; $collection->WP_Post->post_status = 'publish'; // TODO verificar se salvou mesmo @@ -131,36 +131,46 @@ class Collections { } } - $collection->register_post_type(); + $collection->tainacan_register_post_type(); // return a brand new object return new Entities\Collection($collection->WP_Post); } - - function get_collections($args = array()) { - - $args = array_merge([ - 'post_type' => self::POST_TYPE, - 'posts_per_page' => -1, - 'post_status' => 'publish', - ], $args); - - $posts = get_posts($args); - - $return = []; - - foreach ($posts as $post) { - $return[] = new Entities\Collection($post); - } - - // TODO: Pegar coleções registradas via código - - return $return; - } - - function get_collection_by_id($id) { - return new Entities\Collection($id); + + public function update($object){ + } - + public function delete($object){ + + } + + /** + * Obtém um coleção específica pelo ID ou várias coleções + * + * @param array $object || int $object + * @return Array || Collection + */ + public function fetch($object = []){ + if(is_numeric($object)){ + return new Entities\Collection($object); + } elseif(is_array($object)) { + $args = array_merge([ + 'post_type' => Entities\Collection::POST_TYPE, + 'posts_per_page' => -1, + 'post_status' => 'publish', + ], $object); + + $posts = get_posts($args); + + $collections = []; + foreach ($posts as $post) { + $collections[] = new Entities\Collection($post); + } + + // TODO: Pegar coleções registradas via código + + return $collections; + } + } } \ No newline at end of file diff --git a/src/classes/repositories/class-tainacan-items.php b/src/classes/repositories/class-tainacan-items.php index 9bea83946..33ed5876f 100644 --- a/src/classes/repositories/class-tainacan-items.php +++ b/src/classes/repositories/class-tainacan-items.php @@ -40,7 +40,7 @@ class Items { global $Tainacan_Collections, $Tainacan_Taxonomies; - $collections = $Tainacan_Collections->get_collections(); + $collections = $Tainacan_Collections->fetch(); $taxonomies = $Tainacan_Taxonomies->get_taxonomies(); if (!is_array($collections)){ @@ -119,11 +119,11 @@ class Items { global $Tainacan_Collections; if (empty($collections)) { - $collections = $Tainacan_Collections->get_collections(); + $collections = $Tainacan_Collections->fetch(); } if (is_numeric($collections)){ - $collections = $Tainacan_Collections->get_collection_by_id($collection); + $collections = $Tainacan_Collections->fetch($collection); } if ($collections instanceof Entities\Collection) { @@ -133,7 +133,7 @@ class Items { foreach ($collections as $collection) { if (is_numeric($collection)){ - $collection = $Tainacan_Collections->get_collection_by_id($collection); + $collection = $Tainacan_Collections->fetch($collection); } if ($collection instanceof Entities\Collection){ $cpt[] = $collection->get_db_identifier(); diff --git a/src/classes/repositories/class-tainacan-logs.php b/src/classes/repositories/class-tainacan-logs.php index 4bbe95d05..22d9fa74d 100644 --- a/src/classes/repositories/class-tainacan-logs.php +++ b/src/classes/repositories/class-tainacan-logs.php @@ -6,7 +6,7 @@ if ( ! defined( 'ABSPATH' ) ) { exit; } -class Logs extends \Tainacan\Repository { +class Logs { const POST_TYPE = 'tainacan-logs'; diff --git a/src/classes/repositories/class-tainacan-repository.php b/src/classes/repositories/class-tainacan-repository.php new file mode 100644 index 000000000..566e5cc5a --- /dev/null +++ b/src/classes/repositories/class-tainacan-repository.php @@ -0,0 +1,14 @@ + \ No newline at end of file diff --git a/src/classes/traits/class-tainacan-entity-collection-relation.php b/src/classes/traits/class-tainacan-entity-collection-relation.php index 15e4f2d0d..a7c3a0863 100644 --- a/src/classes/traits/class-tainacan-entity-collection-relation.php +++ b/src/classes/traits/class-tainacan-entity-collection-relation.php @@ -18,7 +18,7 @@ trait Entity_Collection_Relation { if (is_numeric($this->get_collection_id())) { global $Tainacan_Collections; - $this->collection = $Tainacan_Collections->get_collection_by_id($this->get_collection_id()); + $this->collection = $Tainacan_Collections->fetch($this->get_collection_id()); return $this->collection; } diff --git a/src/classes/traits/class-tainacan-entity-collections-relation.php b/src/classes/traits/class-tainacan-entity-collections-relation.php index 2dbc32f60..5df4754a9 100644 --- a/src/classes/traits/class-tainacan-entity-collections-relation.php +++ b/src/classes/traits/class-tainacan-entity-collections-relation.php @@ -6,22 +6,22 @@ namespace Tainacan\Traits; trait Entity_Collections_Relation { - function get_collections_ids() { + function fetch_ids() { return $this->get_mapped_property('collections_ids'); } - function get_collections() { + function fetch() { if (isset($this->collection) && !empty($this->collection) && is_array($this->collection)){ return $this->collection; } - if (is_array($this->get_collections_ids()) && !empty(is_array($this->get_collections_ids()))) { + if (is_array($this->fetch_ids()) && !empty(is_array($this->fetch_ids()))) { global $Tainacan_Collections; $collections = []; - foreach ($this->get_collections_ids() as $col_id) { - $collections[] = $Tainacan_Collections->get_collection_by_id($col_id); + foreach ($this->fetch_ids() as $col_id) { + $collections[] = $Tainacan_Collections->fetch($col_id); } return $collections; diff --git a/tests/test-collections.php b/tests/test-collections.php index 8ebcd82ae..b8a09b280 100644 --- a/tests/test-collections.php +++ b/tests/test-collections.php @@ -29,7 +29,7 @@ class Collections extends \WP_UnitTestCase { // - $test = $Tainacan_Collections->get_collection_by_id($col->get_id()); + $test = $Tainacan_Collections->fetch($col->get_id()); $this->assertEquals($test->get_name(), 'teste'); @@ -51,7 +51,7 @@ class Collections extends \WP_UnitTestCase { global $Tainacan_Collections; $col = $Tainacan_Collections->insert($x); - $collection = $Tainacan_Collections->get_collection_by_id($col->get_id()); + $collection = $Tainacan_Collections->fetch($col->get_id());