pequeno refactor do mapeamento

This commit is contained in:
Leo Germani 2017-11-12 11:39:46 -02:00
parent 15f6821293
commit 5f8e2b1a8e
7 changed files with 199 additions and 60 deletions

View File

@ -18,7 +18,7 @@ class Entity {
if (!array_key_exists($prop, $map)) if (!array_key_exists($prop, $map))
return null; return null;
$mapped = $map[$prop]; $mapped = $map[$prop]['map'];
if ( $mapped == 'meta') { if ( $mapped == 'meta') {
return get_post_meta($this->WP_Post->ID, $prop, true); return get_post_meta($this->WP_Post->ID, $prop, true);
@ -28,7 +28,7 @@ class Entity {
return get_term_meta($this->WP_Term->term_id, $prop, true); return get_term_meta($this->WP_Term->term_id, $prop, true);
}elseif ( isset( $this->WP_Post )) { }elseif ( isset( $this->WP_Post )) {
return isset($this->WP_Post->$mapped) ? $this->WP_Post->$mapped : null; return isset($this->WP_Post->$mapped) ? $this->WP_Post->$mapped : null;
} else{ } elseif ( isset( $this->WP_Term )) {
return isset($this->WP_Term->$mapped) ? $this->WP_Term->$mapped : null; return isset($this->WP_Term->$mapped) ? $this->WP_Term->$mapped : null;
} }
@ -53,5 +53,12 @@ class Entity {
} }
function validate($value) {
return true;
}
function get_validation_errors() {
return [];
}
} }

View File

@ -10,13 +10,34 @@ class TainacanCollections {
const POST_TYPE = 'tainacan-collections'; const POST_TYPE = 'tainacan-collections';
var $map = [ var $map = [
'ID' => 'ID', 'ID' => [
'name' => 'post_title', 'map' => 'ID',
'order' => 'menu_order', 'validation' => ''
'parent' => 'parent', ],
'description' => 'post_content', 'name' => [
'slug' => 'post_name', 'map' => 'post_title',
'itens_per_page' => 'meta' 'validation' => ''
],
'order' => [
'map' => 'menu_order',
'validation' => ''
],
'parent' => [
'map' => 'parent',
'validation' => ''
],
'description' => [
'map' => 'post_content',
'validation' => ''
],
'slug' => [
'map' => 'post_name',
'validation' => ''
],
'itens_per_page' => [
'map' => 'meta',
'validation' => ''
],
]; ];
function __construct() { function __construct() {
@ -63,8 +84,8 @@ class TainacanCollections {
// First iterate through the native post properties // First iterate through the native post properties
$map = $this->map; $map = $this->map;
foreach ($map as $prop => $mapped) { foreach ($map as $prop => $mapped) {
if ($mapped != 'meta') { if ($mapped['map'] != 'meta') {
$collection->WP_Post->$mapped = $collection->get_mapped_property($prop); $collection->WP_Post->{$mapped['map']} = $collection->get_mapped_property($prop);
} }
} }
@ -80,9 +101,9 @@ class TainacanCollections {
// Now run through properties stored as postmeta // Now run through properties stored as postmeta
foreach ($map as $prop => $mapped) { foreach ($map as $prop => $mapped) {
if ($mapped == 'meta') { if ($mapped['map'] == 'meta') {
update_post_meta($id, $prop, $collection->get_mapped_property($prop)); update_post_meta($id, $prop, $collection->get_mapped_property($prop));
} elseif ($mapped == 'meta_multi') { } elseif ($mapped['map'] == 'meta_multi') {
$values = $collection->get_mapped_property($prop); $values = $collection->get_mapped_property($prop);
delete_post_meta($id, $prop); delete_post_meta($id, $prop);
if (is_array($values)) if (is_array($values))

View File

@ -8,11 +8,24 @@ if ( ! defined( 'ABSPATH' ) ) {
class TainacanItems { class TainacanItems {
var $map = [ var $map = [
'ID' => 'ID', 'ID' => [
'title' => 'post_title', 'map' => 'ID',
'description' => 'post_content', 'validation' => ''
'collection_id' => 'meta', ],
//'collection' => 'relation...' 'title' => [
'map' => 'post_title',
'validation' => ''
],
'description' => [
'map' => 'post_content',
'validation' => ''
],
'collection_id' => [
'map' => 'meta',
'validation' => ''
],
//'collection' => 'relation...',
// metadata .. metadata...
]; ];
function __construct() { function __construct() {
@ -66,8 +79,8 @@ class TainacanItems {
// iterate through the native post properties // iterate through the native post properties
foreach ($map as $prop => $mapped) { foreach ($map as $prop => $mapped) {
if ($mapped != 'meta') { if ($mapped['map'] != 'meta') {
$item->WP_Post->$mapped = $item->get_mapped_property($prop); $item->WP_Post->{$mapped['map']} = $item->get_mapped_property($prop);
} }
} }
@ -78,9 +91,9 @@ class TainacanItems {
// Now run through properties stored as postmeta // Now run through properties stored as postmeta
foreach ($map as $prop => $mapped) { foreach ($map as $prop => $mapped) {
if ($mapped == 'meta') { if ($mapped['map'] == 'meta') {
update_post_meta($id, $prop, $item->get_mapped_property($prop)); update_post_meta($id, $prop, $item->get_mapped_property($prop));
} elseif ($mapped == 'meta_multi') { } elseif ($mapped['map'] == 'meta_multi') {
$values = $item->get_mapped_property($prop); $values = $item->get_mapped_property($prop);
delete_post_meta($id, $prop); delete_post_meta($id, $prop);
if (is_array($values)) if (is_array($values))

View File

@ -11,19 +11,58 @@ class Tainacan_Metadatas {
const POST_TYPE = 'tainacan-metadata'; const POST_TYPE = 'tainacan-metadata';
var $map = [ var $map = [
'ID' => 'ID', 'ID' => [
'name' => 'post_title', 'map' => 'ID',
'order' => 'menu_order', 'validation' => ''
'parent' => 'parent', ],
'description' => 'post_content', 'name' => [
'type' => 'meta', 'map' => 'post_title',
'required' => 'meta', 'validation' => ''
'cardinality' => 'meta', ],
'privacy' => 'meta', 'order' => [
'mask' => 'meta', 'map' => 'menu_order',
'default_value' => 'meta', 'validation' => ''
'option' => 'meta', ],
'collection_id' => 'meta', 'parent' => [
'map' => 'parent',
'validation' => ''
],
'description' => [
'map' => 'post_content',
'validation' => ''
],
'type' => [
'map' => 'meta',
'validation' => ''
],
'required' => [
'map' => 'meta',
'validation' => ''
],
'cardinality' => [
'map' => 'meta',
'validation' => ''
],
'privacy' => [
'map' => 'meta',
'validation' => ''
],
'mask' => [
'map' => 'meta',
'validation' => ''
],
'default_value' => [
'map' => 'meta',
'validation' => ''
],
'option' => [
'map' => 'meta',
'validation' => ''
],
'collection_id' => [
'map' => 'meta',
'validation' => ''
],
]; ];
function __construct() { function __construct() {
@ -75,8 +114,8 @@ class Tainacan_Metadatas {
// First iterate through the native post properties // First iterate through the native post properties
$map = $this->map; $map = $this->map;
foreach ($map as $prop => $mapped) { foreach ($map as $prop => $mapped) {
if ($mapped != 'meta') { if ($mapped['map'] != 'meta') {
$metadata->WP_Post->$mapped = $metadata->get_mapped_property($prop); $metadata->WP_Post->{$mapped['map']} = $metadata->get_mapped_property($prop);
} }
} }
@ -88,9 +127,9 @@ class Tainacan_Metadatas {
// Now run through properties stored as postmeta // Now run through properties stored as postmeta
foreach ($map as $prop => $mapped) { foreach ($map as $prop => $mapped) {
if ($mapped == 'meta') { if ($mapped['map'] == 'meta') {
update_post_meta($id, $prop, $metadata->get_mapped_property($prop)); update_post_meta($id, $prop, $metadata->get_mapped_property($prop));
} elseif ($mapped == 'meta_multi') { } elseif ($mapped['map'] == 'meta_multi') {
$values = $metadata->get_mapped_property($prop); $values = $metadata->get_mapped_property($prop);
delete_post_meta($id, $prop); delete_post_meta($id, $prop);
if (is_array($values)) if (is_array($values))
@ -108,13 +147,16 @@ class Tainacan_Metadatas {
* @return array * @return array
*/ */
function get_metadata_by_collection( $collection, $args = array()) { function get_metadata_by_collection( $collection, $args = array()) {
// TODO: get metadata from parent collections
$collection_id = ( is_object( $collection ) ) ? $collection->get_id() : $collection; $collection_id = ( is_object( $collection ) ) ? $collection->get_id() : $collection;
$args = array_merge([ $args = array_merge([
'post_type' => self::POST_TYPE, 'post_type' => self::POST_TYPE,
'posts_per_page' => -1, 'posts_per_page' => -1,
'post_status' => 'publish', 'post_status' => 'publish',
'meta_key' => 'collection', 'meta_key' => 'collection_id',
'meta_value' => $collection_id 'meta_value' => $collection_id
], $args); ], $args);

View File

@ -11,13 +11,34 @@ class Tainacan_Taxonomies {
const POST_TYPE = 'tainacan-taxonomies'; const POST_TYPE = 'tainacan-taxonomies';
var $map = [ var $map = [
'ID' => 'ID', 'ID' => [
'name' => 'post_title', 'map' => 'ID',
'parent' => 'parent', 'validation' => ''
'description' => 'post_content', ],
'slug' => 'post_name', 'name' => [
'allow_insert' => 'meta', 'map' => 'post_title',
'collections_ids' => 'meta_multi', 'validation' => ''
],
'parent' => [
'map' => 'parent',
'validation' => ''
],
'description' => [
'map' => 'post_content',
'validation' => ''
],
'slug' => [
'map' => 'post_name',
'validation' => ''
],
'allow_insert' => [
'map' => 'meta',
'validation' => ''
],
'collections_ids' => [
'map' => 'meta_multi',
'validation' => ''
],
]; ];
@ -91,8 +112,8 @@ class Tainacan_Taxonomies {
// First iterate through the native post properties // First iterate through the native post properties
$map = $this->map; $map = $this->map;
foreach ($map as $prop => $mapped) { foreach ($map as $prop => $mapped) {
if ($mapped != 'meta') { if ($mapped['map'] != 'meta') {
$taxonomy->WP_Post->$mapped = $taxonomy->get_mapped_property($prop); $taxonomy->WP_Post->{$mapped['map']} = $taxonomy->get_mapped_property($prop);
} }
} }
@ -104,9 +125,9 @@ class Tainacan_Taxonomies {
// Now run through properties stored as postmeta // Now run through properties stored as postmeta
foreach ($map as $prop => $mapped) { foreach ($map as $prop => $mapped) {
if ($mapped == 'meta') { if ($mapped['map'] == 'meta') {
update_post_meta($id, $prop, $taxonomy->get_mapped_property($prop)); update_post_meta($id, $prop, $taxonomy->get_mapped_property($prop));
} elseif ($mapped == 'meta_multi') { } elseif ($mapped['map'] == 'meta_multi') {
$values = $taxonomy->get_mapped_property($prop); $values = $taxonomy->get_mapped_property($prop);
delete_post_meta($id, $prop); delete_post_meta($id, $prop);
if (is_array($values)) if (is_array($values))

View File

@ -9,20 +9,38 @@ exit;
class Tainacan_Terms { class Tainacan_Terms {
var $map = [ var $map = [
'term_id' => 'term_id', 'term_id' => [
'name' => 'name', 'map' => 'term_id',
'parent' => 'parent', 'validation' => ''
'description' => 'description', ],
'taxonomy' => 'taxonomy', 'name' => [
'user' => 'termmeta', 'map' => 'name',
'validation' => ''
],
'parent' => [
'map' => 'parent',
'validation' => ''
],
'description' => [
'map' => 'description',
'validation' => ''
],
'taxonomy' => [
'map' => 'taxonomy',
'validation' => ''
],
'user' => [
'map' => 'termmeta',
'validation' => ''
],
]; ];
function insert( Tainacan_Term $term ){ function insert( Tainacan_Term $term ){
// First iterate through the native post properties // First iterate through the native post properties
$map = $this->map; $map = $this->map;
foreach ($map as $prop => $mapped) { foreach ($map as $prop => $mapped) {
if ($mapped != 'termmeta') { if ($mapped['map'] != 'termmeta') {
$term->WP_Term->$mapped = $term->get_mapped_property($prop); $term->WP_Term->{$mapped['map']} = $term->get_mapped_property($prop);
} }
} }
@ -34,7 +52,7 @@ class Tainacan_Terms {
// Now run through properties stored as postmeta // Now run through properties stored as postmeta
foreach ($map as $prop => $mapped) { foreach ($map as $prop => $mapped) {
if ($mapped == 'termmeta') { if ($mapped['map'] == 'termmeta') {
update_term_meta($term_inserted['term_id'], $prop, $term->get_mapped_property($prop)); update_term_meta($term_inserted['term_id'], $prop, $term->get_mapped_property($prop));
} }
} }

View File

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