Skip to content

Commit

Permalink
v1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonybudd committed May 26, 2017
1 parent b3ae26e commit ffa3cf7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
10 changes: 8 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -272,6 +275,7 @@ Class Product extends WP_Model
...

public $filter = [
'stock' => 'number_format',
'weight'
];

Expand All @@ -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
```

***
Expand Down
28 changes: 25 additions & 3 deletions src/WP_Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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])

)
);
}

/**
Expand All @@ -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)]);
}

Expand Down
12 changes: 11 additions & 1 deletion test/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) ){
Expand All @@ -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');
}



// -----------------------------------------------------
Expand Down

0 comments on commit ffa3cf7

Please sign in to comment.