Skip to content

Commit

Permalink
Merge pull request #38 from RickvdStaaij/feature/values-in-callback
Browse files Browse the repository at this point in the history
Make the filter data available in callbacks and custom filter rules
  • Loading branch information
Rick van der Staaij authored and Rick van der Staaij committed Dec 10, 2015
2 parents 737a429 + 27d3993 commit 78c2091
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 7 deletions.
8 changes: 6 additions & 2 deletions docs/filter-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,13 @@ $result = $f->filter(['newsletter' => 'yes']);

Callback allows you to quickly add a custom filter, as you can provide a closure that manipulates the value.

The callable function will receive two parameters from the callback rule. First `$value`, holding the
currently filtered value for the current key. And second `$filterData`, holding all the data that is being
filtered by the filter.

```php
$f = new Filter;
$f->value('name')->callback(function($value) {
$f->value('name')->callback(function($value, $filterData) {
return '<strong>' . $value . '</strong>';
});
$result = $f->filter(['name' => 'John']);
Expand All @@ -76,7 +80,7 @@ Callback can also be used if a value is not set in the filter data. Make sure yo

```php
$f = new Filter;
$f->value('year')->callback(function() {
$f->value('year')->callback(function($value, $filterData) {
return date('Y');
}, true);
$result = $f->filter([]); // note that no year is set
Expand Down
4 changes: 3 additions & 1 deletion src/Chain.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ class Chain
*
* @param bool $isSet
* @param mixed $value
* @param array|null $filterData
* @return FilterResult
*/
public function filter($isSet, $value = null)
public function filter($isSet, $value = null, $filterData = null)
{
/** @var FilterRule $rule */
foreach ($this->rules as $rule) {
$rule->setFilterData($filterData);
if ($isSet || $rule->allowedNotSet()) {
$value = $rule->filter($value);
$isSet = true;
Expand Down
6 changes: 3 additions & 3 deletions src/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ protected function filterGlobals(array $data)
if (is_array($value)) {
$data[$key] = $this->filterGlobals($value);
} else {
$filterResult = $this->globalChain->filter(true, $value);
$filterResult = $this->globalChain->filter(true, $value, $data);
$data[$key] = $filterResult->getFilteredValue();
}
}
Expand All @@ -152,10 +152,10 @@ protected function filterGlobals(array $data)
protected function getFilterResult($key, Chain $chain)
{
if ($this->data->has($key)) {
return $chain->filter(true, $this->data->get($key));
return $chain->filter(true, $this->data->get($key), $this->data->getArrayCopy());
}

return $chain->filter(false);
return $chain->filter(false, null, $this->data->getArrayCopy());
}

/**
Expand Down
21 changes: 21 additions & 0 deletions src/FilterRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ abstract class FilterRule
*/
protected $allowNotSet = false;

/**
* @var array|null
*/
protected $filterData;

/**
* @param string|null $encodingFormat
*/
Expand All @@ -31,6 +36,22 @@ public function setEncodingFormat($encodingFormat)
$this->encodingFormat = $encodingFormat;
}

/**
* @param array|null $filterData
*/
public function setFilterData($filterData)
{
$this->filterData = $filterData;
}

/**
* @return array|null
*/
public function getFilterData()
{
return $this->filterData;
}

/**
* @return bool
*/
Expand Down
2 changes: 1 addition & 1 deletion src/FilterRule/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ public function __construct(callable $callable, $allowNotSet = false)
*/
public function filter($value)
{
return call_user_func($this->callable, $value);
return call_user_func($this->callable, $value, $this->getFilterData());
}
}
48 changes: 48 additions & 0 deletions tests/FilterRule/CallbackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,24 @@ public function testNotSetWhenNotAllowedNotSet()
$this->assertEquals([], $result);
}

/**
* Test if the callback can build values using other data provided in the filter data
*
* @dataProvider getMultiValueCallbackResults
* @param array $values
* @param callable $callable
* @param mixed $filteredValue
*/
public function testAccessFilterValuesInCallback($values, callable $callable, $filteredValue)
{
$this->filter->value('test')->callback($callable);

$result = $this->filter->filter($values);

$values['test'] = $filteredValue;
$this->assertEquals($values, $result);
}

/**
* @return array
*/
Expand All @@ -90,4 +108,34 @@ function ($value) {
],
];
}

/**
* @return array
*/
public function getMultiValueCallbackResults()
{
return [
[
[
'gender' => 'Sir',
'test' => 'John',
'last_name' => 'Doe',
],
function ($value, $filterValues) {
return implode(' ', [$filterValues['gender'], $value, $filterValues['last_name']]);
},
'Sir John Doe',
],
[
[
'multiplier' => 10,
'test' => 50,
],
function ($value, $filterValues) {
return $value * $filterValues['multiplier'];
},
500,
],
];
}
}

0 comments on commit 78c2091

Please sign in to comment.