Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
37.50% |
3 / 8 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
| IdTypeTrait | |
37.50% |
3 / 8 |
|
25.00% |
1 / 4 |
14.79 | |
0.00% |
0 / 1 |
| getIdTester | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setIdTester | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setIdType | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| testIdType | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace pvc\struct\types\id; |
| 4 | |
| 5 | use pvc\interfaces\struct\types\id\IdTesterInterface; |
| 6 | use pvc\interfaces\struct\types\id\IdType; |
| 7 | use pvc\struct\types\err\NonExistentIdTesterException; |
| 8 | |
| 9 | trait IdTypeTrait |
| 10 | { |
| 11 | protected IdTesterInterface $idTester; |
| 12 | |
| 13 | protected function getIdTester() : ?IdTesterInterface |
| 14 | { |
| 15 | return $this->idTester ?? null; |
| 16 | } |
| 17 | |
| 18 | public function setIdTester(IdTesterInterface $idTester): void |
| 19 | { |
| 20 | $this->idTester = $idTester; |
| 21 | } |
| 22 | |
| 23 | public function setIdType(IdType $idType) : void |
| 24 | { |
| 25 | if ($this->getIdTester() === null) { |
| 26 | throw new NonExistentIdTesterException(); |
| 27 | } |
| 28 | $this->idTester->setIdType($idType); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * via the nullsafe operator, this method identifies a key as valid if the |
| 33 | * idTester property is not set. Note that the key can be of any data type |
| 34 | * (e.g. an array, an object, etc) and this method will pass it through |
| 35 | */ |
| 36 | public function testIdType($id) : bool |
| 37 | { |
| 38 | $result = $this->getIdTester()?->testIdType($id); |
| 39 | if ($result === null) return true; |
| 40 | return $result; |
| 41 | } |
| 42 | } |