Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
12 / 12 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| Delegate | |
100.00% |
12 / 12 |
|
100.00% |
4 / 4 |
5 | |
100.00% |
1 / 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 | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| 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(...$propertyPath): callable |
| 22 | { |
| 23 | $entity = $this->getEntity(); |
| 24 | |
| 25 | return function ($newValue) use ($propertyPath, $entity): Entity { |
| 26 | self::setValueAtPath($entity, $propertyPath, $newValue); |
| 27 | |
| 28 | return $entity; |
| 29 | }; |
| 30 | } |
| 31 | |
| 32 | private static function setValueAtPath(&$container, array $propertyPath, mixed $newValue): void |
| 33 | { |
| 34 | $property = array_shift($propertyPath); |
| 35 | if ($propertyPath === []) { |
| 36 | $container[$property] = $newValue; |
| 37 | |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | self::setValueAtPath($container[$property], $propertyPath, $newValue); |
| 42 | } |
| 43 | } |