Repository Interface with collections

This commit is contained in:
weryques 2017-11-20 12:41:54 -02:00
parent c26f7e6d5e
commit 05f5f97eb7
11 changed files with 277 additions and 118 deletions

View File

@ -1,16 +1,16 @@
<?php
namespace Tainacan;
// namespace Tainacan;
class Repository {
// class Repository {
function find_by($prop, $value) {
// function find_by($prop, $value) {
$map = $this->map;
// $map = $this->map;
if (!key_exists($prop, $map)){
return null;
}
}
// if (!key_exists($prop, $map)){
// return null;
// }
// }
}
// }

View File

@ -9,6 +9,7 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
* @author
*/
class Collection extends \Tainacan\Entity {
const POST_TYPE = 'tainacan-collections';
function __construct($which = 0) {
@ -28,12 +29,12 @@ class Collection extends \Tainacan\Entity {
}
}
/**
/**
* Registra novo tipo de post (post type)
*
* @return void
*/
function register_post_type() {
function tainacan_register_post_type() {
$cpt_labels = array(
'name' => 'Item',
'singular_name' => 'Item',

View File

@ -2,10 +2,11 @@
namespace Tainacan\Entities;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/**
* Representa a entidade Metadata e extende a super classe Entity
*/
class Metadata extends \Tainacan\Entity {
use \Tainacan\Traits\Entity_Collection_Relation;
@ -28,133 +29,266 @@ class Metadata extends \Tainacan\Entity {
}
// Getters
/**
* Retorna o ID do metadado
*
* @return integer
*/
function get_id() {
return $this->get_mapped_property('id');
}
/**
* Retorna o nome do metadado
*
* @return string
*/
function get_name() {
return $this->get_mapped_property('name');
}
/**
* Retorna a forma de ordenação do metadado
*
* @return string
*/
function get_order() {
return $this->get_mapped_property('order');
}
/**
* Retorna o parent do metadado
*
* @return string
*/
function get_parent() {
return $this->get_mapped_property('parent');
}
/**
* Retorna a descrição do metado
*
* @return string
*/
function get_description() {
return $this->get_mapped_property('description');
}
/**
* Retorna se é metadado obrigatório
*
* @return boolean
*/
function get_required(){
return $this->get_mapped_property('required');
}
/**
* Retorna se é metado multiplo
*
* @return boolean
*/
function get_multiple(){
return $this->get_mapped_property('multiple');
}
/**
* Retorna a cardinalidade
*
* @return string
*/
function get_cardinality(){
return $this->get_mapped_property('cardinality');
}
/**
* Retorna se é metadado chave
*
* @return boolean
*/
function get_collection_key(){
return $this->get_mapped_property('collection_key');
}
/**
* Retorna a máscara
*
* @return string
*/
function get_mask(){
return $this->get_mapped_property('mask');
}
/**
* Retorna o nível de privacidade
*
* @return string
*/
function get_privacy(){
return $this->get_mapped_property('privacy');
}
/**
* Retorna valor padrão do metadado
*
* @return string || integer
*/
function get_default_value(){
return $this->get_mapped_property('default_value');
}
/**
* Retorna o objeto Metadado
*
* @return array || object
*/
function get_field_type_object(){
return unserialize(base64_decode( $this->get_mapped_property('field_type_object') ) );
}
/**
* Retorna o objeto field type
*
* @return array || object
*/
function get_field_type(){
return base64_decode($this->get_mapped_property('field_type'));
}
// Setters
/**
* Atribui nome
*
* @param [string] $value
* @return void
*/
function set_name($value) {
return $this->set_mapped_property('name', $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_required( $value ){
return $this->set_mapped_property('required', $value);
}
function set_multiple( $value ){
return $this->set_mapped_property('multiple', $value);
}
function set_cardinality( $value ){
return $this->set_mapped_property('cardinality', $value);
}
function set_collection_key( $value ){
return $this->set_mapped_property('collection_key', $value);
}
function set_mask( $value ){
return $this->set_mapped_property('mask', $value);
}
function set_privacy( $value ){
return $this->set_mapped_property('privacy', $value);
}
function set_default_value( $value ){
return $this->set_mapped_property('default_property', $value);
}
function set_field_type_object(\Tainacan\Field_Types\Field_Type $value){
$this->set_field_type( get_class( $value ) );
return $this->set_mapped_property('field_type_object', base64_encode( serialize($value) ) ); // Encode to avoid backslaches removal
$this->set_mapped_property('name', $value);
}
/**
* este metodo eh privado pois eh setado automaticamente pelo metodo set_field_type_object
* Atribui o tipo de ordenação
*
* @param [string] $value
* @return void
*/
function set_order($value) {
$this->set_mapped_property('order', $value);
}
/**
* Atribui ID do parent
*
* @param [integer] $value
* @return void
*/
function set_parent($value) {
$this->set_mapped_property('parent', $value);
}
/**
* Atribui descrição
*
* @param [string] $value
* @return void
*/
function set_description($value) {
$this->set_mapped_property('description', $value);
}
/**
* Define se é obrigatório
*
* @param [boolean] $value
* @return void
*/
function set_required( $value ){
$this->set_mapped_property('required', $value);
}
/**
* Define se é multiplo
*
* @param [boolean] $value
* @return void
*/
function set_multiple( $value ){
$this->set_mapped_property('multiple', $value);
}
/**
* Define a cardinalidade
*
* @param [string] $value
* @return void
*/
function set_cardinality( $value ){
$this->set_mapped_property('cardinality', $value);
}
/**
* Define se é chave
*
* @param [string] $value
* @return void
*/
function set_collection_key( $value ){
$this->set_mapped_property('collection_key', $value);
}
/**
* Atribui máscara
*
* @param [string] $value
* @return void
*/
function set_mask( $value ){
$this->set_mapped_property('mask', $value);
}
/**
* Define o nível de privacidade
*
* @param [string] $value
* @return void
*/
function set_privacy( $value ){
$this->set_mapped_property('privacy', $value);
}
/**
* Define o valor padrão
*
* @param [string || integer] $value
* @return void
*/
function set_default_value( $value ){
$this->set_mapped_property('default_property', $value);
}
function set_field_type_object(\Tainacan\Field_Types\Field_Type $value){
$this->set_field_type( get_class( $value ) );
$this->set_mapped_property('field_type_object', base64_encode( serialize($value) ) ); // Encode to avoid backslaches removal
}
/**
* Este metodo é privado, porque é utilizado apenas neste contexto pelo @method set_field_type_object()
*
* @param $value
*
*/
private function set_field_type($value){
return $this->set_mapped_property('field_type', base64_encode($value) ) ; // Encode to avoid backslaches removal
$this->set_mapped_property('field_type', base64_encode($value) ) ; // Encode to avoid backslaches removal
}
// helpers
function is_multiple() {
return $this->get_multiple() === 'yes';
$this->get_multiple() === 'yes';
}
function is_collection_key() {
return $this->get_collection_key() === 'yes';
$this->get_collection_key() === 'yes';
}
function is_required() {
return $this->get_required() === 'yes';
$this->get_required() === 'yes';
}
}

View File

@ -55,8 +55,8 @@ class Taxonomy extends \Tainacan\Entity {
$tax_cpts = [];
if (is_array($this->get_collections())){
foreach ($this->get_collections() as $tax_col){
if (is_array($this->fetch())){
foreach ($this->fetch() as $tax_col){
$tax_cpts[] = $tax_col->get_db_identifier();
}
}

View File

@ -9,13 +9,12 @@ if ( ! defined( 'ABSPATH' ) ) {
use \Respect\Validation\Validator as v;
class Collections {
class Collections implements Repository {
const POST_TYPE = 'tainacan-collections';
var $map;
function __construct() {
add_action('init', array(&$this, 'register_post_type'));
add_action('init', array(&$this, 'tainacan_register_post_type'));
}
function get_map() {
@ -52,12 +51,12 @@ class Collections {
];
}
function register_post_type() {
function tainacan_register_post_type() {
$labels = array(
'name' => 'Collections',
'singular_name' => 'Collections',
'add_new' => 'Adicionar Novo',
'add_new_item' =>'Adicionar Collections',
'add_new_item' => 'Adicionar Collections',
'edit_item' => 'Editar',
'new_item' => 'Novo Collections',
'view_item' => 'Visualizar',
@ -85,14 +84,15 @@ class Collections {
'rewrite' => true,
'capability_type' => 'post',
);
register_post_type(self::POST_TYPE, $args);
register_post_type(Entities\Collection::POST_TYPE, $args);
}
function insert(Entities\Collection $collection) {
function insert($collection) {
// validate
if (!$collection->validate())
if (!$collection->validate()){
return $collection->get_errors();
}
// TODO: Throw Warning saying you must validate object before insert()
$map = $this->get_map();
@ -105,7 +105,7 @@ class Collections {
}
// save post and geet its ID
$collection->WP_Post->post_type = self::POST_TYPE;
$collection->WP_Post->post_type = Entities\Collection::POST_TYPE;
$collection->WP_Post->post_status = 'publish';
// TODO verificar se salvou mesmo
@ -131,36 +131,46 @@ class Collections {
}
}
$collection->register_post_type();
$collection->tainacan_register_post_type();
// return a brand new object
return new Entities\Collection($collection->WP_Post);
}
function get_collections($args = array()) {
$args = array_merge([
'post_type' => self::POST_TYPE,
'posts_per_page' => -1,
'post_status' => 'publish',
], $args);
$posts = get_posts($args);
$return = [];
foreach ($posts as $post) {
$return[] = new Entities\Collection($post);
}
// TODO: Pegar coleções registradas via código
return $return;
}
function get_collection_by_id($id) {
return new Entities\Collection($id);
public function update($object){
}
public function delete($object){
}
/**
* Obtém um coleção específica pelo ID ou várias coleções
*
* @param array $object || int $object
* @return Array || Collection
*/
public function fetch($object = []){
if(is_numeric($object)){
return new Entities\Collection($object);
} elseif(is_array($object)) {
$args = array_merge([
'post_type' => Entities\Collection::POST_TYPE,
'posts_per_page' => -1,
'post_status' => 'publish',
], $object);
$posts = get_posts($args);
$collections = [];
foreach ($posts as $post) {
$collections[] = new Entities\Collection($post);
}
// TODO: Pegar coleções registradas via código
return $collections;
}
}
}

View File

@ -40,7 +40,7 @@ class Items {
global $Tainacan_Collections, $Tainacan_Taxonomies;
$collections = $Tainacan_Collections->get_collections();
$collections = $Tainacan_Collections->fetch();
$taxonomies = $Tainacan_Taxonomies->get_taxonomies();
if (!is_array($collections)){
@ -119,11 +119,11 @@ class Items {
global $Tainacan_Collections;
if (empty($collections)) {
$collections = $Tainacan_Collections->get_collections();
$collections = $Tainacan_Collections->fetch();
}
if (is_numeric($collections)){
$collections = $Tainacan_Collections->get_collection_by_id($collection);
$collections = $Tainacan_Collections->fetch($collection);
}
if ($collections instanceof Entities\Collection) {
@ -133,7 +133,7 @@ class Items {
foreach ($collections as $collection) {
if (is_numeric($collection)){
$collection = $Tainacan_Collections->get_collection_by_id($collection);
$collection = $Tainacan_Collections->fetch($collection);
}
if ($collection instanceof Entities\Collection){
$cpt[] = $collection->get_db_identifier();

View File

@ -6,7 +6,7 @@ if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Logs extends \Tainacan\Repository {
class Logs {
const POST_TYPE = 'tainacan-logs';

View File

@ -0,0 +1,14 @@
<?php
namespace Tainacan\Repositories;
interface Repository {
public function insert($object);
public function delete($object);
public function fetch($object);
public function update($object);
}
?>

View File

@ -18,7 +18,7 @@ trait Entity_Collection_Relation {
if (is_numeric($this->get_collection_id())) {
global $Tainacan_Collections;
$this->collection = $Tainacan_Collections->get_collection_by_id($this->get_collection_id());
$this->collection = $Tainacan_Collections->fetch($this->get_collection_id());
return $this->collection;
}

View File

@ -6,22 +6,22 @@ namespace Tainacan\Traits;
trait Entity_Collections_Relation {
function get_collections_ids() {
function fetch_ids() {
return $this->get_mapped_property('collections_ids');
}
function get_collections() {
function fetch() {
if (isset($this->collection) && !empty($this->collection) && is_array($this->collection)){
return $this->collection;
}
if (is_array($this->get_collections_ids()) && !empty(is_array($this->get_collections_ids()))) {
if (is_array($this->fetch_ids()) && !empty(is_array($this->fetch_ids()))) {
global $Tainacan_Collections;
$collections = [];
foreach ($this->get_collections_ids() as $col_id) {
$collections[] = $Tainacan_Collections->get_collection_by_id($col_id);
foreach ($this->fetch_ids() as $col_id) {
$collections[] = $Tainacan_Collections->fetch($col_id);
}
return $collections;

View File

@ -29,7 +29,7 @@ class Collections extends \WP_UnitTestCase {
//
$test = $Tainacan_Collections->get_collection_by_id($col->get_id());
$test = $Tainacan_Collections->fetch($col->get_id());
$this->assertEquals($test->get_name(), 'teste');
@ -51,7 +51,7 @@ class Collections extends \WP_UnitTestCase {
global $Tainacan_Collections;
$col = $Tainacan_Collections->insert($x);
$collection = $Tainacan_Collections->get_collection_by_id($col->get_id());
$collection = $Tainacan_Collections->fetch($col->get_id());