Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
87.88% |
29 / 33 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
| PropertyAccess | |
87.88% |
29 / 33 |
|
25.00% |
1 / 4 |
14.35 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getPropertyValue | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| visit | |
92.59% |
25 / 27 |
|
0.00% |
0 / 1 |
10.04 | |||
| throwErrorOnMissingValue | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| 1 | <?php |
| 2 | namespace Apie\Core; |
| 3 | |
| 4 | use Apie\Core\Context\ApieContext; |
| 5 | use Apie\Core\Exceptions\IndexNotFoundException; |
| 6 | use Apie\Core\Lists\ItemHashmap; |
| 7 | use Apie\Core\Lists\ItemList; |
| 8 | use Apie\Core\Metadata\GetterInterface; |
| 9 | use Apie\Core\Metadata\MetadataFactory; |
| 10 | use Apie\Core\Metadata\MetadataInterface; |
| 11 | use ReflectionClass; |
| 12 | |
| 13 | final class PropertyAccess |
| 14 | { |
| 15 | private function __construct() |
| 16 | { |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * @param array<int, string> $property |
| 21 | */ |
| 22 | public static function getPropertyValue( |
| 23 | object $object, |
| 24 | array $property, |
| 25 | ApieContext $context = new ApieContext(), |
| 26 | bool $throwErrorOnMissing = true |
| 27 | ): mixed { |
| 28 | $node = MetadataFactory::getResultMetadata(new ReflectionClass($object), $context); |
| 29 | |
| 30 | return self::visit($object, $node, $context, $property, $throwErrorOnMissing); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * @param array<int, string> $property |
| 35 | */ |
| 36 | private static function visit( |
| 37 | mixed $object, |
| 38 | MetadataInterface $node, |
| 39 | ApieContext $apieContext, |
| 40 | array $property, |
| 41 | bool $throwErrorOnMissing |
| 42 | ): mixed { |
| 43 | $hashmap = $node->getHashmap(); |
| 44 | $key = array_shift($property); |
| 45 | if (($hashmap[$key] ?? null) instanceof GetterInterface) { |
| 46 | $newObject = $hashmap[$key]->getValue($object, $apieContext); |
| 47 | if (empty($property)) { |
| 48 | return $newObject; |
| 49 | } |
| 50 | if (!is_object($newObject)) { |
| 51 | return self::throwErrorOnMissingValue($key, $throwErrorOnMissing); |
| 52 | } |
| 53 | $class = new ReflectionClass($newObject); |
| 54 | return self::visit( |
| 55 | $newObject, |
| 56 | MetadataFactory::getResultMetadata($class, $apieContext), |
| 57 | $apieContext, |
| 58 | $property, |
| 59 | $throwErrorOnMissing |
| 60 | ); |
| 61 | } |
| 62 | $arrayItemType = $node->getArrayItemType(); |
| 63 | if (null === $arrayItemType) { |
| 64 | return self::throwErrorOnMissingValue($key, $throwErrorOnMissing); |
| 65 | } |
| 66 | if (is_array($object) || $object instanceof ItemList || $object instanceof ItemHashmap) { |
| 67 | if (!isset($object[$key])) { |
| 68 | return self::throwErrorOnMissingValue($key, $throwErrorOnMissing); |
| 69 | } |
| 70 | $value = $object[$key]; |
| 71 | if (empty($property)) { |
| 72 | return $value; |
| 73 | } |
| 74 | return self::visit($value, $arrayItemType, $apieContext, $property, $throwErrorOnMissing); |
| 75 | } |
| 76 | return self::throwErrorOnMissingValue($key, $throwErrorOnMissing); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * @phpstan-return ($throwErrorOnMissing is true ? never : null) |
| 81 | */ |
| 82 | private static function throwErrorOnMissingValue(?string $key, bool $throwErrorOnMissing): mixed |
| 83 | { |
| 84 | if ($throwErrorOnMissing) { |
| 85 | throw new IndexNotFoundException($key); |
| 86 | } |
| 87 | return null; |
| 88 | } |
| 89 | } |