move prop id to super class, check post_types on object creation, set some defaults props, abstract get_map fucntion, fix map filter for use only in the right entity, add initial objects tests

This commit is contained in:
Jacson Passold 2017-12-11 12:25:28 -02:00
parent 5670e5c134
commit c10407f74b
17 changed files with 259 additions and 177 deletions

View File

@ -94,14 +94,6 @@ class Collection extends Entity {
register_post_type($cpt_slug, $args);
}
/**
* Get the collection ID
*
* @return integer
*/
function get_id() {
return $this->get_mapped_property('id');
}
/**
* Get collection name
*

View File

@ -6,6 +6,7 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
* Entity Super class
*
*/
class Entity {
/**
@ -58,8 +59,21 @@ class Entity {
} else {
$this->WP_Post = new \StdClass();
}
if(is_int($which) && $which != 0 && $this->WP_Post->post_type != $this->get_post_type() ) {
throw new \Exception('the returned post is not the same type of the entity! expected: '.$this->get_post_type().' and actual: '.$this->WP_Post->post_type );
if(
is_int($which) &&
$which != 0 &&
(
( $this->get_post_type() !== false && $this->WP_Post->post_type != $this->get_post_type() ) ||
// Lets check if it is a collection and have rigth post_type
( $this->get_post_type() === false && $this->WP_Post->post_type != Collection::$db_identifier_prefix.$this->get_db_identifier() ) // TODO check if we can use only get_db_identifier for this
)
) {
if($this->get_post_type() === false) {
throw new \Exception('the returned post is not the same type of the entity! expected: '.Collection::$db_identifier_prefix.$this->get_db_identifier().' and actual: '.$this->WP_Post->post_type );
}
else {
throw new \Exception('the returned post is not the same type of the entity! expected: '.$this->get_post_type().' and actual: '.$this->WP_Post->post_type );
}
}
}
@ -86,13 +100,12 @@ class Entity {
$this->set_validated(false);
$this->$prop = $value;
}
/**
* Define the entity status
*
* @param [string] $value
*/
public function set_status($value){
/**
* set the status of the entity
* @param string $value
*/
public function set_status($value){
$value = apply_filters('tainacan-set-post-status', $value);
$this->set_mapped_property('status', $value);
}
@ -160,43 +173,42 @@ class Entity {
return $is_valid;
}
/**
* Return the array of error messages
*
* @return array
*/
public function get_errors() {
public function get_errors() {
return $this->errors;
}
/**
* Return the entity post type
*
* @return string
*/
public static function get_post_type() {
public static function get_post_type() {
return static::$post_type;
}
/**
* Return the entity status
*
* @return mixed
*/
public function get_status(){
public function get_status(){
$value = $this->get_mapped_property('status');
if(empty($value)) $value = 'draft';
return apply_filters('tainacan-get-post-status', $value);
}
/**
* Make a array of error messages
*
* @param $type
* @param $message
*/
public function add_error($type, $message) {
/**
* Get entity DB identifier
*
* This identifier is used to register the entity on database, ex.: post_type
*
* @return string
*/
function get_db_identifier() {
return self::get_post_type();
}
/**
* Get the entity ID
*
* @return integer
*/
public function get_id() {
return $this->get_mapped_property('id');
}
public function add_error($type, $message) {
$this->errors[] = [$type => $message];
}
@ -206,41 +218,21 @@ class Entity {
public function reset_errors() {
$this->errors = [];
}
/**
* Return true if is validated or false if not validated
*
* @return boolean
*/
public function get_validated() {
public function get_validated() {
return $this->validated;
}
/**
* Define validated
*
* @param [boolean] $value
*/
protected function set_validated($value) {
protected function set_validated($value) {
$this->validated = $value;
}
/**
* Define as valid
*
* @return boolean
*/
protected function set_as_valid() {
protected function set_as_valid() {
$this->reset_errors();
$this->set_validated(true);
return true;
}
/**
* Return the attributes of the entity in JSON
*
* @return JSON
*/
public function __toJSON(){
global ${$this->repository};
$map = ${$this->repository}->get_map();

View File

@ -23,15 +23,6 @@ class Filter extends Entity {
return 'Hello, my name is '. $this->get_name();
}
/**
* Return the filter ID
*
* @return integer
*/
function get_id() {
return $this->get_mapped_property('id');
}
/**
* Return the filter name
*

View File

@ -28,14 +28,14 @@ class Item extends Entity {
function get_id() {
return $this->get_mapped_property('id');
}
/**
* Return the item title
*
* @return string
*/
function get_title() {
return $this->get_mapped_property('title');
return $this->get_mapped_property('title');
}
/**
@ -65,6 +65,15 @@ class Item extends Entity {
return $this->get_mapped_property('description');
}
/**
*
* {@inheritDoc}
* @see \Tainacan\Entities\Entity::get_db_identifier()
*/
public function get_db_identifier() {
return $this->get_mapped_property('collection_id');
}
/**
* Define the title
*

View File

@ -30,15 +30,6 @@ class Log extends Entity {
return 'Hello, my title is '. $this->get_title();
}
/**
* Return the Log ID
*
* @return integer
*/
function get_id() {
return $this->get_mapped_property('id');
}
/**
* Return the Log title
*

View File

@ -24,15 +24,6 @@ class Metadata extends Entity {
return 'Hello, my name is '. $this->get_name();
}
/**
* Return the metadata ID
*
* @return integer
*/
function get_id() {
return $this->get_mapped_property('id');
}
/**
* Return the metadata name
*

View File

@ -98,15 +98,6 @@ class Taxonomy extends Entity {
// Getters
/**
* Return the unique identifier
*
* @return integer
*/
function get_id() {
return $this->get_mapped_property('id');
}
/**
* Return the name
*

View File

@ -11,14 +11,7 @@ use Tainacan\Entities\Collection;
class Collections extends Repository {
public $entities_type = '\Tainacan\Entities\Collection';
public function get_map() {
return apply_filters('tainacan-get-map', [
'id' => [
'map' => 'ID',
'title' => __('ID', 'tainacan'),
'type' => 'integer',
'description'=> __('Unique identifier', 'tainacan'),
//'validation' => v::numeric(),
],
return apply_filters('tainacan-get-map-'.$this->get_name(), [
'name' => [
'map' => 'post_title',
'title' => __('Name', 'tainacan'),

View File

@ -10,14 +10,7 @@ class Filters extends Repository {
public $entities_type = '\Tainacan\Entities\Filter';
public function get_map() {
return apply_filters('tainacan-get-map', [
'id' => [
'map' => 'ID',
'title' => __('ID', 'tainacan'),
'type' => 'integer',
'description'=> __('Unique identifier', 'tainacan'),
//'validation' => ''
],
return apply_filters('tainacan-get-map-'.$this->get_name(), [
'name' => [
'map' => 'post_title',
'title' => __('Name', 'tainacan'),

View File

@ -83,4 +83,8 @@ class Item_Metadata extends Repository {
}
public function register_post_type() { }
public function get_map() { return []; }
public function get_default_properties($map) { return []; }
}

View File

@ -9,14 +9,7 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
class Items extends Repository {
public $entities_type = '\Tainacan\Entities\Item';
public function get_map() {
return apply_filters('tainacan-get-map', [
'id' => [
'map' => 'ID',
'title' => __('ID', 'tainacan'),
'type' => 'integer',
'description'=> __('Unique identifier', 'tainacan'),
//'validation' => ''
],
return apply_filters('tainacan-get-map-'.$this->get_name(), [
'title' => [
'map' => 'post_title',
'title' => __('Title', 'tainacan'),

View File

@ -19,14 +19,7 @@ class Logs extends Repository {
}
public function get_map() {
return apply_filters('tainacan-get-map', [
'id' => [
'map' => 'ID',
'title' => 'ID',
'type' => 'integer',
'description' => __('Unique identifier', 'tainacan'),
//'validation' => ''
],
return apply_filters('tainacan-get-map-'.$this->get_name(), [
'title' => [
'map' => 'post_title',
'title' => __('Title', 'tainacan'),

View File

@ -16,15 +16,7 @@ class Metadatas extends Repository {
public $field_types = [];
public function get_map() {
return apply_filters('tainacan-get-map', [
'id' => [
'map' => 'ID',
'title' => __('ID', 'tainacan'),
'type' => 'integer',
'description'=> __('Unique identifier', 'tainacan'),
// 'on_error' => __('The ID should be numeric', 'tainacan'),
// 'validation' => v::numeric(),
],
return apply_filters('tainacan-get-map-'.$this->get_name(), [
'name' => [
'map' => 'post_title',
'title' => __('Name', 'tainacan'),

View File

@ -16,7 +16,7 @@ abstract class Repository {
*/
function __construct() {
add_action('init', array(&$this, 'register_post_type'));
add_filter('tainacan-get-map', array($this, 'get_default_properties'));
add_filter('tainacan-get-map-'.$this->get_name(), array($this, 'get_default_properties'));
}
/**
@ -44,9 +44,7 @@ abstract class Repository {
* 'validation' => v::stringType(),
* ],
*/
public function get_map() {
return apply_filters('tainacan-get-map', array());
}
public abstract function get_map();
/**
* Return repository name
@ -234,21 +232,31 @@ abstract class Repository {
/**
* Return default properties
*
* @param $map
* @param array $map
*
* @return array
*/
public function get_default_properties($map) {
if(is_array($map)) {
$map['status'] = [
'map' => 'post_status',
'title' => __('Status', 'tainacan'),
'type' => 'string',
'description' => __('Status', 'tainacan'),
//'validation' => v::stringType(),
];
}
return $map;
if(is_array($map)) {
$defaults = array(
'status' => array(
'map' => 'post_status',
'title' => __('Status', 'tainacan'),
'type' => 'string',
'description' => __('Status', 'tainacan'),
//'validation' => v::stringType(),
),
'id' => array(
'map' => 'ID',
'title' => __('ID', 'tainacan'),
'type' => 'integer',
'description'=> __('Unique identifier', 'tainacan'),
//'validation' => v::numeric(),
),
);
return array_merge($defaults, $map);
}
return $map;
}
/**
@ -286,8 +294,11 @@ abstract class Repository {
return $property;
}
public static function get_collections_db_identifier()
{
/**
* Return array of collections db identifiers
* @return array[]
*/
public static function get_collections_db_identifier() {
global $Tainacan_Collections;
$collections = $Tainacan_Collections->fetch([], 'OBJECT');
$cpts = [];
@ -297,6 +308,35 @@ abstract class Repository {
return $cpts;
}
public static function get_entity_by_post($post) {
$post_type = $post->post_type;
$prefix = substr($post_type, 0, strlen(Entities\Collection::$db_identifier_prefix));
// its is a collection Item?
if($prefix == Entities\Collection::$db_identifier_prefix) {
$cpts = self::get_collections_db_identifier();
if(array_key_exists($post_type, $cpts)) {
return $entity = new \Tainacan\Entities\Item($post);
}
else {
throw new \Exception('Collection object not found for this post');
}
}
else {
global $Tainacan_Collections,$Tainacan_Metadatas, $Tainacan_Item_Metadata,$Tainacan_Filters,$Tainacan_Taxonomies,$Tainacan_Terms,$Tainacan_Logs;
$tnc_globals = [$Tainacan_Collections,$Tainacan_Metadatas, $Tainacan_Item_Metadata,$Tainacan_Filters,$Tainacan_Taxonomies,$Tainacan_Terms,$Tainacan_Logs];
foreach ($tnc_globals as $tnc_repository)
{
$entity_post_type = $tnc_repository->entities_type::get_post_type();
if($entity_post_type == $post_type)
{
return new $tnc_repository->entities_type($post);
}
}
}
return false;
}
/**
* @param $object
* @return mixed

View File

@ -14,14 +14,7 @@ class Taxonomies extends Repository {
public $entities_type = '\Tainacan\Entities\Taxonomy';
public function get_map() {
return apply_filters('tainacan-get-map', [
'id' => [
'map' => 'ID',
'title' => __('ID', 'tainacan'),
'type' => 'integer',
'description'=> __('Unique identifier', 'tainacan'),
//'validation' => ''
],
return apply_filters('tainacan-get-map-'.$this->get_name(), [
'name' => [
'map' => 'post_title',
'title' => __('Name', 'tainacan'),

View File

@ -14,7 +14,7 @@ class Terms extends Repository {
public $entities_type = '\Tainacan\Entities\Term';
public function get_map() {
return apply_filters('tainacan-get-map', [
return apply_filters('tainacan-get-map-'.$this->get_name(), [
'term_id' => [
'map' => 'term_id',
'title' => __('ID', 'tainacan'),
@ -63,6 +63,14 @@ class Terms extends Repository {
]);
}
public function get_default_properties($map)
{
$defaults = parent::get_default_properties($map);
//its uses the term_id and not id
unset($defaults['id']);
return $defaults;
}
public function insert($term){
// First iterate through the native post properties
$map = $this->get_map();

116
tests/test-objects.php Normal file
View File

@ -0,0 +1,116 @@
<?php
namespace Tainacan\Tests;
use Tainacan\Repositories\Repository;
/**
* Class TestCollections
*
* @package Test_Tainacan
*/
/**
* Sample test case.
* @group architecture
*/
class Objects extends TAINACAN_UnitTestCase {
function test_object_transformation()
{
$x = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'testeT',
'description' => 'adasdasdsa',
'default_order' => 'DESC'
),
true
);
$test = get_post($x->get_id());
$entity = Repository::get_entity_by_post($test);
$this->assertEquals($x->get_db_identifier(), $entity->get_db_identifier());
$type = $this->tainacan_field_factory->create_field('text');
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste',
'status' => 'publish'
),
true
);
$collection2 = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'teste2',
'status' => 'publish'
),
true
);
$metadata = $this->tainacan_entity_factory->create_entity(
'metadata',
array(
'name' => 'metadado',
'status' => 'publish',
'collection' => $collection,
'field_type' => $type
),
true
);
$metadata2 = $this->tainacan_entity_factory->create_entity(
'metadata',
array(
'name' => 'metadado2',
'status' => 'publish',
'collection' => $collection,
'field_type' => $type
),
true
);
$metadata3 = $this->tainacan_entity_factory->create_entity(
'metadata',
array(
'name' => 'metadado3',
'status' => 'publish',
'collection' => $collection,
'field_type' => $type
),
true
);
global $Tainacan_Items;
$i = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'orange',
'collection' => $collection,
'add_metadata' => [
[$metadata, 'value_1']
]
),
true
);
$test = get_post($i->get_id());
$entity = Repository::get_entity_by_post($test);
$this->assertEquals($i->get_db_identifier(), $entity->get_db_identifier());
$test = get_post($metadata->get_id());
$entity = Repository::get_entity_by_post($test);
$this->assertEquals($metadata->get_db_identifier(), $entity->get_db_identifier());
$test = get_post($metadata2->get_id());
$entity = Repository::get_entity_by_post($test);
$this->assertEquals($metadata2->get_db_identifier(), $entity->get_db_identifier());
$metadatas = $i->get_metadata();
//var_dump($metadatas);
$item_metadata = array_pop($metadatas);
$test = get_post($item_metadata->get_metadata()->get_id());
$entity = Repository::get_entity_by_post($test);
$this->assertEquals($item_metadata->get_metadata()->get_db_identifier(), $entity->get_db_identifier());
}
}