Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
9 / 9 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
Boolean | |
100.00% |
9 / 9 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
filter | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace Verja\Filter; |
4 | |
5 | use Verja\Filter; |
6 | use Verja\Gate; |
7 | use Verja\Validator; |
8 | |
9 | class Boolean extends Filter |
10 | { |
11 | /** @var string[] */ |
12 | protected $stringTrue = ['1', 'true', 't', 'yes', 'y']; |
13 | |
14 | /** @var string[] */ |
15 | protected $stringFalse = ['0', 'false', 'f', 'no', 'n']; |
16 | |
17 | /** |
18 | * Boolean constructor. |
19 | * |
20 | * @param string[] $stringTrue |
21 | * @param string[] $stringFalse |
22 | * @param bool $overwrite Overwrite the arrays instead of merging |
23 | */ |
24 | public function __construct(array $stringTrue = [], array $stringFalse = [], $overwrite = false) |
25 | { |
26 | if ($overwrite) { |
27 | $this->stringTrue = $stringTrue; |
28 | $this->stringFalse = $stringFalse; |
29 | } else { |
30 | $this->stringTrue = array_merge($this->stringTrue, $stringTrue); |
31 | $this->stringFalse = array_merge($this->stringFalse, $stringFalse); |
32 | } |
33 | } |
34 | |
35 | /** |
36 | * Filter $value |
37 | * |
38 | * @param mixed $value |
39 | * @param array $context |
40 | * @return boolean |
41 | */ |
42 | public function filter($value, array $context = []) |
43 | { |
44 | Gate::assert(new Validator\Boolean($this->stringTrue, $this->stringFalse, true), $value); |
45 | |
46 | // the validator says it's either true or false string |
47 | if (is_string($value)) { |
48 | return in_array(strtolower($value), $this->stringTrue); |
49 | } |
50 | |
51 | return (bool)$value; |
52 | } |
53 | } |