allow change output in fetch methods

This commit is contained in:
Eduardo humberto 2017-11-30 11:44:29 -02:00
parent 023ef0f15a
commit c15cb6464f
11 changed files with 154 additions and 79 deletions

View File

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

View File

@ -127,7 +127,7 @@ class Item extends Entity {
$collection = $this->get_collection();
$all_metadata = [];
if ($collection) {
$meta_list = $Tainacan_Metadatas->fetch_by_collection( $collection );
$meta_list = $Tainacan_Metadatas->fetch_by_collection( $collection, [], 'OBJECT' );
foreach ($meta_list as $meta) {
$all_metadata[$meta->get_id()] = new Item_Metadata_Entity($this, $meta);

View File

@ -31,7 +31,7 @@ class Collections extends Repository {
//'validation' => v::stringType(),
],
'parent' => [
'map' => 'parent',
'map' => 'post_parent',
'name' => __('Parent Collection', 'tainacan'),
'description'=> __('Parent collection ID', 'tainacan'),
//'validation' => v::stringType(),
@ -138,6 +138,7 @@ class Collections extends Repository {
/**
* @param Tainacan\Entities\Collection $collection
* @return \Tainacan\Entities\Collection
* {@inheritDoc}
* @see \Tainacan\Repositories\Repository::insert()
*/
@ -162,9 +163,10 @@ class Collections extends Repository {
* 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
* @param string $output One of 2 pre-defined constants 'WP_Query' | 'OBJECT' . Defaults to WP_Query
* @return \WP_Query|Array an instance of wp query OR array of entities;
*/
public function fetch($args = []){
public function fetch($args = [], $output = 'WP_Query'){
if(is_numeric( $args )){
return new Entities\Collection($args);
} elseif(is_array($args)) {
@ -177,7 +179,8 @@ class Collections extends Repository {
// TODO: Pegar coleções registradas via código
return new \WP_Query($args);
$wp_query = new \WP_Query($args);
return $this->fetch_output($wp_query, $output);
}
}
}

View File

@ -141,15 +141,10 @@ class Filters extends Repository {
* 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
* @param string $output One of 2 pre-defined constants 'WP_Query' | 'OBJECT' . Defaults to WP_Query
* @return \WP_Query|Array an instance of wp query OR array of entities;
*/
public function fetch($args = [], $output = 'WP_Query'){
if( is_numeric($args) ){
return new Entities\Filter($args);
} elseif (!empty($args)) {
@ -161,30 +156,9 @@ class Filters extends Repository {
$args['post_type'] = Entities\Filter::get_post_type();
return new \WP_Query($args);;
$wp_query = new \WP_Query($args);
return $this->fetch_output($wp_query, $output);
}
// 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;
// }
}
/**

View File

@ -54,7 +54,7 @@ class Item_Metadata extends Repository {
return [];
}
$meta_list = $Tainacan_Metadatas->fetch_by_collection($collection);
$meta_list = $Tainacan_Metadatas->fetch_by_collection($collection, [], 'OBJECT' );
$return = [];

View File

@ -124,9 +124,10 @@ class Items extends Repository {
*
* @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
* @param string $output One of 2 pre-defined constants 'WP_Query' | 'OBJECT' . Defaults to WP_Query
* @return \WP_Query|Array an instance of wp query OR array of entities;
*/
public function fetch($args = [],$collections = []){
public function fetch($args = [],$collections = [], $output = 'WP_Query'){
global $Tainacan_Collections;
@ -178,7 +179,8 @@ class Items extends Repository {
$args['post_type'] = $cpt;
return new \WP_Query($args);
$wp_query = new \WP_Query($args);
return $this->fetch_output($wp_query, $output);
}
public function update($object){

View File

@ -116,9 +116,10 @@ class Logs extends Repository {
* 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
* @param string $output One of 2 pre-defined constants 'WP_Query' | 'OBJECT' . Defaults to WP_Query
* @return \WP_Query|Array an instance of wp query OR array of entities;
*/
public function fetch($args = []){
public function fetch($args = [], $output = 'WP_Query'){
if(is_numeric($args)){
return new Entities\Log($args);
} else {
@ -128,15 +129,8 @@ class Logs extends Repository {
$args['post_type'] = Entities\Log::get_post_type();
$posts = get_posts($args);
$logs = [];
foreach ($posts as $post) {
$logs[] = new Entities\Log($post);
}
return $logs;
$wp_query = new \WP_Query($args);
return $this->fetch_output($wp_query, $output);
}
}

View File

@ -10,6 +10,7 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
*/
class Metadatas extends Repository {
protected $entities_type = '\Tainacan\Entities\Metadata';
protected $default_metadata = 'default';
public function get_map() {
return [
@ -117,6 +118,15 @@ class Metadatas extends Repository {
register_post_type(Entities\Metadata::get_post_type(), $args);
}
/**
* constant used in default metadata in attribute collection_id
*
* @return string the value of constant
*/
public function get_default_metadata_attribute(){
return $this->default_metadata;
}
/**
* fetch metadata based on ID or WP_Query args
@ -125,9 +135,10 @@ class Metadatas extends Repository {
* 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
* @param string $output One of 2 pre-defined constants 'WP_Query' | 'OBJECT' . Defaults to WP_Query
* @return \WP_Query|Array an instance of wp query OR array of entities;
*/
public function fetch( $args ) {
public function fetch( $args, $output = 'WP_Query' ) {
if( is_numeric($args) ){
return new Entities\Metadata($args);
@ -140,37 +151,43 @@ class Metadatas extends Repository {
$args['post_type'] = Entities\Metadata::get_post_type();
return new \WP_Query($args);
$wp_query = new \WP_Query($args);
return $this->fetch_output($wp_query, $output);
}
}
/**
* fetch metadata by collection
* fetch metadata by collection, searches all metadata available
*
* @param Entities\Collection $collection
* @param array $args
* @return Array Entities\Metadata
*/
public function fetch_by_collection(Entities\Collection $collection, $args = []){
$metadata = [];
public function fetch_by_collection(Entities\Collection $collection, $args = [], $output = 'WP_Query'){
$collection_id = $collection->get_id();
$args = array_merge([
'meta_key' => 'collection_id',
'meta_value' => $collection_id
], $args);
//get parent collections
$parents = get_post_ancestors( $collection_id );
$wp_query = $this->fetch($args);
//insert the actual collection
$parents[] = $collection_id;
if ( $wp_query->have_posts() ){
while ( $wp_query->have_posts() ) {
$wp_query->the_post();
$metadata[] = new Entities\Metadata( get_the_ID() );
}
//search for default metadata
$parents[] = $this->get_default_metadata_attribute();
$meta_query = array(
'key' => 'collection_id',
'value' => $parents,
'compare' => 'IN',
);
if( isset( $args['meta_query'] ) ){
$args['meta_query'][] = $meta_query;
}else{
$args['meta_query'] = array( $meta_query );
}
return $metadata;
return $this->fetch( $args, $output );
}
public function update($object){

View File

@ -71,6 +71,28 @@ abstract class Repository {
return new $this->entities_type($obj->WP_Post);
}
/**
* @param \WP_Query $WP_Query
* @param string $output
* @return array|\WP_Query
*/
public function fetch_output(\WP_Query $WP_Query, $output = 'WP_Query' ){
if( $output === 'WP_Query'){
return $WP_Query;
}else if( $output === 'OBJECT' ) {
$result = [];
if ( $WP_Query->have_posts() ){
while ( $WP_Query->have_posts() ) {
$WP_Query->the_post();
$result[] = new $this->entities_type( get_the_ID() );
}
}
return $result;
}
}
/**
* @param $object
* @return mixed

View File

@ -145,10 +145,11 @@ class Taxonomies extends Repository {
* 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
* @param array $args WP_Query args | int $args the taxonomy id
* @param string $output One of 2 pre-defined constants 'WP_Query' | 'OBJECT' . Defaults to WP_Query
* @return \WP_Query|Array an instance of wp query OR array of entities;
*/
public function fetch( $args ) {
public function fetch( $args, $output = 'WP_Query' ) {
if( is_numeric($args) ){
return new Entities\Taxonomy($args);
@ -161,7 +162,8 @@ class Taxonomies extends Repository {
$args['post_type'] = Entities\Taxonomy::get_post_type();
return new \WP_Query($args);;
$wp_query = new \WP_Query($args);
return $this->fetch_output($wp_query, $output);
}
}

View File

@ -3,18 +3,18 @@
namespace Tainacan\Tests;
/**
* Class TestCollections
* Class Metadata
*
* @package Test_Tainacan
*/
/**
* Sample test case.
* Metadata test case.
*/
class Metadata extends \WP_UnitTestCase {
/**
* Teste da insercao de um metadado simples sem o tipo
* Test insert a regular metadata with type
*/
function test_add() {
global $Tainacan_Collections, $Tainacan_Metadatas;
@ -42,9 +42,9 @@ class Metadata extends \WP_UnitTestCase {
}
/**
* Teste da insercao de um metadado simples com o tipo
* Test insert a regular metadata with type
*/
function teste_add_type(){
function test_add_type(){
global $Tainacan_Collections, $Tainacan_Metadatas;
$collection = new \Tainacan\Entities\Collection();
@ -70,4 +70,65 @@ class Metadata extends \WP_UnitTestCase {
$this->assertEquals('Tainacan\Field_Types\Text', $test->get_field_type());
$this->assertEquals($test->get_field_type_object(), $type);
}
function test_hierarchy_metadata(){
global $Tainacan_Collections, $Tainacan_Metadatas;
$metadata_default = new \Tainacan\Entities\Metadata();
$collection_grandfather = new \Tainacan\Entities\Collection();
$metadata_grandfather = new \Tainacan\Entities\Metadata();
$collection_father = new \Tainacan\Entities\Collection();
$metadata_father = new \Tainacan\Entities\Metadata();
$collection_son = new \Tainacan\Entities\Collection();
$metadata_son = new \Tainacan\Entities\Metadata();
$type = new \Tainacan\Field_Types\Text();
//creating metadata default
$metadata_default->set_name('metadata default');
$metadata_default->set_collection_id( $Tainacan_Metadatas->get_default_metadata_attribute() );
$metadata_default->set_field_type_object( $type );
$Tainacan_Metadatas->insert($metadata_default);
//creating collection grandfather
$collection_grandfather->set_name('collection grandfather');
$collection_grandfather = $Tainacan_Collections->insert($collection_grandfather);
//creating metadata grandfather
$metadata_grandfather->set_name('metadata grandfather');
$metadata_grandfather->set_collection_id( $collection_grandfather->get_id() );
$metadata_grandfather->set_field_type_object( $type );
$Tainacan_Metadatas->insert($metadata_grandfather);
//creating collection father
$collection_father->set_name('collection father');
$collection_father->set_parent( $collection_grandfather->get_id() );
$collection_father = $Tainacan_Collections->insert( $collection_father );
$this->assertEquals( $collection_grandfather->get_id(), $collection_father->get_parent() );
//creating metadata father
$metadata_father->set_name('metadata father');
$metadata_father->set_collection_id( $collection_father->get_id() );
$metadata_father->set_field_type_object( $type );
$Tainacan_Metadatas->insert($metadata_father);
//creating collection son
$collection_son->set_name('collection son');
$collection_son->set_parent( $collection_father->get_id() );
$collection_son = $Tainacan_Collections->insert($collection_son);
$this->assertEquals( $collection_father->get_id(), $collection_son->get_parent() );
//creating metadata son
$metadata_son->set_name('metadata son');
$metadata_son->set_collection_id( $collection_son->get_id() );
$metadata_son->set_field_type_object( $type );
$Tainacan_Metadatas->insert($metadata_son);
$retrieve_metadata = $Tainacan_Metadatas->fetch_by_collection( $collection_son, [], 'OBJECT' );
$this->assertEquals( 4, sizeof( $retrieve_metadata ) );
}
}