This commit is contained in:
Leo Germani 2017-11-09 10:44:52 -02:00
parent 332acfb3ab
commit c7215e5f30
6 changed files with 170 additions and 8 deletions

View File

@ -49,6 +49,15 @@ class TainacanCollection extends Entity {
return $this->get_mapped_property('itens_per_page');
}
// special Getters
//
function get_db_identifier() {
global $TainacanCollections;
return $TainacanCollections->get_collection_db_identifier($this->get_id());
}
// Setters
//

View File

@ -0,0 +1,97 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class TainacanItem extends Entity {
function __construct($which = 0) {
$this->repository = 'TainacanItems';
if (is_numeric($which) && $which > 0) {
$post = get_post($which);
if ($post instanceof WP_Post) {
$this->WP_Post = get_post($which);
}
} elseif ($which instanceof WP_Post) {
$this->WP_Post = $which;
} else {
$this->WP_Post = new StdClass();
}
}
// Getters
//
function get_id() {
return $this->get_mapped_property('ID');
}
function get_title() {
return $this->get_mapped_property('title');
}
function get_order() {
return $this->get_mapped_property('order');
}
function get_parent() {
return $this->get_mapped_property('parent');
}
function get_description() {
return $this->get_mapped_property('description');
}
function get_collection_id() {
return $this->get_mapped_property('collection_id');
}
// sepecial Getters
function get_collection() {
if (isset($this->collection) && $this->collection instanceof TainacanCollection)
return $this->collection;
if (is_numeric($this->get_collection_id())) {
$collection = get_post($this->get_collection_id());
if ($collection instanceof WP_Post) {
$this->collection = new TainacanCollection($collection);
return $this->collection;
}
}
return null;
}
// Setters
//
function set_title($value) {
return $this->set_mapped_property('title', $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_collection_id($value) {
return $this->set_mapped_property('collection_id', $value);
}
// sepecial Setters
function set_collection(TainacanCollection $collection) {
$this->collection = $collection;
$this->set_collection_id($collection->get_id());
}
}

View File

@ -15,8 +15,6 @@ class Entity {
return $this->$prop;
//$map = $this->map_properties();
if (!array_key_exists($prop, $map))
return null;

View File

@ -7,6 +7,14 @@ if ( ! defined( 'ABSPATH' ) ) {
class TainacanItems {
var $map = [
'ID' => 'ID',
'title' => 'post_title',
'description' => 'post_content',
'collection_id' => 'meta',
//'collection' => 'relation...'
];
function __construct() {
add_action('init', array(&$this, 'register_post_types'));
}
@ -65,10 +73,21 @@ class TainacanItems {
}
/*
function insert(TainacanItem $item) {
// First iterate through the native post properties
$map = $item->map_properties();
$map = $this->map;
// get collection to determine post type
$collection = $item->get_collection();
if (!$collection)
return false;
$cpt = $collection->get_db_identifier();
// iterate through the native post properties
foreach ($map as $prop => $mapped) {
if ($mapped != 'meta') {
$item->WP_Post->$mapped = $item->get_mapped_property($prop);
@ -76,7 +95,7 @@ class TainacanItems {
}
// save post and geet its ID
$item->WP_Post->post_type = self::POST_TYPE;
$item->WP_Post->post_type = $cpt;
$id = wp_insert_post($item->WP_Post);
// Now run through properties stored as postmeta
@ -86,11 +105,16 @@ class TainacanItems {
}
}
// TODO - save item medatada
// get collection Metadata
// foreach metadata...
return $id;
}
*/
function getItemById($id) {
function get_item_by_id($id) {
return new TainacanItem($id);
}

View File

@ -11,6 +11,7 @@ include('classes/Repositories/Collections.php');
include('classes/Repositories/Items.php');
include('classes/Entity.php');
include('classes/Entities/Collection.php');
include('classes/Entities/Item.php');
/**
*

View File

@ -35,4 +35,37 @@ class TestCollections extends WP_UnitTestCase {
}
function test_item() {
$x = new TainacanCollection();
$x->set_name('teste');
$x->set_description('adasdasdsa');
$x->set_itens_per_page(23);
global $TainacanCollections;
$cid = $TainacanCollections->insert($x);
$collection = $TainacanCollections->get_collection_by_id($cid);
$i = new TainacanItem();
$i->set_title('item teste');
$i->set_description('adasdasdsa');
$i->set_collection($collection);
global $TainacanItems;
$id = $TainacanItems->insert($i);
$item = $TainacanItems->get_item_by_id($id);
$this->assertEquals($item->get_title(), 'item teste');
$this->assertEquals($item->get_description(), 'adasdasdsa');
$this->assertEquals($item->get_collection_id(), $cid);
}
}