Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
13 / 13 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| Parser | |
100.00% |
13 / 13 |
|
100.00% |
6 / 6 |
8 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getMsg | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setMsg | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getParsedValue | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| parse | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| getMsgDomain | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| parseValue | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| setMsgContent | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @author: Doug Wilbourne (dougwilbourne@gmail.com) |
| 5 | */ |
| 6 | |
| 7 | declare(strict_types=1); |
| 8 | |
| 9 | namespace pvc\parser; |
| 10 | |
| 11 | use pvc\interfaces\msg\MsgInterface; |
| 12 | use pvc\interfaces\parser\ParserInterface; |
| 13 | |
| 14 | /** |
| 15 | * Parser creates a few default implementation methods for child classes that implement ParserInterface. |
| 16 | * |
| 17 | * Class Parser |
| 18 | * @template DataType |
| 19 | */ |
| 20 | abstract class Parser implements ParserInterface |
| 21 | { |
| 22 | /** |
| 23 | * @var MsgInterface |
| 24 | */ |
| 25 | protected MsgInterface $msg; |
| 26 | |
| 27 | /** |
| 28 | * @var DataType |
| 29 | */ |
| 30 | protected $parsedValue; |
| 31 | |
| 32 | |
| 33 | public function __construct(MsgInterface $msg) |
| 34 | { |
| 35 | $this->setMsg($msg); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * getMsg |
| 40 | * @return MsgInterface |
| 41 | */ |
| 42 | public function getMsg(): MsgInterface |
| 43 | { |
| 44 | return $this->msg; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * setMsg |
| 49 | * @param MsgInterface $msg |
| 50 | */ |
| 51 | public function setMsg(MsgInterface $msg): void |
| 52 | { |
| 53 | $this->msg = $msg; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @function getParsedValue |
| 58 | * @return DataType|null |
| 59 | * see the comment in the parse method below about parsing empty strings as to why we coalesce to null in this |
| 60 | * getter. |
| 61 | */ |
| 62 | public function getParsedValue() |
| 63 | { |
| 64 | return ($this->parsedValue ?? null); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * parse |
| 69 | * @param string $data |
| 70 | * @return bool |
| 71 | */ |
| 72 | public function parse(string $data): bool |
| 73 | { |
| 74 | /** |
| 75 | * clear the message content and the parsedValue attribute so that subsequent iterations of this same parser |
| 76 | * does not leave a message or a parsed value leftover from a prior iteration. |
| 77 | */ |
| 78 | $this->getMsg()->clearContent(); |
| 79 | unset($this->parsedValue); |
| 80 | |
| 81 | /** |
| 82 | * Usually, parsing a string into a specific data type occurs before the data is fed as a parameter into the |
| 83 | * model. Only the model knows whether an empty string (null value) is an acceptable value or not. That's why |
| 84 | * the Validator classes all have an 'isRequired' flag. So the behavior we want is that if the $data argument |
| 85 | * here is an empty string, then we simply return null. If we didn't do that, the parser would fail on an |
| 86 | * empty string and produce a message to the effect that $data could not be parsed into a valid data type. |
| 87 | */ |
| 88 | if ($data === '') { |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * parse the value and set an appropriate message if the value cannot be parsed |
| 94 | */ |
| 95 | if (!$this->parseValue($data)) { |
| 96 | $this->setMsgContent($this->getMsg()); |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | public function getMsgDomain(): string |
| 104 | { |
| 105 | return 'Parser'; |
| 106 | } |
| 107 | |
| 108 | abstract protected function parseValue(string $data): bool; |
| 109 | |
| 110 | abstract protected function setMsgContent(MsgInterface $msg): void; |
| 111 | } |