Removes try catch and use dinamic methods set and get

This commit is contained in:
weryques 2018-05-22 16:50:10 -03:00
parent df4f90f974
commit d8300243be
4 changed files with 21 additions and 33 deletions

View File

@ -24,22 +24,12 @@ class REST_Controller extends \WP_REST_Controller {
if (is_array($attributes)) { if (is_array($attributes)) {
foreach ( $attributes as $attribute ) { foreach ( $attributes as $attribute ) {
try { if ( ! is_array( $attribute ) ) {
if(!is_array($attribute)) { $object_filtered[ $attribute ] = $object->get( $attribute );
$get_ = 'get_' . $attribute;
$object_filtered[ $attribute ] = $object->$get_();
}
} catch ( \Exception $error ) {
// Do nothing
} }
} }
} else { } else {
try{ $object_filtered[ $attributes ] = $object->get( $attributes );
$get_ = 'get_' . $attributes;
$object_filtered[$attributes] = $object->$get_();
} catch (\Exception $error){
// Do nothing
}
} }
return $object_filtered; return $object_filtered;
@ -54,12 +44,7 @@ class REST_Controller extends \WP_REST_Controller {
protected function prepare_item_for_updating($object, $new_values){ protected function prepare_item_for_updating($object, $new_values){
foreach ($new_values as $key => $value) { foreach ($new_values as $key => $value) {
try { $object->set($key, $value);
$set_ = 'set_' . $key;
$object->$set_( $value );
} catch (\Exception $error){
// Do nothing
}
} }
return $object; return $object;

View File

@ -128,12 +128,7 @@ class REST_Filters_Controller extends REST_Controller {
$filter_type = new $type(); $filter_type = new $type();
foreach ($filter as $attribute => $value){ foreach ($filter as $attribute => $value){
try { $filter_obj->set($attribute, $value);
$set_ = 'set_'. $attribute;
$filter_obj->$set_($value);
} catch (\Exception $error){
//
}
} }
if(isset($request['collection_id']) && isset($request['field_id'])) { if(isset($request['collection_id']) && isset($request['field_id'])) {

View File

@ -86,13 +86,7 @@ class REST_Terms_Controller extends REST_Controller {
$taxonomy = $to_prepare[1]; $taxonomy = $to_prepare[1];
foreach ($attributes as $attribute => $value){ foreach ($attributes as $attribute => $value){
$set_ = 'set_'. $attribute; $this->term->set($attribute, $value);
try {
$this->term->$set_( $value );
} catch (\Exception $error){
// Do nothing
}
} }
$this->term->set_taxonomy($taxonomy); $this->term->set_taxonomy($taxonomy);

View File

@ -191,7 +191,21 @@ class Entity {
if ( method_exists($this, $method) ) { if ( method_exists($this, $method) ) {
return $this->$method($value); return $this->$method($value);
} }
return null; }
/**
* get the value property
*
*
* @param string $prop id of the property
* @param mixed $value the value to be setted
* @return null|mixed Null on failure, the value that was set on success
*/
public function get($prop) {
$method = 'get_' . $prop;
if ( method_exists($this, $method) ) {
return $this->$method();
}
} }
/** /**