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) {
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() {

View File

@ -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');

View File

@ -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() {

View File

@ -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');

View File

@ -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');

View File

@ -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');
}

View File

@ -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() {

View File

@ -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) {
// First iterate through the native post properties
$map = $this->map;
$count = 0;
// validate
if (!$collection->validate())
return $collection->get_errors();
$map = $this->get_map();
// First iterate through the native post properties
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);
}
}

View File

@ -7,10 +7,15 @@ class Tainacan_Filters {
const POST_TYPE = 'tainacan-filters';
var $map = [
'ID' => [
function __construct(){
add_action('init', array(&$this, 'register_post_type'));
}
function get_map() {
return [
'id' => [
'map' => 'ID',
'validation' => ''
//'validation' => ''
],
'name' => [
'map' => 'post_title',
@ -45,9 +50,6 @@ class Tainacan_Filters {
'validation' => ''
],
];
function __construct(){
add_action('init', array(&$this, 'register_post_type'));
}
function register_post_type(){
@ -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);

View File

@ -5,10 +5,15 @@ if ( ! defined( 'ABSPATH' ) ) {
class Tainacan_Items {
var $map = [
'ID' => [
function __construct() {
add_action('init', array(&$this, 'register_post_types'));
}
function get_map() {
return [
'id' => [
'map' => 'ID',
'validation' => ''
//'validation' => ''
],
'title' => [
'map' => 'post_title',
@ -25,9 +30,6 @@ class Tainacan_Items {
//'collection' => 'relation...',
// metadata .. metadata...
];
function __construct() {
add_action('init', array(&$this, 'register_post_types'));
}
function register_post_types() {
@ -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'

View File

@ -9,10 +9,15 @@ class Tainacan_Logs extends Tainacan_Repository {
const POST_TYPE = 'tainacan-logs';
var $map = [
'ID' => [
function __construct() {
add_action('init', array(&$this, 'register_post_type'));
}
function get_map() {
return [
'id' => [
'map' => 'ID',
'validation' => ''
//'validation' => ''
],
'title' => [
'map' => 'post_title',
@ -43,9 +48,6 @@ class Tainacan_Logs extends Tainacan_Repository {
'validation' => ''
],
];
function __construct() {
add_action('init', array(&$this, 'register_post_type'));
}
function register_post_type() {
@ -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);

View File

@ -10,10 +10,15 @@ class Tainacan_Metadatas {
const POST_TYPE = 'tainacan-metadata';
var $map = [
'ID' => [
function __construct() {
add_action('init', array(&$this, 'register_post_type'));
}
function get_map() {
return [
'id' => [
'map' => 'ID',
'validation' => ''
//'validation' => ''
],
'name' => [
'map' => 'post_title',
@ -76,9 +81,6 @@ class Tainacan_Metadatas {
'validation' => ''
],
];
function __construct() {
add_action('init', array(&$this, 'register_post_type'));
}
function register_post_type() {
@ -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);

View File

@ -10,10 +10,15 @@ class Tainacan_Taxonomies {
const POST_TYPE = 'tainacan-taxonomies';
var $map = [
'ID' => [
function __construct() {
add_action('init', array(&$this, 'register_post_type'));
}
function get_map() {
return [
'id' => [
'map' => 'ID',
'validation' => ''
//'validation' => ''
],
'name' => [
'map' => 'post_title',
@ -40,10 +45,6 @@ class Tainacan_Taxonomies {
'validation' => ''
],
];
function __construct() {
add_action('init', array(&$this, 'register_post_type'));
}
function register_post_type() {
@ -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);

View File

@ -8,10 +8,11 @@ exit;
*/
class Tainacan_Terms {
var $map = [
function get_map() {
return [
'term_id' => [
'map' => 'term_id',
'validation' => ''
//'validation' => ''
],
'name' => [
'map' => 'name',
@ -34,10 +35,11 @@ class Tainacan_Terms {
'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);

View File

@ -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()));
}
}