refactor repositories fetch method

This commit is contained in:
Eduardo humberto 2017-11-29 16:06:22 -02:00
parent 75d609e2b7
commit 57b3e5586b
16 changed files with 355 additions and 152 deletions

View File

@ -221,7 +221,7 @@ class Collection extends Entity {
*/
function get_metadata() {
$Tainacan_Metadatas = new \Tainacan\Repositories\Metadatas();
return $Tainacan_Metadatas->fetch($this);
return $Tainacan_Metadatas->fetch_by_collection( $this );
}
/**

View File

@ -75,7 +75,7 @@ class Item_Metadata_Entity extends Entity {
return $this->value;
global $Tainacan_Item_Metadata;
return $Tainacan_Item_Metadata->fetch($this);
return $Tainacan_Item_Metadata->get_value($this);
}
/**
@ -167,7 +167,7 @@ class Item_Metadata_Entity extends Entity {
]
]);
if (!empty($test)) {
if ($test->have_posts()) {
$this->add_error('key_exists', $metadata->get_name() . ' is a collection key and there is another item with the same value');
return false;
}

View File

@ -119,13 +119,15 @@ class Item extends Entity {
* @return Array || Metadata
*/
function get_metadata() {
global $Tainacan_Metadatas;
if (isset($this->metadata))
return $this->metadata;
$collection = $this->get_collection();
$all_metadata = [];
if ($collection) {
$meta_list = $collection->get_metadata();
$meta_list = $Tainacan_Metadatas->fetch_by_collection( $collection );
foreach ($meta_list as $meta) {
$all_metadata[$meta->get_id()] = new Item_Metadata_Entity($this, $meta);

View File

@ -156,31 +156,28 @@ class Collections extends Repository {
}
/**
* Obtém um coleção específica pelo ID ou várias coleções
* fetch collection based on ID or WP_Query args
*
* @param array $object || int $object
* @return Array || Collection
* Collections are stored as posts. Check WP_Query docs
* to learn all args accepted in the $args parameter
*
* @param array $args WP_Query args || int $args the collection id
* @return \WP_Query an instance of wp query
*/
public function fetch($object = []){
if(is_numeric($object)){
return new Entities\Collection($object);
} elseif(is_array($object)) {
public function fetch($args = []){
if(is_numeric( $args )){
return new Entities\Collection($args);
} elseif(is_array($args)) {
$args = array_merge([
'post_type' => Entities\Collection::get_post_type(),
'posts_per_page' => -1,
'post_status' => 'publish',
], $object);
], $args);
$posts = get_posts($args);
$collections = [];
foreach ($posts as $post) {
$collections[] = new Entities\Collection($post);
}
$args['post_type'] = Entities\Collection::get_post_type();
// TODO: Pegar coleções registradas via código
return $collections;
return new \WP_Query($args);
}
}
}

View File

@ -134,52 +134,100 @@ class Filters extends Repository {
}
public function fetch($object = [], $args = []){
/**
* fetch filter based on ID or WP_Query args
*
* Filters are stored as posts. Check WP_Query docs
* to learn all args accepted in the $args parameter
*
* @param array $args WP_Query args || int $args the filter id
* @rreturn new \WP_Query($args);
*/
public function fetch($args = []){
/**
* Se for numérico retorna o objeto filtro
* Se não, mas se valor em $object e $args retorna filtro de coleção especifica
* Se não, mas se for string retorna os filtros pelo tipo de metadado
* Se não, retorna todos os filtros
*/
if(is_numeric($object)){
return new Entities\Filter($object);
} elseif (!empty($object) && !empty($args)) {
if( is_numeric($args) ){
return new Entities\Filter($args);
} elseif (!empty($args)) {
// TODO: get filters from parent collections
$collection_id = ( is_object( $object ) ) ? $object->get_id() : $object;
$args = array_merge([
'post_type' => Entities\Filter::get_post_type(),
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_key' => 'collection_id',
'meta_value' => $collection_id
'post_status' => 'publish'
], $args);
$wp_query = new \WP_Query($args);
$args['post_type'] = Entities\Filter::get_post_type();
return $wp_query;
} elseif(is_string($object)) {
$filters = array();
$filters_type = $this->fetch();
foreach ( $filters_type as $filter_type ){
if( in_array( $object, $filter_type->get_supported_types() ) ){
$filters[] = $filter_type;
}
}
return $filters;
} else {
$filters = array();
foreach (get_declared_classes() as $class) {
if (is_subclass_of($class, '\Tainacan\Filter_Types\Filter_Type')){
$filters[] = new $class();
}
}
return $filters;
return new \WP_Query($args);;
}
// elseif(is_string($object)) {
// $filters = array();
// $filters_type = $this->fetch();
//
// foreach ( $filters_type as $filter_type ){
// if( in_array( $object, $filter_type->get_supported_types() ) ){
// $filters[] = $filter_type;
// }
// }
//
// return $filters;
// } else {
// $filters = array();
//
// foreach (get_declared_classes() as $class) {
// if (is_subclass_of($class, '\Tainacan\Filter_Types\Filter_Type')){
// $filters[] = new $class();
// }
// }
//
// return $filters;
// }
}
/**
* fetch all declared filter type classes
*
* @return Array of Entities\Filter_Types\Filter_Type objects
*/
public function fetch_filter_types(){
$filters = array();
foreach (get_declared_classes() as $class) {
if (is_subclass_of($class, '\Tainacan\Filter_Types\Filter_Type')){
$filters[] = new $class();
}
}
return $filters;
}
/**
* fetch only supported filters for the type specified
*
* @param ( string || array ) $types Primitve types of metadata ( float, string, int)
* @return array Filters supported by the primitive types passed in $types
*/
public function fetch_supported_filter_types($types){
$filter_types = $this->fetch_filter_types();
$supported_filter_types = [];
foreach ( $filter_types as $filter_type){
$filter = new $filter_type();
if( ( is_array( $types ) )){
foreach ( $types as $single_type ) {
if( in_array( $single_type ,$filter->get_supported_types() )){
$supported_filter_types[] = $filter;
}
}
}else if( in_array( $types ,$filter->get_supported_types() )){
$supported_filter_types[] = $filter;
}
}
return $supported_filter_types;
}
}

View File

@ -38,6 +38,12 @@ class Item_Metadata extends Repository {
}
/**
* Fetch Item Metadata objects related to an Item
*
* @param Entities\Item $object
* @return array
*/
public function fetch($object){
if($object instanceof Entities\Item){
global $Tainacan_Items, $Tainacan_Metadatas;
@ -48,7 +54,7 @@ class Item_Metadata extends Repository {
return [];
}
$meta_list = $Tainacan_Metadatas->fetch($collection);
$meta_list = $Tainacan_Metadatas->fetch_by_collection($collection);
$return = [];
@ -59,13 +65,22 @@ class Item_Metadata extends Repository {
}
return $return;
} elseif($object instanceof Entities\Item_Metadata_Entity){
// Retorna o valor do metadado
$unique = ! $object->is_multiple();
return get_post_meta($object->item->get_id(), $object->metadata->get_id(), $unique);
}else{
return [];
}
}
/**
* Get the value for a Item metadata.
*
* @param Entities\Item_Metadata_Entity $item_metadata
* @return mixed
*/
public function get_value(Entities\Item_Metadata_Entity $item_metadata) {
$unique = ! $item_metadata->is_multiple();
return get_post_meta($item_metadata->item->get_id(), $item_metadata->metadata->get_id(), $unique);
}
public function register_post_type() { }
}

View File

@ -53,6 +53,9 @@ class Items extends Repository {
}
public function insert($item) {
global $Tainacan_Metadatas;
$map = $this->get_map();
// get collection to determine post type
@ -110,7 +113,20 @@ class Items extends Repository {
return new Entities\Item($item->WP_Post);
}
public function fetch($args = [], $object = []){
/**
* fetch items based on ID or WP_Query args
*
* Items are stored as posts. Check WP_Query docs
* to learn all args accepted in the $args parameter
*
* The second paramater specifies from which collections item should be fetched.
* You can pass the Collection ID or object, or an Array of IDs or collection objects
*
* @param array $args WP_Query args || int $args the item id
* @param array $collections Array Entities\Collection || Array int collections IDs || int collection id || Entities\Collection collection object
* @return \WP_Query an instance of wp query
*/
public function fetch($args = [],$collections = []){
global $Tainacan_Collections;
@ -118,20 +134,26 @@ class Items extends Repository {
return new Entities\Item($args);
}
if (empty($object)) {
$object = $Tainacan_Collections->fetch();
if (empty($collections)){
$wp_query = $Tainacan_Collections->fetch();
if( $wp_query->have_posts() ){
while ( $wp_query->have_posts() ){
$wp_query->the_post();
$collections[] = new Entities\Collection( get_the_ID() );
}
}
}
if (is_numeric($object)){
$object = $Tainacan_Collections->fetch($collection);
if (is_numeric($collections)){
$collections = $Tainacan_Collections->fetch($collections);
}
if ($object instanceof Entities\Collection) {
$cpt = $object->get_db_identifier();
} elseif (is_array($object)) {
if ($collections instanceof Entities\Collection) {
$cpt = $collections->get_db_identifier();
} elseif (is_array($collections)) {
$cpt = [];
foreach ($object as $collection) {
foreach ($collections as $collection) {
if (is_numeric($collection)){
$collection = $Tainacan_Collections->fetch($collection);
}
@ -148,21 +170,15 @@ class Items extends Repository {
return [];
}
//TODO: get collection order and order by options
$args = array_merge([
'post_type' => $cpt,
'posts_per_page' => -1,
'post_status' => 'publish',
], $args);
$posts = get_posts($args);
$args['post_type'] = $cpt;
$return = [];
foreach ($posts as $post) {
$return[] = new Entities\Item($post);
}
return $return;
return new \WP_Query($args);
}
public function update($object){

View File

@ -108,15 +108,25 @@ class Logs extends Repository {
register_post_type(Entities\Log::get_post_type(), $args);
}
public function fetch($object = []){
if(is_numeric($object)){
return new Entities\Log($object);
/**
* fetch logs based on ID or WP_Query args
*
* Logs are stored as posts. Check WP_Query docs
* to learn all args accepted in the $args parameter
*
* @param array $args WP_Query args || int $args the log id
* @return Array of Entities\Log objects || Entities\Log
*/
public function fetch($args = []){
if(is_numeric($args)){
return new Entities\Log($args);
} else {
$args = array_merge([
'post_type' => Entities\Log::get_post_type(),
'posts_per_page' => -1,
'post_status' => 'publish',
], $object);
], $args);
$args['post_type'] = Entities\Log::get_post_type();
$posts = get_posts($args);
@ -125,7 +135,7 @@ class Logs extends Repository {
foreach ($posts as $post) {
$logs[] = new Entities\Log($post);
}
// TODO: Pegar coleções registradas via código
return $logs;
}
}

View File

@ -117,33 +117,62 @@ class Metadatas extends Repository {
register_post_type(Entities\Metadata::get_post_type(), $args);
}
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;
/**
* fetch metadata based on ID or WP_Query args
*
* metadata are stored as posts. Check WP_Query docs
* to learn all args accepted in the $args parameter
*
* @param array $args WP_Query args || int $args the metadata id
* @return \WP_Query an instance of wp query
*/
public function fetch( $args ) {
if( is_numeric($args) ){
return new Entities\Metadata($args);
} elseif (!empty($args)) {
$args = array_merge([
'post_type' => Entities\Metadata::get_post_type(),
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_key' => 'collection_id',
'meta_value' => $collection_id
'post_status' => 'publish'
], $args);
$posts = get_posts($args);
$args['post_type'] = Entities\Metadata::get_post_type();
$return = [];
foreach ($posts as $post) {
$return[] = new Entities\Metadata($post);
}
return $return;
return new \WP_Query($args);
}
}
/**
* fetch metadata by collection
*
* @param Entities\Collection $collection
* @param array $args
* @return Array Entities\Metadata
*/
public function fetch_by_collection(Entities\Collection $collection, $args = []){
$metadata = [];
$collection_id = $collection->get_id();
$args = array_merge([
'meta_key' => 'collection_id',
'meta_value' => $collection_id
], $args);
$wp_query = $this->fetch($args);
if ( $wp_query->have_posts() ){
while ( $wp_query->have_posts() ) {
$wp_query->the_post();
$metadata[] = new Entities\Metadata( get_the_ID() );
}
}
return $metadata;
}
public function update($object){
}

View File

@ -71,9 +71,27 @@ abstract class Repository {
return new $this->entities_type($obj->WP_Post);
}
/**
* @param $object
* @return mixed
*/
public abstract function delete($object);
public abstract function fetch($object);
/**
* @param $args
* @return mixed
*/
public abstract function fetch($args);
/**
* @param $object
* @return mixed
*/
public abstract function update($object);
/**
* @return mixed
*/
public abstract function register_post_type();
}

View File

@ -139,8 +139,30 @@ class Taxonomies extends Repository {
register_taxonomy( $taxonomy_name, array( ), $args );
}
public function fetch($object) {
return new Entities\Taxonomy($object);
/**
* fetch taxonomies based on ID or WP_Query args
*
* Taxonomies are stored as posts. Check WP_Query docs
* to learn all args accepted in the $args parameter
*
* @param array $args WP_Query args || int $args the taxonomy id
* @return Array of Entities\Taxonomy objects || Entities\Taxonomy
*/
public function fetch( $args ) {
if( is_numeric($args) ){
return new Entities\Taxonomy($args);
} elseif (!empty($args)) {
$args = array_merge([
'posts_per_page' => -1,
'post_status' => 'publish'
], $args);
$args['post_type'] = Entities\Taxonomy::get_post_type();
return new \WP_Query($args);;
}
}
public function update($object){

View File

@ -69,23 +69,58 @@ class Terms extends Repository {
}
/**
* Get a term or all terms
* fetch terms based on ID or get terms args
*
* @param string || Array $object1
* @param string || Array || interger $object2
* @param string $object3
* @return Array of WP_Term || WP_Term
* Terms are stored as terms. Check get_terms() docs
* to learn all args accepted in the $args parameter
*
* The second paramater specifies from which taxonomies should be fetched.
* You can pass the Taxonomy ID or object, or an Array of IDs or taxonomies objects
*
* @param array $args WP_Query args || int $args the term id
* @param array $taxonomies Array Entities\Taxonomy || Array int terms IDs || int collection id || Entities\Taxonomy taxonomy object
* @return Array of Entities\Term objects || Entities\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);
public function fetch( $args = [], $taxonomies = []){
global $Tainacan_Taxonomies;
if ( $taxonomies instanceof Entities\Taxonomy ) {
$cpt = $taxonomies->get_db_identifier();
} elseif (is_array( $taxonomies )) {
$cpt = [];
foreach ($taxonomies as $taxonomy) {
if (is_numeric($taxonomy)){
$taxonomy = $Tainacan_Taxonomies->fetch( $taxonomy );
}
if ($taxonomy instanceof Entities\Taxonomy){
$cpt[] = $taxonomy->get_db_identifier();
}
}
} else {
return [];
}
if(is_array( $args ) && !empty( $cpt ) ){ // if an array of arguments is
$terms = get_terms( $cpt, $args );
$return = [];
foreach ($terms as $term) {
$tainacan_term = new Entities\Term( $term );
$tainacan_term->set_user( get_term_meta($tainacan_term->get_id() , 'user', true ) );
$return[] = $tainacan_term;
}
return $return;
} elseif( is_numeric($args) && !empty($cpt) && !is_array( $cpt ) ){ // if an id is passed taxonomy cannot be an array
$wp_term = get_term_by('id', $args, $cpt);
$tainacan_term = new Entities\Term( $wp_term );
$tainacan_term->set_user( get_term_meta($tainacan_term->get_id() , 'user', true ) );
return $tainacan_term;
}else{
return [];
}
}

View File

@ -63,9 +63,9 @@ class Collections extends \WP_UnitTestCase {
$i->set_collection($collection);
global $Tainacan_Items;
$item = $Tainacan_Items->insert($i);
$item = $Tainacan_Items->insert( $i );
$item = $Tainacan_Items->fetch($item->get_id());
$item = $Tainacan_Items->fetch( $item->get_id() );
$this->assertEquals($item->get_title(), 'item teste');
$this->assertEquals($item->get_description(), 'adasdasdsa');

View File

@ -84,10 +84,10 @@ class Filters extends \WP_UnitTestCase {
function test_get_filters_type(){
global $Tainacan_Filters;
$all_filter_types = $Tainacan_Filters->fetch();
$all_filter_types = $Tainacan_Filters->fetch_filter_types();
$this->assertEquals( 2, count( $all_filter_types ) );
$float_filters = $Tainacan_Filters->fetch('float');
$float_filters = $Tainacan_Filters->fetch_supported_filter_types('float');
$this->assertTrue( count( $float_filters ) > 0 );
}
}

View File

@ -8,6 +8,8 @@ namespace Tainacan\Tests;
* @package Test_Tainacan
*/
use Tainacan\Entities\Entity;
/**
* Sample test case.
*/
@ -77,29 +79,39 @@ class Items extends \WP_UnitTestCase {
// should return all 4 items
$test_query = $Tainacan_Items->query([]);
$this->assertEquals(4, sizeof($test_query));
$this->assertEquals(4, $test_query->post_count );
// should also return all 4 items
$test_query = $Tainacan_Items->query(['collections' => [$collection, $collection2]]);
$this->assertEquals(4, sizeof($test_query));
$this->assertEquals(4, $test_query->post_count);
// should return only the first item
$test_query = $Tainacan_Items->query(['collections' => $collection]);
$this->assertEquals(1, sizeof($test_query));
$this->assertEquals('orange', $test_query[0]->get_title());
$this->assertEquals(1,$test_query->post_count);
$test_query->the_post();
$item = new \Tainacan\Entities\Item( get_the_ID() );
$this->assertEquals('orange', $item->get_title() );
$test_query = $Tainacan_Items->query(['title' => 'orange']);
$this->assertEquals(1, sizeof($test_query));
$this->assertEquals('orange', $test_query[0]->get_title());
$test_query->the_post();
$item = new \Tainacan\Entities\Item( get_the_ID() );
$this->assertEquals(1, $test_query->post_count);
$this->assertEquals('orange', $item->get_title());
// should return the other 3 items
$test_query = $Tainacan_Items->query(['collections' => $collection2]);
$this->assertEquals(3, sizeof($test_query));
$this->assertEquals(3,$test_query->post_count);
$test_query = $Tainacan_Items->query(['title' => 'apple']);
$this->assertEquals(1, sizeof($test_query));
$this->assertEquals('apple', $test_query[0]->get_title());
$apple_meta = $test_query[0]->get_metadata();
$this->assertEquals(2, sizeof($apple_meta));
$test_query->the_post();
$item = new \Tainacan\Entities\Item( get_the_ID() );
$this->assertEquals(1, $test_query->post_count);
$this->assertEquals('apple', $item->get_title());
$apple_meta = $item->get_metadata();
$this->assertEquals(2, sizeof( $apple_meta ));
$apple_meta_values = [];
foreach ($apple_meta as $am) {
$this->assertEquals('value_2', $am->get_value());
@ -115,7 +127,7 @@ class Items extends \WP_UnitTestCase {
]
]
]);
$this->assertEquals(1, sizeof($test_query));
$this->assertEquals(1, $test_query->post_count);
// should return 2 items
$test_query = $Tainacan_Items->query([
@ -127,7 +139,7 @@ class Items extends \WP_UnitTestCase {
]
]
]);
$this->assertEquals(2, sizeof($test_query));
$this->assertEquals(2, $test_query->post_count);
// should return 2 item
$test_query = $Tainacan_Items->query([
@ -140,7 +152,7 @@ class Items extends \WP_UnitTestCase {
]
]
]);
$this->assertEquals(2, sizeof($test_query));
$this->assertEquals(2, $test_query->post_count);
}
}

View File

@ -60,8 +60,7 @@ class Taxonomies extends \WP_UnitTestCase {
$term_id = $Tainacan_Terms->insert( $term ) ;
//retorna um objeto da classe Tainacan_Term
$test = $Tainacan_Terms->fetch('id', $term_id, $taxonomy_test->get_db_identifier());
$test = $Tainacan_Terms->fetch($term_id, $taxonomy_test);
$this->assertEquals( $test->get_name(), 'Rock' );
$this->assertEquals( $test->get_user(), 56 );
}