Skip to content

Commit

Permalink
Merge pull request #43 from RickvdStaaij/feature/remove-keys
Browse files Browse the repository at this point in the history
Add Remove and RemoveNull filter rules
  • Loading branch information
Rick van der Staaij authored and Rick van der Staaij committed Jan 22, 2016
2 parents f05f74e + 0fb3af0 commit 746c7e0
Show file tree
Hide file tree
Showing 19 changed files with 465 additions and 23 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ $f = new Particle\Filter\Filter;
$f->values(['user.first_name', 'user.last_name'])->trim()->lower()->upperFirst();
$f->value('newsletter')->bool();
$f->value('created_at')->defaults(date('Y-m-d'));
$f->all()->removeNull();

$result = $f->filter([
'user' => [
'first_name' => ' JOHN ',
'middle_name' => null,
'last_name' => ' DOE ',
],
'newsletter' => 'yes',
'referral' => null,
]);

var_dump($result);
Expand All @@ -46,6 +49,7 @@ var_dump($result);
- [A large set of available filters](http://filter.particle-php.com/en/latest/filter-rules/)
- Ability to set default values if nothing is provided
- Ability to filter nested, repeated arrays
- Ability to remove (empty) values
- Ability to extend the filter to add your own custom filter rules

## Non functional features
Expand Down
25 changes: 25 additions & 0 deletions docs/filter-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ filters, take a look at the callback filter-rule, or check out "Extending the Fi
* [bool](#bool)()
* [callback](#callback)($callable, $allowNotSet = false)
* [defaults](#defaults)($defaultValue)
* [each](#each)($callable)
* [encode](#encode)($toEncodingFormat = null, $fromEncodingFormat = null)
* [float](#float)()
* [int](#int)()
Expand All @@ -17,6 +18,8 @@ filters, take a look at the callback filter-rule, or check out "Extending the Fi
* [numbers](#numbers)()
* [prepend](#prepend)($prepend)
* [regexReplace](#regexreplace)($searchRegex, $replace)
* [remove](#remove)()
* [removeNull](#removenull)()
* [replace](#replace)($search, $replace)
* [string](#string)()
* [stripHtml](#striphtml)($excludeTags = null)
Expand Down Expand Up @@ -234,6 +237,28 @@ $result = $f->filter(['value' => '!!!l!#o?*l&&']);
// array(1) { ["value"]=> string(3) "lol" }
```

## Remove

This rule makes sure a key gets removed from your resulting array.

```php
$f = new Filter;
$f->value('value')->remove();
$result = $f->filter(['value' => 'hello']);
// array(0) {}
```

## RemoveNull

This rule makes sure a key gets removed from your resulting array when the value is null.

```php
$f = new Filter;
$f->values(['value1', 'value2'])->removeNull();
$result = $f->filter(['value1' => 'hello', 'value2' => null]);
// array(1) { ["value1"]=> string(5) "hello" }
```

## Replace

Replace a needle for a replacement in the value.
Expand Down
7 changes: 6 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,28 @@ $f = new Particle\Filter\Filter;

$f->values(['user.first_name', 'user.last_name'])->trim()->lower()->upperFirst();
$f->value('newsletter')->bool();
$f->value('created_at')->defaults(date('Y-m-d'));
$f->all()->removeNull();

$result = $f->filter([
'user' => [
'first_name' => ' JOHN ',
'middle_name' => null,
'last_name' => ' DOE ',
],
'newsletter' => 'yes',
'referral' => null,
]);

var_dump($result);
/**
* array(2) {
* array(3) {
* ["user"]=> array(2) {
* ["first_name"]=> string(4) "John"
* ["last_name"]=> string(3) "Doe"
* }
* ["newsletter"]=> bool(true)
* ["created_at"]=> string(10) "2015-12-10"
* }
*/
```
Expand Down
10 changes: 8 additions & 2 deletions docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ php composer.phar require particle/filter

## First usage

Make sure you include `vendor/autoload.php` in your application, and then create your first validator
Make sure you include `vendor/autoload.php` in your application, and then create your first filter
instance:

```php
use Particle\Validator\Filter;
use Particle\Filter\Filter;

$filter = new Filter;

// filter rules
$filter->all()->trim();
$filter->value('first_name')->upperFirst();

$result = $filter->filter([
// data to filter
]);
```
6 changes: 5 additions & 1 deletion src/Chain.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
*/
namespace Particle\Filter;

use Particle\Filter\Exception\ExpectFilterResultException;

/**
* Class Chain
*
Expand All @@ -27,15 +29,17 @@ class Chain
* @param mixed $value
* @param array|null $filterData
* @return FilterResult
* @throws ExpectFilterResultException
*/
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;
$isSet = $rule->isNotEmpty();
}
}

Expand Down
21 changes: 21 additions & 0 deletions src/Exception/ExpectFilterResultException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Particle.
*
* @link http://github.com/particle-php for the canonical source repository
* @copyright Copyright (c) 2005-2015 Particle (http://particle-php.com)
* @license https://github.com/particle-php/Filter/blob/master/LICENSE New BSD License
*/
namespace Particle\Filter\Exception;

use Exception;

/**
* Class ExpectFilterResultException
*
* @package Particle\Filter\Exception
*/
class ExpectFilterResultException extends Exception
{

}
37 changes: 29 additions & 8 deletions src/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function addFilterRule(FilterRule $rule, $key = null)
*/
public function filter(array $data)
{
$data = $this->filterGlobals($data);
$data = $this->filterArrayWithGlobalChain($data);

$this->data = new Container($data);

Expand All @@ -124,21 +124,40 @@ public function filter(array $data)
* @param array $data
* @return array
*/
protected function filterGlobals(array $data)
protected function filterArrayWithGlobalChain(array $data)
{
if ($this->globalChain === null) {
return $data;
}

foreach ($data as $key => $value) {
if (is_array($value)) {
$data[$key] = $this->filterGlobals($value);
} else {
$filterResult = $this->globalChain->filter(true, $value, $data);
$data[$key] = $filterResult->getFilteredValue();
}
$data = $this->filterValueWithGlobalChain($value, $key, $data);
}

return array_filter($data);
}

/**
* Filters a value with the global chain
*
* @param mixed $value
* @param string $key
* @param array $data
* @return array
*/
protected function filterValueWithGlobalChain($value, $key, $data)
{
if (is_array($value)) {
$data[$key] = $this->filterArrayWithGlobalChain($value);
return $data;
}

$filterResult = $this->globalChain->filter(true, $value, $data);
if ($filterResult->isNotEmpty()) {
$data[$key] = $filterResult->getFilteredValue();
} else {
unset($data[$key]);
}
return $data;
}

Expand Down Expand Up @@ -170,6 +189,8 @@ protected function filterChains()
$key,
$filterResult->getFilteredValue()
);
} else {
$this->data->remove($key);
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/FilterResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,26 @@ public function regexReplace($searchRegex, $replace)
return $this->addRule(new FilterRule\RegexReplace($searchRegex, $replace));
}

/**
* Results rule that returns an empty result so it can be removed
*
* @return $this
*/
public function remove()
{
return $this->addRule(new FilterRule\Remove);
}

/**
* Results rule that returns an empty result when the value is null so it can be removed
*
* @return $this
*/
public function removeNull()
{
return $this->addRule(new FilterRule\RemoveNull);
}

/**
* Results rule that returns a value with replacements
*
Expand Down
10 changes: 5 additions & 5 deletions src/FilterResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ class FilterResult
/**
* @var bool
*/
protected $isSet;
protected $isNotEmpty;

/**
* @var mixed
*/
protected $filteredValue;

/**
* @param bool $isSet
* @param bool $isNotEmpty
* @param null|mixed $filteredValue
*/
public function __construct($isSet, $filteredValue = null)
public function __construct($isNotEmpty, $filteredValue = null)
{
$this->isSet = $isSet;
$this->isNotEmpty = $isNotEmpty;
$this->filteredValue = $filteredValue;
}

Expand All @@ -40,7 +40,7 @@ public function __construct($isSet, $filteredValue = null)
*/
public function isNotEmpty()
{
return $this->isSet;
return $this->isNotEmpty;
}

/**
Expand Down
27 changes: 27 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 bool
*/
protected $isEmpty = false;

/**
* @var array|null
*/
Expand All @@ -36,12 +41,34 @@ public function setEncodingFormat($encodingFormat)
$this->encodingFormat = $encodingFormat;
}

/**
* Set the value to empty
*
* @return null
*/
protected function setEmpty()
{
$this->isEmpty = true;
return null;
}

/**
* @return bool
*/
public function isNotEmpty()
{
return !$this->isEmpty;
}

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

// Make sure that the value is not empty by default
$this->isEmpty = false;
}

/**
Expand Down
10 changes: 4 additions & 6 deletions src/FilterRule/Each.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,10 @@ public function __construct(callable $callable)
*/
public function filter($values)
{
if (!is_array($values)) {
return $values;
}

foreach ($values as $key => $value) {
$values[$key] = $this->filterValue($value);
if (is_array($values)) {
foreach ($values as $key => $value) {
$values[$key] = $this->filterValue($value);
}
}

return $values;
Expand Down
1 change: 1 addition & 0 deletions src/FilterRule/NumberFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function filter($value)
if (empty($value)) {
return $value;
}

return number_format(floatval($value), $this->decimals, $this->decimalPoint, $this->thousandSeparator);
}
}
Loading

0 comments on commit 746c7e0

Please sign in to comment.