avançando esquema de validação

This commit is contained in:
Leo Germani 2017-11-15 00:04:40 -02:00
parent 602d088b0a
commit 65ab6e8708
15 changed files with 335 additions and 262 deletions

View File

@ -9,7 +9,7 @@ class Tainacan_Entity {
function get_mapped_property($prop) { function get_mapped_property($prop) {
global ${$this->repository}; global ${$this->repository};
$map = ${$this->repository}->map; $map = ${$this->repository}->get_map();
if (isset($this->$prop) && !empty($this->$prop)){ if (isset($this->$prop) && !empty($this->$prop)){
return $this->$prop; return $this->$prop;
@ -22,9 +22,9 @@ class Tainacan_Entity {
$mapped = $map[$prop]['map']; $mapped = $map[$prop]['map'];
if ( $mapped == 'meta') { if ( $mapped == 'meta') {
$return = get_post_meta($this->WP_Post->ID, $prop, true); $return = isset($this->WP_Post->ID) ? get_post_meta($this->WP_Post->ID, $prop, true) : null;
} elseif ( $mapped == 'meta_multi') { } elseif ( $mapped == 'meta_multi') {
$return = get_post_meta($this->WP_Post->ID, $prop, false); $return = isset($this->WP_Post->ID) ? get_post_meta($this->WP_Post->ID, $prop, false) : null;
} elseif ( $mapped == 'termmeta' ){ } elseif ( $mapped == 'termmeta' ){
$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 )) {
@ -54,7 +54,55 @@ class Tainacan_Entity {
} }
function validate() { function validate() {
return true;
global ${$this->repository};
$map = ${$this->repository}->get_map();
$valid = true;
$this->reset_errors();
foreach ($map as $prop => $mapped) {
if (!$this->validate_prop($prop))
$valid = false;
}
return $valid;
}
function validate_prop($prop) {
global ${$this->repository};
$map = ${$this->repository}->get_map();
$mapped = $map[$prop];
$return = true;
if (
isset($mapped['validation']) &&
is_object($mapped['validation']) &&
method_exists($mapped['validation'], 'validate')
) {
$validation = $mapped['validation'];
$prop_value = $this->get_mapped_property($prop);
if (is_array($prop_value)) {
foreach ($prop_value as $val) {
if (!$validation->validate($val)) {
//
$this->add_error('invalid', $prop . ' is invalid');
$return = false;
}
}
} else {
if (!$validation->validate($prop_value)) {
//
$this->add_error('invalid', $prop . ' is invalid');
$return = false;
}
}
}
return $return;
} }
function get_errors() { function get_errors() {

View File

@ -69,7 +69,7 @@ class Tainacan_Collection extends Tainacan_Entity {
// Getters // Getters
function get_id() { function get_id() {
return $this->get_mapped_property('ID'); return $this->get_mapped_property('id');
} }
function get_name() { function get_name() {
return $this->get_mapped_property('name'); return $this->get_mapped_property('name');

View File

@ -27,7 +27,7 @@ class Tainacan_Filter extends Tainacan_Entity {
// Getters // Getters
function get_id() { function get_id() {
return $this->get_mapped_property('ID'); return $this->get_mapped_property('id');
} }
function get_name() { function get_name() {

View File

@ -27,7 +27,7 @@ class Tainacan_Item extends Tainacan_Entity {
// Getters // Getters
function get_id() { function get_id() {
return $this->get_mapped_property('ID'); return $this->get_mapped_property('id');
} }
function get_title() { function get_title() {
return $this->get_mapped_property('title'); return $this->get_mapped_property('title');

View File

@ -30,7 +30,7 @@ class Tainacan_Log extends Tainacan_Entity {
// Getters // Getters
// //
function get_id() { function get_id() {
return $this->get_mapped_property('ID'); return $this->get_mapped_property('id');
} }
function get_title() { function get_title() {
return $this->get_mapped_property('title'); return $this->get_mapped_property('title');

View File

@ -27,7 +27,7 @@ class Tainacan_Metadata extends Tainacan_Entity {
// Getters // Getters
function get_id() { function get_id() {
return $this->get_mapped_property('ID'); return $this->get_mapped_property('id');
} }

View File

@ -73,7 +73,7 @@ class Tainacan_Taxonomy extends Tainacan_Entity {
// Getters // Getters
function get_id() { function get_id() {
return $this->get_mapped_property('ID'); return $this->get_mapped_property('id');
} }
function get_name() { function get_name() {

View File

@ -12,11 +12,13 @@ class Tainacan_Collections {
function __construct() { function __construct() {
add_action('init', array(&$this, 'register_post_type')); add_action('init', array(&$this, 'register_post_type'));
}
$this->map = [
'ID' => [ function get_map() {
return [
'id' => [
'map' => 'ID', 'map' => 'ID',
'validation' => v::numeric(), //'validation' => v::numeric(),
], ],
'name' => [ 'name' => [
'map' => 'post_title', 'map' => 'post_title',
@ -24,22 +26,23 @@ class Tainacan_Collections {
], ],
'order' => [ 'order' => [
'map' => 'menu_order', 'map' => 'menu_order',
'validation' => v::stringType(), //'validation' => v::stringType(),
], ],
'parent' => [ 'parent' => [
'map' => 'parent', 'map' => 'parent',
'validation' => v::stringType(), //'validation' => v::stringType(),
], ],
'description' => [ 'description' => [
'map' => 'post_content', 'map' => 'post_content',
'validation' => v::stringType(), //'validation' => v::stringType(),
], ],
'slug' => [ 'slug' => [
'map' => 'post_name', 'map' => 'post_name',
'validation' => v::stringType(), //'validation' => v::stringType(),
], ],
'itens_per_page' => [ 'itens_per_page' => [
'map' => 'meta', 'map' => 'meta',
'default' => 10,
'validation' => v::intVal()->positive(), 'validation' => v::intVal()->positive(),
], ],
]; ];
@ -82,25 +85,17 @@ class Tainacan_Collections {
} }
function insert(Tainacan_Collection $collection) { function insert(Tainacan_Collection $collection) {
// validate
if (!$collection->validate())
return $collection->get_errors();
$map = $this->get_map();
// First iterate through the native post properties // First iterate through the native post properties
$map = $this->map;
$count = 0;
foreach ($map as $prop => $mapped) { foreach ($map as $prop => $mapped) {
if ($mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi') { if ($mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi') {
$prop_value = $collection->get_mapped_property($prop); $collection->WP_Post->{$mapped['map']} = $collection->get_mapped_property($prop);
$validation = $mapped['validation'];
if($validation->validate($prop_value)){
var_dump($prop_value);
var_dump($validation->validate($prop_value));
$collection->WP_Post->{$mapped['map']} = $prop_value;
} else {
$count++;
//throw new Exception("Prop ". $prop ." with this value ". $prop_value ." is invalid", 1);
}
} }
} }

View File

@ -7,48 +7,50 @@ class Tainacan_Filters {
const POST_TYPE = 'tainacan-filters'; const POST_TYPE = 'tainacan-filters';
var $map = [
'ID' => [
'map' => 'ID',
'validation' => ''
],
'name' => [
'map' => 'post_title',
'validation' => ''
],
'order' => [
'map' => 'menu_order',
'validation' => ''
],
'description' => [
'map' => 'post_content',
'validation' => ''
],
'filter_type_object' => [
'map' => 'meta',
'validation' => ''
],
'filter_type' => [
'map' => 'meta',
'validation' => ''
],
'collection_id' => [
'map' => 'meta',
'validation' => ''
],
'color' => [
'map' => 'meta',
'validation' => ''
],
'metadata' => [
'map' => 'meta',
'validation' => ''
],
];
function __construct(){ function __construct(){
add_action('init', array(&$this, 'register_post_type')); add_action('init', array(&$this, 'register_post_type'));
} }
function get_map() {
return [
'id' => [
'map' => 'ID',
//'validation' => ''
],
'name' => [
'map' => 'post_title',
'validation' => ''
],
'order' => [
'map' => 'menu_order',
'validation' => ''
],
'description' => [
'map' => 'post_content',
'validation' => ''
],
'filter_type_object' => [
'map' => 'meta',
'validation' => ''
],
'filter_type' => [
'map' => 'meta',
'validation' => ''
],
'collection_id' => [
'map' => 'meta',
'validation' => ''
],
'color' => [
'map' => 'meta',
'validation' => ''
],
'metadata' => [
'map' => 'meta',
'validation' => ''
],
];
}
function register_post_type(){ function register_post_type(){
$labels = array( $labels = array(
@ -93,7 +95,7 @@ class Tainacan_Filters {
*/ */
function insert( Tainacan_Filter $metadata ) { function insert( Tainacan_Filter $metadata ) {
// First iterate through the native post properties // First iterate through the native post properties
$map = $this->map; $map = $this->get_map();
foreach ($map as $prop => $mapped) { foreach ($map as $prop => $mapped) {
if ($mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi') { if ($mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi') {
$metadata->WP_Post->{$mapped['map']} = $metadata->get_mapped_property($prop); $metadata->WP_Post->{$mapped['map']} = $metadata->get_mapped_property($prop);

View File

@ -5,31 +5,33 @@ if ( ! defined( 'ABSPATH' ) ) {
class Tainacan_Items { class Tainacan_Items {
var $map = [
'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() { function __construct() {
add_action('init', array(&$this, 'register_post_types')); add_action('init', array(&$this, 'register_post_types'));
} }
function get_map() {
return [
'id' => [
'map' => 'ID',
//'validation' => ''
],
'title' => [
'map' => 'post_title',
'validation' => ''
],
'description' => [
'map' => 'post_content',
'validation' => ''
],
'collection_id' => [
'map' => 'meta',
'validation' => ''
],
//'collection' => 'relation...',
// metadata .. metadata...
];
}
function register_post_types() { function register_post_types() {
global $Tainacan_Collections, $Tainacan_Taxonomies; global $Tainacan_Collections, $Tainacan_Taxonomies;
@ -55,7 +57,7 @@ class Tainacan_Items {
} }
function insert(Tainacan_Item $item) { function insert(Tainacan_Item $item) {
$map = $this->map; $map = $this->get_map();
// get collection to determine post type // get collection to determine post type
$collection = $item->get_collection(); $collection = $item->get_collection();
@ -166,7 +168,7 @@ class Tainacan_Items {
// other item properties, present in the "map" // other item properties, present in the "map"
function query($args) { function query($args) {
$map = $this->map; $map = $this->get_map();
$wp_query_exceptions = [ $wp_query_exceptions = [
'ID' => 'p', 'ID' => 'p',
'post_title' => 'title' 'post_title' => 'title'

View File

@ -9,45 +9,47 @@ class Tainacan_Logs extends Tainacan_Repository {
const POST_TYPE = 'tainacan-logs'; const POST_TYPE = 'tainacan-logs';
var $map = [
'ID' => [
'map' => 'ID',
'validation' => ''
],
'title' => [
'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' => ''
],
'user_id' => [
'map' => 'post_author',
'validation' => ''
],
];
function __construct() { function __construct() {
add_action('init', array(&$this, 'register_post_type')); add_action('init', array(&$this, 'register_post_type'));
} }
function get_map() {
return [
'id' => [
'map' => 'ID',
//'validation' => ''
],
'title' => [
'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' => ''
],
'user_id' => [
'map' => 'post_author',
'validation' => ''
],
];
}
function register_post_type() { function register_post_type() {
$labels = array( $labels = array(
'name' => 'logs', 'name' => 'logs',
@ -86,7 +88,7 @@ class Tainacan_Logs extends Tainacan_Repository {
function insert(Tainacan_Log $log) { function insert(Tainacan_Log $log) {
// First iterate through the native post properties // First iterate through the native post properties
$map = $this->map; $map = $this->get_map();
foreach ($map as $prop => $mapped) { foreach ($map as $prop => $mapped) {
if ($mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi') { if ($mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi') {
$log->WP_Post->{$mapped['map']} = $log->get_mapped_property($prop); $log->WP_Post->{$mapped['map']} = $log->get_mapped_property($prop);

View File

@ -10,76 +10,78 @@ class Tainacan_Metadatas {
const POST_TYPE = 'tainacan-metadata'; const POST_TYPE = 'tainacan-metadata';
var $map = [
'ID' => [
'map' => 'ID',
'validation' => ''
],
'name' => [
'map' => 'post_title',
'validation' => ''
],
'order' => [
'map' => 'menu_order',
'validation' => ''
],
'parent' => [
'map' => 'parent',
'validation' => ''
],
'description' => [
'map' => 'post_content',
'validation' => ''
],
'field_type' => [
'map' => 'meta',
'validation' => ''
],
'required' => [
'map' => 'meta',
'validation' => '', // yes or no
'default' => 'no'
],
'collection_key' => [
'map' => 'meta',
'validation' => '', // yes or no. it cant be key if its multiple
'default' => 'no'
],
'multiple' => [
'map' => 'meta',
'validation' => '', // yes or no. It cant be multiple if its collection_key
'default' => 'no'
],
'cardinality' => [
'map' => 'meta',
'validation' => '',
'default' => 1
],
'privacy' => [
'map' => 'meta',
'validation' => ''
],
'mask' => [
'map' => 'meta',
'validation' => ''
],
'default_value' => [
'map' => 'meta',
'validation' => ''
],
'field_type_object' => [
'map' => 'meta',
'validation' => ''
],
'collection_id' => [
'map' => 'meta',
'validation' => ''
],
];
function __construct() { function __construct() {
add_action('init', array(&$this, 'register_post_type')); add_action('init', array(&$this, 'register_post_type'));
} }
function get_map() {
return [
'id' => [
'map' => 'ID',
//'validation' => ''
],
'name' => [
'map' => 'post_title',
'validation' => ''
],
'order' => [
'map' => 'menu_order',
'validation' => ''
],
'parent' => [
'map' => 'parent',
'validation' => ''
],
'description' => [
'map' => 'post_content',
'validation' => ''
],
'field_type' => [
'map' => 'meta',
'validation' => ''
],
'required' => [
'map' => 'meta',
'validation' => '', // yes or no
'default' => 'no'
],
'collection_key' => [
'map' => 'meta',
'validation' => '', // yes or no. it cant be key if its multiple
'default' => 'no'
],
'multiple' => [
'map' => 'meta',
'validation' => '', // yes or no. It cant be multiple if its collection_key
'default' => 'no'
],
'cardinality' => [
'map' => 'meta',
'validation' => '',
'default' => 1
],
'privacy' => [
'map' => 'meta',
'validation' => ''
],
'mask' => [
'map' => 'meta',
'validation' => ''
],
'default_value' => [
'map' => 'meta',
'validation' => ''
],
'field_type_object' => [
'map' => 'meta',
'validation' => ''
],
'collection_id' => [
'map' => 'meta',
'validation' => ''
],
];
}
function register_post_type() { function register_post_type() {
$labels = array( $labels = array(
@ -124,7 +126,7 @@ class Tainacan_Metadatas {
*/ */
function insert( Tainacan_Metadata $metadata ) { function insert( Tainacan_Metadata $metadata ) {
// First iterate through the native post properties // First iterate through the native post properties
$map = $this->map; $map = $this->get_map();
foreach ($map as $prop => $mapped) { foreach ($map as $prop => $mapped) {
if ($mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi') { if ($mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi') {
$metadata->WP_Post->{$mapped['map']} = $metadata->get_mapped_property($prop); $metadata->WP_Post->{$mapped['map']} = $metadata->get_mapped_property($prop);

View File

@ -10,41 +10,42 @@ class Tainacan_Taxonomies {
const POST_TYPE = 'tainacan-taxonomies'; const POST_TYPE = 'tainacan-taxonomies';
var $map = [
'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' => ''
],
];
function __construct() { function __construct() {
add_action('init', array(&$this, 'register_post_type')); add_action('init', array(&$this, 'register_post_type'));
} }
function get_map() {
return [
'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' => ''
],
];
}
function register_post_type() { function register_post_type() {
$labels = array( $labels = array(
@ -109,7 +110,7 @@ class Tainacan_Taxonomies {
*/ */
function insert( Tainacan_Taxonomy $taxonomy ) { function insert( Tainacan_Taxonomy $taxonomy ) {
// First iterate through the native post properties // First iterate through the native post properties
$map = $this->map; $map = $this->get_map();
foreach ($map as $prop => $mapped) { foreach ($map as $prop => $mapped) {
if ($mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi') { if ($mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi') {
$taxonomy->WP_Post->{$mapped['map']} = $taxonomy->get_mapped_property($prop); $taxonomy->WP_Post->{$mapped['map']} = $taxonomy->get_mapped_property($prop);

View File

@ -8,36 +8,38 @@ exit;
*/ */
class Tainacan_Terms { class Tainacan_Terms {
var $map = [ function get_map() {
'term_id' => [ return [
'map' => 'term_id', 'term_id' => [
'validation' => '' 'map' => 'term_id',
], //'validation' => ''
'name' => [ ],
'map' => 'name', 'name' => [
'validation' => '' 'map' => 'name',
], 'validation' => ''
'parent' => [ ],
'map' => 'parent', 'parent' => [
'validation' => '' 'map' => 'parent',
], 'validation' => ''
'description' => [ ],
'map' => 'description', 'description' => [
'validation' => '' 'map' => 'description',
], 'validation' => ''
'taxonomy' => [ ],
'map' => 'taxonomy', 'taxonomy' => [
'validation' => '' 'map' => 'taxonomy',
], 'validation' => ''
'user' => [ ],
'map' => 'termmeta', 'user' => [
'validation' => '' '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->get_map();
foreach ($map as $prop => $mapped) { foreach ($map as $prop => $mapped) {
if ($mapped['map'] != 'termmeta') { if ($mapped['map'] != 'termmeta') {
$term->WP_Term->{$mapped['map']} = $term->get_mapped_property($prop); $term->WP_Term->{$mapped['map']} = $term->get_mapped_property($prop);

View File

@ -67,5 +67,24 @@ class Test_Collections extends WP_UnitTestCase {
$this->assertEquals($item->get_description(), 'adasdasdsa'); $this->assertEquals($item->get_description(), 'adasdasdsa');
$this->assertEquals($item->get_collection_id(), $collection->get_id()); $this->assertEquals($item->get_collection_id(), $collection->get_id());
}
function test_validation() {
$x = new Tainacan_Collection();
$x->set_name('teste');
$x->set_description('adasdasdsa');
$x->set_itens_per_page('blah');
$this->assertFalse($x->validate());
$this->assertTrue(sizeof($x->get_errors()) > 0);
$x->set_itens_per_page(15);
$this->assertTrue($x->validate());
$this->assertTrue(empty($x->get_errors()));
} }
} }