From ffa3cf7ad7bff9e95164100cca4b5594ba3973d0 Mon Sep 17 00:00:00 2001 From: Anthony Budd Date: Fri, 26 May 2017 15:07:17 +0100 Subject: [PATCH] v1.1.0 --- readme.md | 10 ++++++++-- src/WP_Model.php | 28 +++++++++++++++++++++++++--- test/test.php | 12 +++++++++++- 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/readme.md b/readme.md index 1e354cd..ce2c4b2 100644 --- a/readme.md +++ b/readme.md @@ -263,7 +263,10 @@ echo $product->color; // black ``` ### Filter Properties -If you need a property to be parsed before the are returned you can use a filter method. You must add the attribute name to a array named $filter and create a method prefixed with ‘_filter’, this method must take one argument, this will be the property value. +If you need a property to be parsed before it is returned you can use a filter method. You must add the attribute name to a array named $filter and create a method prefixed with ‘_filter’, this method must take one argument, this will be the property value. + +Alternatively, if you want to send the value through an existing function (intval(), number_format(), your_function(), etc) you can do this by naming the desired function as the value using the assoc array syntax. +Note: as the example code shows, you can use both methods of filtering simultaneously. ```php @@ -272,6 +275,7 @@ Class Product extends WP_Model ... public $filter = [ + 'stock' => 'number_format', 'weight' ]; @@ -281,10 +285,12 @@ Class Product extends WP_Model } $product = Product::insert([ - 'weight' => '250' + 'stock' => '3450', + 'weight' => '250', ]); echo $product->weight; // (int) 250 +echo $product->stock; // (string) 3,450 ``` *** diff --git a/src/WP_Model.php b/src/WP_Model.php index 36f4f13..2bd75c9 100644 --- a/src/WP_Model.php +++ b/src/WP_Model.php @@ -300,15 +300,30 @@ public function getVirtualProperty($attribute) /** * Returns TRUE if $attribute is in the $filter array * and has a corresponding filter property method + * OR + * Returns TRUE if $attribute is in the $filter array + * and the $filter array is an asoc array (:318) + * and the value corresponding to the key ($attribute) has is the name of an exiting function. * * @param string $attribute * @return bool */ public function isFilterProperty($attribute) { - return (isset($this->filter) && - in_array($attribute, $this->filter) && - method_exists($this, ('_filter'. ucfirst($attribute)))); + return ( + ( + isset($this->filter) && + in_array($attribute, $this->filter) && + method_exists($this, ('_filter'. ucfirst($attribute))) + ) || ( + count(array_filter(array_keys($this->filter), 'is_string')) > 0 && + isset($this->filter) && + in_array($attribute, array_keys($this->filter)) && + isset($this->filter[$attribute]) && + function_exists($this->filter[$attribute]) + + ) + ); } /** @@ -319,6 +334,13 @@ public function isFilterProperty($attribute) */ public function getFilterProperty($attribute) { + if( count(array_filter(array_keys($this->filter), 'is_string')) > 0 && + isset($this->filter[$attribute]) && + function_exists($this->filter[$attribute])) { + + return ($this->filter[$attribute]($this->get($attribute))); + } + return call_user_func_array([$this, ('_filter'. ucfirst($attribute))], [$this->get($attribute)]); } diff --git a/test/test.php b/test/test.php index fcbbb44..029d14d 100644 --- a/test/test.php +++ b/test/test.php @@ -77,7 +77,9 @@ function test(){ // Filter $product = Product::insert([ - 'weight' => '250' + 'weight' => '250', + 'stock_left' => '50', + 'items_sold' => '5000' ]); if(! (is_int($product->weight)) ){ @@ -88,6 +90,14 @@ function test(){ error(__LINE__ .' $default'); } + if(! (is_int($product->stock_left)) ){ + error(__LINE__ .' $default'); + } + + if(! ($product->items_sold === '5,000') ){ + error(__LINE__ .' $default'); + } + // -----------------------------------------------------