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))
return null;
$mapped = $map[$prop];
$mapped = $map[$prop]['map'];
if ( $mapped == 'meta') {
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);
}elseif ( isset( $this->WP_Post )) {
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;
}
@ -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';
var $map = [
'ID' => 'ID',
'name' => 'post_title',
'order' => 'menu_order',
'parent' => 'parent',
'description' => 'post_content',
'slug' => 'post_name',
'itens_per_page' => 'meta'
'ID' => [
'map' => 'ID',
'validation' => ''
],
'name' => [
'map' => 'post_title',
'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() {
@ -63,8 +84,8 @@ class TainacanCollections {
// First iterate through the native post properties
$map = $this->map;
foreach ($map as $prop => $mapped) {
if ($mapped != 'meta') {
$collection->WP_Post->$mapped = $collection->get_mapped_property($prop);
if ($mapped['map'] != 'meta') {
$collection->WP_Post->{$mapped['map']} = $collection->get_mapped_property($prop);
}
}
@ -80,9 +101,9 @@ class TainacanCollections {
// Now run through properties stored as postmeta
foreach ($map as $prop => $mapped) {
if ($mapped == 'meta') {
if ($mapped['map'] == 'meta') {
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);
delete_post_meta($id, $prop);
if (is_array($values))

View File

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

View File

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

View File

@ -11,13 +11,34 @@ class Tainacan_Taxonomies {
const POST_TYPE = 'tainacan-taxonomies';
var $map = [
'ID' => 'ID',
'name' => 'post_title',
'parent' => 'parent',
'description' => 'post_content',
'slug' => 'post_name',
'allow_insert' => 'meta',
'collections_ids' => 'meta_multi',
'ID' => [
'map' => 'ID',
'validation' => ''
],
'name' => [
'map' => 'post_title',
'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
$map = $this->map;
foreach ($map as $prop => $mapped) {
if ($mapped != 'meta') {
$taxonomy->WP_Post->$mapped = $taxonomy->get_mapped_property($prop);
if ($mapped['map'] != 'meta') {
$taxonomy->WP_Post->{$mapped['map']} = $taxonomy->get_mapped_property($prop);
}
}
@ -104,9 +125,9 @@ class Tainacan_Taxonomies {
// Now run through properties stored as postmeta
foreach ($map as $prop => $mapped) {
if ($mapped == 'meta') {
if ($mapped['map'] == 'meta') {
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);
delete_post_meta($id, $prop);
if (is_array($values))

View File

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

View File

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