-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added filter_if filter to filter if attribute value matches (#44)
- Loading branch information
Showing
4 changed files
with
99 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} | ||
} |