From cbe4b093a9d4e65e5d597a4c5eb1d1a74b6f2cf6 Mon Sep 17 00:00:00 2001 From: weryques Date: Tue, 21 Nov 2017 09:15:44 -0200 Subject: [PATCH] Now metadata, term and taxonomy repos. implements repository --- .../entities/class-tainacan-collection.php | 4 +- .../entities/class-tainacan-metadata.php | 103 +++++++----------- .../entities/class-tainacan-taxonomy.php | 4 +- .../class-tainacan-item-metadata.php | 2 +- .../repositories/class-tainacan-metadatas.php | 61 +++++------ .../class-tainacan-taxonomies.php | 24 ++-- .../repositories/class-tainacan-terms.php | 34 ++++-- tests/test-item-metadata.php | 6 +- tests/test-metadata.php | 4 +- tests/test-taxonomies.php | 6 +- 10 files changed, 119 insertions(+), 129 deletions(-) diff --git a/src/classes/entities/class-tainacan-collection.php b/src/classes/entities/class-tainacan-collection.php index 3897e13e7..5330ddd37 100644 --- a/src/classes/entities/class-tainacan-collection.php +++ b/src/classes/entities/class-tainacan-collection.php @@ -164,13 +164,13 @@ class Collection extends \Tainacan\Entity { * * Returns an array of \Entity\Metadata objects, representing all the metadata of the collection. * - * @see \Tainacan\Repositories\Metadatas->get_metadata_by_collection() + * @see \Tainacan\Repositories\Metadatas->fetch() * * @return array */ function get_metadata() { $Tainacan_Metadatas = new \Tainacan\Repositories\Metadatas(); - return $Tainacan_Metadatas->get_metadata_by_collection($this); + return $Tainacan_Metadatas->fetch($this); } /** diff --git a/src/classes/entities/class-tainacan-metadata.php b/src/classes/entities/class-tainacan-metadata.php index 374308690..2d251db92 100644 --- a/src/classes/entities/class-tainacan-metadata.php +++ b/src/classes/entities/class-tainacan-metadata.php @@ -5,16 +5,14 @@ namespace Tainacan\Entities; defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); /** - * Represents the Metada entity + * Representa a entidade Metadata e extende a super classe Entity */ class Metadata extends \Tainacan\Entity { + + const POST_TYPE = 'tainacan-metadata'; use \Tainacan\Traits\Entity_Collection_Relation; - /** - * Create an instance of Metadata - * @param integer|\WP_Post optional $which Metadata ID or a WP_Post object for existing metadata. Leave empty to create a new metadata. - */ function __construct( $which = 0 ) { $this->repository = 'Tainacan_Metadatas'; @@ -34,7 +32,7 @@ class Metadata extends \Tainacan\Entity { } /** - * Get metadata ID + * Retorna o ID do metadado * * @return integer */ @@ -43,7 +41,7 @@ class Metadata extends \Tainacan\Entity { } /** - * Get metadata name + * Retorna o nome do metadado * * @return string */ @@ -52,7 +50,7 @@ class Metadata extends \Tainacan\Entity { } /** - * Get metadata order + * Retorna a forma de ordenação do metadado * * @return string */ @@ -61,7 +59,7 @@ class Metadata extends \Tainacan\Entity { } /** - * Get metadata parent ID + * Retorna o parent do metadado * * @return string */ @@ -70,7 +68,7 @@ class Metadata extends \Tainacan\Entity { } /** - * Get metadata description + * Retorna a descrição do metado * * @return string */ @@ -79,7 +77,7 @@ class Metadata extends \Tainacan\Entity { } /** - * Get metadata required option + * Retorna se é metadado obrigatório * * @return boolean */ @@ -88,16 +86,16 @@ class Metadata extends \Tainacan\Entity { } /** - * Get metadata multiple option + * Retorna se é metado multiplo * - * @return string 'yes or no' + * @return boolean */ function get_multiple(){ return $this->get_mapped_property('multiple'); } /** - * Get metadata cardinality option + * Retorna a cardinalidade * * @return string */ @@ -106,16 +104,16 @@ class Metadata extends \Tainacan\Entity { } /** - * Get metadata collection key option + * Retorna se é metadado chave * - * @return string 'yes or no' + * @return boolean */ function get_collection_key(){ return $this->get_mapped_property('collection_key'); } /** - * Get metadata mask option + * Retorna a máscara * * @return string */ @@ -124,7 +122,7 @@ class Metadata extends \Tainacan\Entity { } /** - * Get metadata privacy option + * Retorna o nível de privacidade * * @return string */ @@ -133,7 +131,7 @@ class Metadata extends \Tainacan\Entity { } /** - * Get metadata default value option + * Retorna valor padrão do metadado * * @return string || integer */ @@ -142,25 +140,25 @@ class Metadata extends \Tainacan\Entity { } /** - * Get metadata field type object with its options + * Retorna o objeto Metadado * - * @return object + * @return array || object */ function get_field_type_object(){ return unserialize(base64_decode( $this->get_mapped_property('field_type_object') ) ); } /** - * Get metadata field type + * Retorna o objeto field type * - * @return string The Field Type class name + * @return array || object */ function get_field_type(){ return base64_decode($this->get_mapped_property('field_type')); } /** - * Set metadata name + * Atribui nome * * @param [string] $value * @return void @@ -170,7 +168,7 @@ class Metadata extends \Tainacan\Entity { } /** - * Set metadata order + * Atribui o tipo de ordenação * * @param [string] $value * @return void @@ -180,7 +178,7 @@ class Metadata extends \Tainacan\Entity { } /** - * Set metadata parent ID + * Atribui ID do parent * * @param [integer] $value * @return void @@ -190,7 +188,7 @@ class Metadata extends \Tainacan\Entity { } /** - * Set metadata description + * Atribui descrição * * @param [string] $value * @return void @@ -200,9 +198,9 @@ class Metadata extends \Tainacan\Entity { } /** - * Set metadata required option + * Define se é obrigatório * - * @param [string] $value yes or no + * @param [boolean] $value * @return void */ function set_required( $value ){ @@ -210,9 +208,9 @@ class Metadata extends \Tainacan\Entity { } /** - * Set metadata multiple option + * Define se é multiplo * - * @param [string] $value yes or no + * @param [boolean] $value * @return void */ function set_multiple( $value ){ @@ -220,9 +218,9 @@ class Metadata extends \Tainacan\Entity { } /** - * Set metadata cardinality + * Define a cardinalidade * - * @param [int] $value + * @param [string] $value * @return void */ function set_cardinality( $value ){ @@ -230,14 +228,9 @@ class Metadata extends \Tainacan\Entity { } /** - * Set metadata collection key option + * Define se é chave * - * Set to yes if you want this metadata to have a unique value inside the collection. - * - * This means that whenever you create or update an item, Tainacan will check and make sure there is no - * other item with the same value for this metadata. - * - * @param [string] $value yes or no + * @param [string] $value * @return void */ function set_collection_key( $value ){ @@ -245,7 +238,7 @@ class Metadata extends \Tainacan\Entity { } /** - * Set metadata mask option + * Atribui máscara * * @param [string] $value * @return void @@ -255,7 +248,7 @@ class Metadata extends \Tainacan\Entity { } /** - * Set metadata privacy option + * Define o nível de privacidade * * @param [string] $value * @return void @@ -265,7 +258,7 @@ class Metadata extends \Tainacan\Entity { } /** - * Set metadata default value + * Define o valor padrão * * @param [string || integer] $value * @return void @@ -274,22 +267,13 @@ class Metadata extends \Tainacan\Entity { $this->set_mapped_property('default_property', $value); } - - /** - * Set metadata field type object - * - * stores the Field Type object with all its options. - * - * @param [\Tainacan\Field_Types\Field_Type] $value An object of a Field Type. Must be an instace of a class child of Field_Type - * @return void - */ 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 } /** - * This is a private methos, called by @method set_field_type_object() to set the field type + * Este metodo é privado, porque é utilizado apenas neste contexto pelo @method set_field_type_object() * * @param $value */ @@ -298,27 +282,14 @@ class Metadata extends \Tainacan\Entity { } // helpers - // - /** - * Returns wether this metadata is multiple or not (has the multiple option set to yes or no) - * @return boolean true if multiple, false if not multiple - */ function is_multiple() { return $this->get_multiple() === 'yes'; } - /** - * Returns wether this metadata is a collection key or not (has the collection key option set to yes or no) - * @return boolean true if collection key, false if not collection key - */ function is_collection_key() { return $this->get_collection_key() === 'yes'; } - /** - * Returns wether this metadata is required or not (has the required option set to yes or no) - * @return boolean true if required, false if not required - */ function is_required() { return $this->get_required() === 'yes'; } diff --git a/src/classes/entities/class-tainacan-taxonomy.php b/src/classes/entities/class-tainacan-taxonomy.php index d4ed38bbb..ac4cae68c 100644 --- a/src/classes/entities/class-tainacan-taxonomy.php +++ b/src/classes/entities/class-tainacan-taxonomy.php @@ -8,7 +8,9 @@ if ( ! defined( 'ABSPATH' ) ) { class Taxonomy extends \Tainacan\Entity { - use \Tainacan\Traits\Entity_Collections_Relation; + use \Tainacan\Traits\Entity_Collections_Relation; + + const POST_TYPE = 'tainacan-taxonomies'; function __construct( $which = 0 ) { diff --git a/src/classes/repositories/class-tainacan-item-metadata.php b/src/classes/repositories/class-tainacan-item-metadata.php index 55d96deaa..33902325e 100644 --- a/src/classes/repositories/class-tainacan-item-metadata.php +++ b/src/classes/repositories/class-tainacan-item-metadata.php @@ -48,7 +48,7 @@ class Item_Metadata implements Repository { return []; } - $meta_list = $Tainacan_Metadatas->get_metadata_by_collection($collection); + $meta_list = $Tainacan_Metadatas->fetch($collection); $return = []; diff --git a/src/classes/repositories/class-tainacan-metadatas.php b/src/classes/repositories/class-tainacan-metadatas.php index f06d77ed4..6640e6aa1 100644 --- a/src/classes/repositories/class-tainacan-metadatas.php +++ b/src/classes/repositories/class-tainacan-metadatas.php @@ -10,9 +10,7 @@ if ( ! defined( 'ABSPATH' ) ) { /** * Class Metadatas */ -class Metadatas { - - const POST_TYPE = 'tainacan-metadata'; +class Metadatas implements Repository { function __construct() { add_action('init', array(&$this, 'register_post_type')); @@ -121,14 +119,14 @@ class Metadatas { 'rewrite' => true, 'capability_type' => 'post', ); - register_post_type(self::POST_TYPE, $args); + register_post_type(Entities\Metadata::POST_TYPE, $args); } /** * @param Entities\Metadata $metadata * @return int */ - function insert( Entities\Metadata $metadata ) { + function insert($metadata) { // First iterate through the native post properties $map = $this->get_map(); foreach ($map as $prop => $mapped) { @@ -138,7 +136,7 @@ class Metadatas { } // save post and get its ID - $metadata->WP_Post->post_type = self::POST_TYPE; + $metadata->WP_Post->post_type = Entities\Metadata::POST_TYPE; $metadata->WP_Post->post_status = 'publish'; $id = wp_insert_post($metadata->WP_Post); @@ -165,41 +163,38 @@ class Metadatas { return new Entities\Metadata($metadata->WP_Post); } - /** - * @param ( Tainacan_Collection ) $collection_id - * @param array $args - * @return array - */ - function get_metadata_by_collection( $collection, $args = array()) { - + public function fetch($object, $args = []){ // TODO: get metadata from parent collections + if(is_numeric($object)){ + return new Entities\Metadata($object); + } else { + $collection_id = ( is_object( $object ) ) ? $object->get_id() : $object; - $collection_id = ( is_object( $collection ) ) ? $collection->get_id() : $collection; + $args = array_merge([ + 'post_type' => Entities\Metadata::POST_TYPE, + 'posts_per_page' => -1, + 'post_status' => 'publish', + 'meta_key' => 'collection_id', + 'meta_value' => $collection_id + ], $args); - $args = array_merge([ - 'post_type' => self::POST_TYPE, - 'posts_per_page' => -1, - 'post_status' => 'publish', - 'meta_key' => 'collection_id', - 'meta_value' => $collection_id - ], $args); + $posts = get_posts($args); - $posts = get_posts($args); + $return = []; - $return = []; + foreach ($posts as $post) { + $return[] = new Entities\Metadata($post); + } - foreach ($posts as $post) { - $return[] = new Entities\Metadata($post); + return $return; } - - return $return; } - /** - * @param int $id - * @return Entities\Metadata - */ - function get_metadata_by_id($id) { - return new Entities\Metadata($id); + public function update($object){ + + } + + public function delete($object){ + } } \ No newline at end of file diff --git a/src/classes/repositories/class-tainacan-taxonomies.php b/src/classes/repositories/class-tainacan-taxonomies.php index 431bdd3ed..6f3d1a683 100644 --- a/src/classes/repositories/class-tainacan-taxonomies.php +++ b/src/classes/repositories/class-tainacan-taxonomies.php @@ -10,9 +10,7 @@ if ( ! defined( 'ABSPATH' ) ) { /** * Class Tainacan_Taxonomies */ -class Taxonomies { - - const POST_TYPE = 'tainacan-taxonomies'; +class Taxonomies implements Repository { function __construct() { add_action('init', array(&$this, 'register_post_type')); @@ -85,12 +83,12 @@ class Taxonomies { 'rewrite' => true, 'capability_type' => 'post', ); - register_post_type(self::POST_TYPE, $args); + register_post_type(Entities\Taxonomy::POST_TYPE, $args); } function get_taxonomies($args = []){ $args = array_merge([ - 'post_type' => self::POST_TYPE, + 'post_type' => Entities\Taxonomy::POST_TYPE, 'posts_per_page' => -1, 'post_status' => 'publish', ], $args); @@ -112,7 +110,7 @@ class Taxonomies { * @param Entities\Taxonomy $metadata * @return int */ - function insert( Entities\Taxonomy $taxonomy ) { + function insert($taxonomy) { // First iterate through the native post properties $map = $this->get_map(); foreach ($map as $prop => $mapped) { @@ -122,7 +120,7 @@ class Taxonomies { } // save post and get its ID - $taxonomy->WP_Post->post_type = self::POST_TYPE; + $taxonomy->WP_Post->post_type = Entities\Taxonomy::POST_TYPE; $taxonomy->WP_Post->post_status = 'publish'; $id = wp_insert_post($taxonomy->WP_Post); @@ -176,7 +174,15 @@ class Taxonomies { register_taxonomy( $taxonomy_name, array( ), $args ); } - function get_taxonomy_by_id($id) { - return new Entities\Taxonomy($id); + public function fetch($object) { + return new Entities\Taxonomy($object); + } + + public function update($object){ + + } + + public function delete($object){ + } } \ No newline at end of file diff --git a/src/classes/repositories/class-tainacan-terms.php b/src/classes/repositories/class-tainacan-terms.php index 67f587b7d..53f2cb9ec 100644 --- a/src/classes/repositories/class-tainacan-terms.php +++ b/src/classes/repositories/class-tainacan-terms.php @@ -10,7 +10,7 @@ if ( ! defined( 'ABSPATH' ) ) { /** * Class Tainacan_Terms */ -class Terms { +class Terms implements Repository { function get_map() { return [ @@ -41,7 +41,7 @@ class Terms { ]; } - function insert( Entities\Term $term ){ + function insert($term){ // First iterate through the native post properties $map = $this->get_map(); foreach ($map as $prop => $mapped) { @@ -66,16 +66,32 @@ class Terms { return $term_inserted['term_id']; } - function get_terms( $taxonomies, $args ){ - return get_terms( $taxonomies, $args ); + /** + * Get a term or all terms + * + * @param string || Array $object1 + * @param string || Array || interger $object2 + * @param string $object3 + * @return Array of WP_Term || WP_Term + */ + public function fetch( $object1 = '', $object2 = '', $object3 = ''){ + if(!empty($object1) && !empty($object2) && empty($object3)){ + return get_terms( $object1, $object2 ); + } elseif(!empty($object1) && !empty($object2) && !empty($object3)){ + $wp_term = get_term_by($object1, $object2, $object3); + + $tainacan_term = new Entities\Term( $wp_term ); + $tainacan_term->set_user( get_term_meta($tainacan_term->get_id() , 'user', true ) ); + + return $tainacan_term; + } } - function get_term_by($field,$value,$taxonomy){ - $wp_term = get_term_by($field,$value,$taxonomy); + public function update($object){ - $tainacan_term = new Entities\Term( $wp_term ); - $tainacan_term->set_user( get_term_meta($tainacan_term->get_id() , 'user', true ) ); + } + + public function delete($object){ - return $tainacan_term; } } \ No newline at end of file diff --git a/tests/test-item-metadata.php b/tests/test-item-metadata.php index 8464fb536..66084cda3 100644 --- a/tests/test-item-metadata.php +++ b/tests/test-item-metadata.php @@ -34,7 +34,7 @@ class Item_Metadata extends \WP_UnitTestCase { //inserindo o metadado $metadata = $Tainacan_Metadatas->insert($metadata); - $test = $Tainacan_Metadatas->get_metadata_by_id($metadata->get_id()); + $test = $Tainacan_Metadatas->fetch($metadata->get_id()); $i = new \Tainacan\Entities\Item(); @@ -80,7 +80,7 @@ class Item_Metadata extends \WP_UnitTestCase { //inserindo o metadado $metadata = $Tainacan_Metadatas->insert($metadata); - $test = $Tainacan_Metadatas->get_metadata_by_id($metadata->get_id()); + $test = $Tainacan_Metadatas->fetch($metadata->get_id()); $i = new \Tainacan\Entities\Item(); @@ -125,7 +125,7 @@ class Item_Metadata extends \WP_UnitTestCase { //inserindo o metadado $metadata = $Tainacan_Metadatas->insert($metadata); - $test = $Tainacan_Metadatas->get_metadata_by_id($metadata->get_id()); + $test = $Tainacan_Metadatas->fetch($metadata->get_id()); $i = new \Tainacan\Entities\Item(); diff --git a/tests/test-metadata.php b/tests/test-metadata.php index 8d7a3fcf4..71b555b64 100644 --- a/tests/test-metadata.php +++ b/tests/test-metadata.php @@ -33,7 +33,7 @@ class Metadata extends \WP_UnitTestCase { //inserindo o metadado $metadata = $Tainacan_Metadatas->insert($metadata); - $test = $Tainacan_Metadatas->get_metadata_by_id($metadata->get_id()); + $test = $Tainacan_Metadatas->fetch($metadata->get_id()); $this->assertEquals($test->get_name(), 'metadado'); $this->assertEquals($test->get_description(), 'descricao'); @@ -63,7 +63,7 @@ class Metadata extends \WP_UnitTestCase { //inserindo o metadado $metadata = $Tainacan_Metadatas->insert($metadata); - $test = $Tainacan_Metadatas->get_metadata_by_id($metadata->get_id()); + $test = $Tainacan_Metadatas->fetch($metadata->get_id()); $this->assertEquals($test->get_name(), 'metadado'); $this->assertEquals($test->get_collection_id(), $collection->get_id()); diff --git a/tests/test-taxonomies.php b/tests/test-taxonomies.php index 617c33ee5..aca3ec720 100644 --- a/tests/test-taxonomies.php +++ b/tests/test-taxonomies.php @@ -31,7 +31,7 @@ class Taxonomies extends \WP_UnitTestCase { $taxonomy = $Tainacan_Taxonomies->insert($taxonomy); //retorna a taxonomia - $test = $Tainacan_Taxonomies->get_taxonomy_by_id($taxonomy->get_id()); + $test = $Tainacan_Taxonomies->fetch($taxonomy->get_id()); $this->assertEquals( $test->get_name(), 'genero' ); $this->assertEquals( $test->get_description(), 'tipos de musica' ); @@ -51,7 +51,7 @@ class Taxonomies extends \WP_UnitTestCase { $taxonomy = $Tainacan_Taxonomies->insert($taxonomy); //retorna a taxonomia - $taxonomy_test = $Tainacan_Taxonomies->get_taxonomy_by_id($taxonomy->get_id()); + $taxonomy_test = $Tainacan_Taxonomies->fetch($taxonomy->get_id()); //insere um termo na taxonomia $term->set_taxonomy( $taxonomy_test->get_db_identifier() ); @@ -60,7 +60,7 @@ class Taxonomies extends \WP_UnitTestCase { $term_id = $Tainacan_Terms->insert( $term ) ; //retorna um objeto da classe Tainacan_Term - $test = $Tainacan_Terms->get_term_by('id', $term_id, $taxonomy_test->get_db_identifier()); + $test = $Tainacan_Terms->fetch('id', $term_id, $taxonomy_test->get_db_identifier()); $this->assertEquals( $test->get_name(), 'Rock' ); $this->assertEquals( $test->get_user(), 56 );