Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
92.86% |
13 / 14 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| Delegate | |
92.86% |
13 / 14 |
|
75.00% |
3 / 4 |
6.01 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getEntity | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setter | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| setValueAtPath | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
3.03 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace BO\Zmsentities\Helper; |
| 4 | |
| 5 | use BO\Zmsentities\Schema\Entity; |
| 6 | |
| 7 | class Delegate |
| 8 | { |
| 9 | protected $entity; |
| 10 | |
| 11 | public function __construct(Entity $entity) |
| 12 | { |
| 13 | $this->entity = $entity; |
| 14 | } |
| 15 | |
| 16 | public function getEntity(): Entity |
| 17 | { |
| 18 | return $this->entity; |
| 19 | } |
| 20 | |
| 21 | public function setter(mixed ...$propertyPath): callable |
| 22 | { |
| 23 | $entity = $this->getEntity(); |
| 24 | |
| 25 | return function (mixed $newValue) use ($propertyPath, $entity): Entity { |
| 26 | self::setValueAtPath($entity, $propertyPath, $newValue); |
| 27 | |
| 28 | return $entity; |
| 29 | }; |
| 30 | } |
| 31 | |
| 32 | private static function setValueAtPath(mixed &$container, array $propertyPath, mixed $newValue): void |
| 33 | { |
| 34 | $property = array_shift($propertyPath); |
| 35 | if ($property === null) { |
| 36 | return; |
| 37 | } |
| 38 | if ($propertyPath === []) { |
| 39 | $container[$property] = $newValue; |
| 40 | |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | self::setValueAtPath($container[$property], $propertyPath, $newValue); |
| 45 | } |
| 46 | } |