From 65ab6e8708db22495715386baa5e8465a010a2b4 Mon Sep 17 00:00:00 2001 From: Leo Germani Date: Wed, 15 Nov 2017 00:04:40 -0200 Subject: [PATCH] =?UTF-8?q?avan=C3=A7ando=20esquema=20de=20valida=C3=A7?= =?UTF-8?q?=C3=A3o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/classes/class-tainacan-entity.php | 56 ++++++- .../entities/class-tainacan-collection.php | 2 +- .../entities/class-tainacan-filter.php | 2 +- src/classes/entities/class-tainacan-item.php | 2 +- src/classes/entities/class-tainacan-log.php | 2 +- .../entities/class-tainacan-metadata.php | 2 +- .../entities/class-tainacan-taxonomy.php | 2 +- .../class-tainacan-collections.php | 43 +++--- .../repositories/class-tainacan-filters.php | 82 ++++++----- .../repositories/class-tainacan-items.php | 48 +++--- .../repositories/class-tainacan-logs.php | 74 +++++----- .../repositories/class-tainacan-metadatas.php | 138 +++++++++--------- .../class-tainacan-taxonomies.php | 67 ++++----- .../repositories/class-tainacan-terms.php | 58 ++++---- .../{test-sample.php => test-collections.php} | 19 +++ 15 files changed, 335 insertions(+), 262 deletions(-) rename tests/{test-sample.php => test-collections.php} (78%) diff --git a/src/classes/class-tainacan-entity.php b/src/classes/class-tainacan-entity.php index 095c01046..03c5924f4 100644 --- a/src/classes/class-tainacan-entity.php +++ b/src/classes/class-tainacan-entity.php @@ -9,7 +9,7 @@ class Tainacan_Entity { function get_mapped_property($prop) { global ${$this->repository}; - $map = ${$this->repository}->map; + $map = ${$this->repository}->get_map(); if (isset($this->$prop) && !empty($this->$prop)){ return $this->$prop; @@ -22,9 +22,9 @@ class Tainacan_Entity { $mapped = $map[$prop]['map']; 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') { - $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' ){ $return = get_term_meta($this->WP_Term->term_id, $prop, true); } elseif ( isset( $this->WP_Post )) { @@ -54,7 +54,55 @@ class Tainacan_Entity { } 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() { diff --git a/src/classes/entities/class-tainacan-collection.php b/src/classes/entities/class-tainacan-collection.php index 18e33d750..e18196862 100644 --- a/src/classes/entities/class-tainacan-collection.php +++ b/src/classes/entities/class-tainacan-collection.php @@ -69,7 +69,7 @@ class Tainacan_Collection extends Tainacan_Entity { // Getters function get_id() { - return $this->get_mapped_property('ID'); + return $this->get_mapped_property('id'); } function get_name() { return $this->get_mapped_property('name'); diff --git a/src/classes/entities/class-tainacan-filter.php b/src/classes/entities/class-tainacan-filter.php index 6eaf94ee6..cf59cb582 100644 --- a/src/classes/entities/class-tainacan-filter.php +++ b/src/classes/entities/class-tainacan-filter.php @@ -27,7 +27,7 @@ class Tainacan_Filter extends Tainacan_Entity { // Getters function get_id() { - return $this->get_mapped_property('ID'); + return $this->get_mapped_property('id'); } function get_name() { diff --git a/src/classes/entities/class-tainacan-item.php b/src/classes/entities/class-tainacan-item.php index 0962861b4..036c8e989 100644 --- a/src/classes/entities/class-tainacan-item.php +++ b/src/classes/entities/class-tainacan-item.php @@ -27,7 +27,7 @@ class Tainacan_Item extends Tainacan_Entity { // Getters function get_id() { - return $this->get_mapped_property('ID'); + return $this->get_mapped_property('id'); } function get_title() { return $this->get_mapped_property('title'); diff --git a/src/classes/entities/class-tainacan-log.php b/src/classes/entities/class-tainacan-log.php index fd8af0e45..19e7886b9 100644 --- a/src/classes/entities/class-tainacan-log.php +++ b/src/classes/entities/class-tainacan-log.php @@ -30,7 +30,7 @@ class Tainacan_Log extends Tainacan_Entity { // Getters // function get_id() { - return $this->get_mapped_property('ID'); + return $this->get_mapped_property('id'); } function get_title() { return $this->get_mapped_property('title'); diff --git a/src/classes/entities/class-tainacan-metadata.php b/src/classes/entities/class-tainacan-metadata.php index 60a612924..6e229748f 100644 --- a/src/classes/entities/class-tainacan-metadata.php +++ b/src/classes/entities/class-tainacan-metadata.php @@ -27,7 +27,7 @@ class Tainacan_Metadata extends Tainacan_Entity { // Getters function get_id() { - return $this->get_mapped_property('ID'); + return $this->get_mapped_property('id'); } diff --git a/src/classes/entities/class-tainacan-taxonomy.php b/src/classes/entities/class-tainacan-taxonomy.php index 5ac34f46a..06c027782 100644 --- a/src/classes/entities/class-tainacan-taxonomy.php +++ b/src/classes/entities/class-tainacan-taxonomy.php @@ -73,7 +73,7 @@ class Tainacan_Taxonomy extends Tainacan_Entity { // Getters function get_id() { - return $this->get_mapped_property('ID'); + return $this->get_mapped_property('id'); } function get_name() { diff --git a/src/classes/repositories/class-tainacan-collections.php b/src/classes/repositories/class-tainacan-collections.php index b1cc51f24..2dba369c1 100644 --- a/src/classes/repositories/class-tainacan-collections.php +++ b/src/classes/repositories/class-tainacan-collections.php @@ -12,11 +12,13 @@ class Tainacan_Collections { function __construct() { add_action('init', array(&$this, 'register_post_type')); - - $this->map = [ - 'ID' => [ + } + + function get_map() { + return [ + 'id' => [ 'map' => 'ID', - 'validation' => v::numeric(), + //'validation' => v::numeric(), ], 'name' => [ 'map' => 'post_title', @@ -24,22 +26,23 @@ class Tainacan_Collections { ], 'order' => [ 'map' => 'menu_order', - 'validation' => v::stringType(), + //'validation' => v::stringType(), ], 'parent' => [ 'map' => 'parent', - 'validation' => v::stringType(), + //'validation' => v::stringType(), ], 'description' => [ 'map' => 'post_content', - 'validation' => v::stringType(), + //'validation' => v::stringType(), ], 'slug' => [ 'map' => 'post_name', - 'validation' => v::stringType(), + //'validation' => v::stringType(), ], 'itens_per_page' => [ 'map' => 'meta', + 'default' => 10, 'validation' => v::intVal()->positive(), ], ]; @@ -82,25 +85,17 @@ class Tainacan_Collections { } function insert(Tainacan_Collection $collection) { + + // validate + if (!$collection->validate()) + return $collection->get_errors(); + + $map = $this->get_map(); + // First iterate through the native post properties - - $map = $this->map; - $count = 0; foreach ($map as $prop => $mapped) { if ($mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi') { - $prop_value = $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); - } + $collection->WP_Post->{$mapped['map']} = $collection->get_mapped_property($prop); } } diff --git a/src/classes/repositories/class-tainacan-filters.php b/src/classes/repositories/class-tainacan-filters.php index e8cb4b22e..f7b8cfdb2 100644 --- a/src/classes/repositories/class-tainacan-filters.php +++ b/src/classes/repositories/class-tainacan-filters.php @@ -7,48 +7,50 @@ class 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(){ 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(){ $labels = array( @@ -93,7 +95,7 @@ class Tainacan_Filters { */ function insert( Tainacan_Filter $metadata ) { // First iterate through the native post properties - $map = $this->map; + $map = $this->get_map(); foreach ($map as $prop => $mapped) { if ($mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi') { $metadata->WP_Post->{$mapped['map']} = $metadata->get_mapped_property($prop); diff --git a/src/classes/repositories/class-tainacan-items.php b/src/classes/repositories/class-tainacan-items.php index d749ef460..061b57e75 100644 --- a/src/classes/repositories/class-tainacan-items.php +++ b/src/classes/repositories/class-tainacan-items.php @@ -5,31 +5,33 @@ if ( ! defined( 'ABSPATH' ) ) { 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() { 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() { global $Tainacan_Collections, $Tainacan_Taxonomies; @@ -55,7 +57,7 @@ class Tainacan_Items { } function insert(Tainacan_Item $item) { - $map = $this->map; + $map = $this->get_map(); // get collection to determine post type $collection = $item->get_collection(); @@ -166,7 +168,7 @@ class Tainacan_Items { // other item properties, present in the "map" function query($args) { - $map = $this->map; + $map = $this->get_map(); $wp_query_exceptions = [ 'ID' => 'p', 'post_title' => 'title' diff --git a/src/classes/repositories/class-tainacan-logs.php b/src/classes/repositories/class-tainacan-logs.php index e88db5b8c..e1fb0f59c 100644 --- a/src/classes/repositories/class-tainacan-logs.php +++ b/src/classes/repositories/class-tainacan-logs.php @@ -9,45 +9,47 @@ class Tainacan_Logs extends Tainacan_Repository { 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() { 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() { $labels = array( 'name' => 'logs', @@ -86,7 +88,7 @@ class Tainacan_Logs extends Tainacan_Repository { function insert(Tainacan_Log $log) { // First iterate through the native post properties - $map = $this->map; + $map = $this->get_map(); foreach ($map as $prop => $mapped) { if ($mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi') { $log->WP_Post->{$mapped['map']} = $log->get_mapped_property($prop); diff --git a/src/classes/repositories/class-tainacan-metadatas.php b/src/classes/repositories/class-tainacan-metadatas.php index 515e9ed62..1b78cace2 100644 --- a/src/classes/repositories/class-tainacan-metadatas.php +++ b/src/classes/repositories/class-tainacan-metadatas.php @@ -10,76 +10,78 @@ class Tainacan_Metadatas { 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() { 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() { $labels = array( @@ -124,7 +126,7 @@ class Tainacan_Metadatas { */ function insert( Tainacan_Metadata $metadata ) { // First iterate through the native post properties - $map = $this->map; + $map = $this->get_map(); foreach ($map as $prop => $mapped) { if ($mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi') { $metadata->WP_Post->{$mapped['map']} = $metadata->get_mapped_property($prop); diff --git a/src/classes/repositories/class-tainacan-taxonomies.php b/src/classes/repositories/class-tainacan-taxonomies.php index 05896ee9e..cb0a26139 100644 --- a/src/classes/repositories/class-tainacan-taxonomies.php +++ b/src/classes/repositories/class-tainacan-taxonomies.php @@ -10,41 +10,42 @@ class 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() { 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() { $labels = array( @@ -109,7 +110,7 @@ class Tainacan_Taxonomies { */ function insert( Tainacan_Taxonomy $taxonomy ) { // First iterate through the native post properties - $map = $this->map; + $map = $this->get_map(); foreach ($map as $prop => $mapped) { if ($mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi') { $taxonomy->WP_Post->{$mapped['map']} = $taxonomy->get_mapped_property($prop); diff --git a/src/classes/repositories/class-tainacan-terms.php b/src/classes/repositories/class-tainacan-terms.php index 04802562b..808abd267 100644 --- a/src/classes/repositories/class-tainacan-terms.php +++ b/src/classes/repositories/class-tainacan-terms.php @@ -8,36 +8,38 @@ exit; */ class Tainacan_Terms { - var $map = [ - '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 get_map() { + return [ + '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; + $map = $this->get_map(); foreach ($map as $prop => $mapped) { if ($mapped['map'] != 'termmeta') { $term->WP_Term->{$mapped['map']} = $term->get_mapped_property($prop); diff --git a/tests/test-sample.php b/tests/test-collections.php similarity index 78% rename from tests/test-sample.php rename to tests/test-collections.php index e0f13854f..2d6b95210 100644 --- a/tests/test-sample.php +++ b/tests/test-collections.php @@ -67,5 +67,24 @@ class Test_Collections extends WP_UnitTestCase { $this->assertEquals($item->get_description(), 'adasdasdsa'); $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())); + + + } }