Skip to content

Commit

Permalink
Added filter_if filter to filter if attribute value matches (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
Spodnet authored and William committed Nov 14, 2019
1 parent 91b4cc2 commit f7bcc08
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 4 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Given a data array with the following format:
'jsonVar' => '{"name":"value"}',
'description' => '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>',
'phone' => '+08(096)90-123-45q',
'country' => 'GB',
'postcode' => 'ab12 3de',
];
```
We can easily format it using our Sanitizer and the some of Sanitizer's default filters:
Expand All @@ -42,6 +44,8 @@ We can easily format it using our Sanitizer and the some of Sanitizer's default
'jsonVar' => 'cast:array',
'description' => 'strip_tags',
'phone' => 'digit',
'country' => 'trim|escape|capitalize',
'postcode' => 'trim|escape|uppercase|filter_if:country,GB',
];

$sanitizer = new Sanitizer($data, $filters);
Expand All @@ -58,6 +62,8 @@ Which will yield:
'jsonVar' => '["name" => "value"]',
'description' => 'Test paragraph. Other text',
'phone' => '080969012345',
'country' => 'GB',
'postcode' => 'AB12 3DE',
];
```
It's usage is very similar to Laravel's Validator module, for those who are already familiar with it, although Laravel is not required to use this library.
Expand All @@ -79,7 +85,7 @@ The following filters are available out of the box:
**format_date** | Always takes two arguments, the date's given format and the target format, following DateTime notation.
**strip_tags** | Strip HTML and PHP tags using php's strip_tags
**digit** | Get only digit characters from the string

**filter_if** | Applies filters if an attribute exactly matches value

## Adding custom filters

Expand Down
20 changes: 20 additions & 0 deletions src/Filters/FilterIf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Waavi\Sanitizer\Filters;

use Waavi\Sanitizer\Contracts\Filter;

class FilterIf implements Filter
{
/**
* Checks if filters should run if there is value passed that matches.
*
* @param array $values
* @param array $options
* @return boolean
*/
public function apply($values, $options = [])
{
return array_key_exists($options[0], $values) && $values[$options[0]] === $options[1];
}
}
27 changes: 24 additions & 3 deletions src/Sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Sanitizer
'trim' => \Waavi\Sanitizer\Filters\Trim::class,
'strip_tags' => \Waavi\Sanitizer\Filters\StripTags::class,
'digit' => \Waavi\Sanitizer\Filters\Digit::class,
'filter_if' => \Waavi\Sanitizer\Filters\FilterIf::class,
];

/**
Expand Down Expand Up @@ -129,12 +130,22 @@ public function sanitize()
foreach ($this->rules as $attr => $rules) {
if (Arr::has($this->data, $attr)) {
$value = Arr::get($this->data, $attr);
$original = $value;

$sanitize = true;
foreach ($rules as $rule) {
$value = $this->applyFilter($rule['name'], $value, $rule['options']);
if ($rule['name'] === 'filter_if') {
$sanitize = $this->applyFilter($rule['name'], $this->data, $rule['options']);
} else {
$value = $this->applyFilter($rule['name'], $value, $rule['options']);
}
}

Arr::set($sanitized, $attr, $value);
if ($sanitize) {
Arr::set($sanitized, $attr, $value);
} else {
Arr::set($sanitized, $attr, $original);
}
}
}

Expand All @@ -151,8 +162,18 @@ public function sanitize()
protected function sanitizeAttribute($attribute, $value)
{
if (isset($this->rules[$attribute])) {
$sanitize = true;
$original = $value;
foreach ($this->rules[$attribute] as $rule) {
$value = $this->applyFilter($rule['name'], $value, $rule['options']);
if ($rule['name'] === 'filter_if') {
$sanitize = $this->applyFilter($rule['name'], $value, $rule['options']);
} else {
$value = $this->applyFilter($rule['name'], $value, $rule['options']);
}
}

if (!$sanitize) {
return $original;
}
}
return $value;
Expand Down
48 changes: 48 additions & 0 deletions tests/Filters/FilterIfTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

use PHPUnit\Framework\TestCase;
use Waavi\Sanitizer\Sanitizer;

class FilterIfTest extends TestCase
{
/**
* @param $data
* @param $rules
* @return mixed
*/
public function sanitize($data, $rules)
{
$sanitizer = new Sanitizer($data, $rules);
return $sanitizer->sanitize();
}

/**
* @test
*/
public function it_apply_filter_if_match()
{
$data = [
'name' => 'HellO EverYboDy',
];
$rules = [
'name' => 'uppercase|filter_if:name,HellO EverYboDy',
];
$data = $this->sanitize($data, $rules);
$this->assertEquals('HELLO EVERYBODY', $data['name']);
}

/**
* @test
*/
public function it_does_not_apply_filter_if_no_match()
{
$data = [
'name' => 'HellO EverYboDy',
];
$rules = [
'name' => 'uppercase|filter_if:name,no match',
];
$data = $this->sanitize($data, $rules);
$this->assertEquals('HellO EverYboDy', $data['name']);
}
}

0 comments on commit f7bcc08

Please sign in to comment.