Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
8 / 8 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
ParserBooleanLoose | |
100.00% |
8 / 8 |
|
100.00% |
2 / 2 |
3 | |
100.00% |
1 / 1 |
parseValue | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
setMsgContent | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | /** |
4 | * @author Doug Wilbourne (dougwilbourne@gmail.com) |
5 | */ |
6 | |
7 | declare(strict_types=1); |
8 | |
9 | namespace pvc\parser\boolean; |
10 | |
11 | use pvc\interfaces\msg\MsgInterface; |
12 | use pvc\parser\Parser; |
13 | |
14 | /** |
15 | * ParserBoolean will take a string and try to convert it into a semantically appropriate boolean value |
16 | * |
17 | * Class ParserBooleanLoose |
18 | * @extends Parser<bool> |
19 | */ |
20 | class ParserBooleanLoose extends Parser |
21 | { |
22 | /** |
23 | * |
24 | * Using filter_var, parse will try and be smart about interpreting Yes, no, true, false, etc |
25 | * appropriately. Note that the FILTER_NULL_ON_FAILURE flag is critical to this working right because |
26 | * FILTER_VALIDATE_BOOLEAN is actually a sanitizer and is returning the (typecast) value of $value, NOT whether |
27 | * in fact $value can be interpreted as boolean or not. There is |
28 | * no flag FILTER_SANITIZE_BOOLEAN. It seems to be a complaint in the PHP community... |
29 | * |
30 | * @function parseValue |
31 | * @param string $data |
32 | * @return bool |
33 | * |
34 | */ |
35 | protected function parseValue(string $data): bool |
36 | { |
37 | $result = filter_var($data, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); |
38 | if (is_null($result)) { |
39 | return false; |
40 | } |
41 | $this->parsedValue = $result; |
42 | return true; |
43 | } |
44 | |
45 | /** |
46 | * setMsgContent |
47 | * @param MsgInterface $msg |
48 | */ |
49 | protected function setMsgContent(MsgInterface $msg): void |
50 | { |
51 | $msgId = 'not_boolean_loose'; |
52 | $msgParameters = []; |
53 | $msg->setContent($this->getMsgDomain(), $msgId, $msgParameters); |
54 | } |
55 | } |