Now Items implements Repository

This commit is contained in:
weryques 2017-11-20 15:13:37 -02:00
parent 3f0e7c5f8d
commit 6a4e2a024c
6 changed files with 70 additions and 55 deletions

View File

@ -75,7 +75,7 @@ class Item_Metadata_Entity extends \Tainacan\Entity {
return $this->value;
global $Tainacan_Item_Metadata;
return $Tainacan_Item_Metadata->get_item_metadata_value($this);
return $Tainacan_Item_Metadata->fetch($this);
}
/**

View File

@ -7,32 +7,9 @@ if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Item_Metadata {
class Item_Metadata implements Repository {
function get_item_metadata_by_item(Entities\Item $item) {
global $Tainacan_Items, $Tainacan_Metadatas;
$collection = $item->get_collection();
if (!$collection instanceof Entities\Collection){
return [];
}
$meta_list = $Tainacan_Metadatas->get_metadata_by_collection($collection);
$return = [];
if (is_array($meta_list)) {
foreach ($meta_list as $meta) {
$return = new Entities\Item_Metadata_Entity($item, $meta);
}
}
return $return;
}
function insert(Entities\Item_Metadata_Entity $item_metadata) {
function insert($item_metadata) {
$unique = ! $item_metadata->is_multiple();
@ -52,12 +29,43 @@ class Item_Metadata {
return new Entities\Item_Metadata_Entity($item_metadata->get_item(), $item_metadata->get_metadata());
}
function get_item_metadata_value(Entities\Item_Metadata_Entity $item_metadata) {
$unique = ! $item_metadata->is_multiple();
public function update($object){
}
public function delete($object){
}
public function fetch($object){
if($object instanceof Entities\Item){
global $Tainacan_Items, $Tainacan_Metadatas;
return get_post_meta($item_metadata->item->get_id(), $item_metadata->metadata->get_id(), $unique);
$collection = $object->get_collection();
if (!$collection instanceof Entities\Collection){
return [];
}
$meta_list = $Tainacan_Metadatas->get_metadata_by_collection($collection);
$return = [];
if (is_array($meta_list)) {
foreach ($meta_list as $meta) {
$return[] = new Entities\Item_Metadata_Entity($object, $meta);
}
}
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);
}
}
}

View File

@ -7,7 +7,7 @@ if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Items {
class Items implements Repository {
function __construct() {
add_action('init', array(&$this, 'register_post_types'));
@ -58,7 +58,7 @@ class Items {
}
}
function insert(Entities\Item $item) {
function insert($item) {
$map = $this->get_map();
// get collection to determine post type
@ -112,26 +112,29 @@ class Items {
// return a brand new object
return new Entities\Item($item->WP_Post);
}
// collections id or array of ids; collection object or array of objects
function get_items($args = array(), $collections = []) {
public function fetch($args = [], $object = []){
global $Tainacan_Collections;
if (empty($collections)) {
$collections = $Tainacan_Collections->fetch();
if(is_numeric($args)){
return new Entities\Item($args);
}
if (empty($object)) {
$object = $Tainacan_Collections->fetch();
}
if (is_numeric($collections)){
$collections = $Tainacan_Collections->fetch($collection);
if (is_numeric($object)){
$object = $Tainacan_Collections->fetch($collection);
}
if ($collections instanceof Entities\Collection) {
$cpt = $collections->get_db_identifier();
} elseif (is_array($collections)) {
if ($object instanceof Entities\Collection) {
$cpt = $object->get_db_identifier();
} elseif (is_array($object)) {
$cpt = [];
foreach ($collections as $collection) {
foreach ($object as $collection) {
if (is_numeric($collection)){
$collection = $Tainacan_Collections->fetch($collection);
}
@ -164,6 +167,14 @@ class Items {
return $return;
}
public function update($object){
}
public function delete($object){
}
// same as WP_Query with few paramaters more:
// collections ID or array of IDs, object or array of objects
@ -210,11 +221,7 @@ class Items {
$collections = !empty($args['collections']) ? $args['collections'] : [];
unset($args['collections']);
return $this->get_items($args, $collections);
return $this->fetch($args, $collections);
### TODO I think its better if we return a WP_Query object. easier for loop and debugging
}
function get_item_by_id($id) {
return new Entities\Item($id);
}
}

View File

@ -64,7 +64,7 @@ class Collections extends \WP_UnitTestCase {
global $Tainacan_Items;
$item = $Tainacan_Items->insert($i);
$item = $Tainacan_Items->get_item_by_id($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

@ -45,7 +45,7 @@ class Item_Metadata extends \WP_UnitTestCase {
global $Tainacan_Items;
$item = $Tainacan_Items->insert($i);
$item = $Tainacan_Items->get_item_by_id($item->get_id());
$item = $Tainacan_Items->fetch($item->get_id());
$item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($item, $metadata);
@ -91,7 +91,7 @@ class Item_Metadata extends \WP_UnitTestCase {
global $Tainacan_Items;
$item = $Tainacan_Items->insert($i);
$item = $Tainacan_Items->get_item_by_id($item->get_id());
$item = $Tainacan_Items->fetch($item->get_id());
$item_metadata = new \Tainacan\Entities\Item_Metadata_Entity($item, $metadata);
@ -136,7 +136,7 @@ class Item_Metadata extends \WP_UnitTestCase {
global $Tainacan_Items;
$item = $Tainacan_Items->insert($i);
$item = $Tainacan_Items->get_item_by_id($item->get_id());
$item = $Tainacan_Items->fetch($item->get_id());

View File

@ -46,7 +46,7 @@ class Items extends \WP_UnitTestCase {
$i->add_metadata($metadata, 'value_1');
$item = $Tainacan_Items->insert($i);
$item = $Tainacan_Items->get_item_by_id($item->get_id());
$item = $Tainacan_Items->fetch($item->get_id());
$meta_test = $item->get_metadata();
$this->assertTrue( isset($meta_test[$metadata->get_id()]) );