|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Maiorano\ObjectHydrator\Strategies\Reflection; |
|
4
|
|
|
|
|
5
|
|
|
use Generator; |
|
6
|
|
|
use Maiorano\ObjectHydrator\Attributes\HydrationKey; |
|
7
|
|
|
use Maiorano\ObjectHydrator\Mappings\PropertyMapping; |
|
8
|
|
|
use Maiorano\ObjectHydrator\Strategies\DirectKeyAccessTrait; |
|
9
|
|
|
use Maiorano\ObjectHydrator\Strategies\HydrationStrategyInterface; |
|
10
|
|
|
use Maiorano\ObjectHydrator\Strategies\RecursiveCheckTrait; |
|
11
|
|
|
use ReflectionClass; |
|
12
|
|
|
use ReflectionProperty; |
|
13
|
|
|
|
|
14
|
|
|
final class PropertiesStrategy implements HydrationStrategyInterface |
|
15
|
|
|
{ |
|
16
|
|
|
use RecursiveCheckTrait; |
|
17
|
|
|
use DirectKeyAccessTrait; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var int |
|
21
|
|
|
*/ |
|
22
|
|
|
private int $propertyTypes; |
|
23
|
|
|
/** |
|
24
|
|
|
* @var ReflectionClass |
|
25
|
|
|
*/ |
|
26
|
|
|
private ReflectionClass $reflectionClass; |
|
27
|
|
|
/** |
|
28
|
|
|
* @var PropertyMapping[] |
|
29
|
|
|
*/ |
|
30
|
|
|
private array $mappings; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param int $propertyTypes |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(int $propertyTypes = ReflectionProperty::IS_PUBLIC) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->propertyTypes = $propertyTypes; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param object $object |
|
42
|
|
|
* |
|
43
|
|
|
* @return void |
|
44
|
|
|
*/ |
|
45
|
|
|
public function initialize(object $object): void |
|
46
|
|
|
{ |
|
47
|
|
|
$this->reflectionClass = new ReflectionClass($object); |
|
48
|
|
|
$this->mappings = iterator_to_array($this->generateKeyMap()); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return Generator |
|
53
|
|
|
*/ |
|
54
|
|
|
private function generateKeyMap(): Generator |
|
55
|
|
|
{ |
|
56
|
|
|
foreach ($this->reflectionClass->getProperties($this->propertyTypes) as $property) { |
|
57
|
|
|
$attributes = $property->getAttributes(HydrationKey::class); |
|
58
|
|
|
yield from $attributes |
|
59
|
|
|
? $this->generateKeysFromAttributes($property, $attributes) |
|
60
|
|
|
: $this->generateKeysFromNames($property); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param ReflectionProperty $property |
|
66
|
|
|
* @param array $attributes |
|
67
|
|
|
* |
|
68
|
|
|
* @return Generator |
|
69
|
|
|
*/ |
|
70
|
|
|
private function generateKeysFromAttributes(ReflectionProperty $property, array $attributes): Generator |
|
71
|
|
|
{ |
|
72
|
|
|
foreach ($attributes as $attribute) { |
|
73
|
|
|
$mapping = new PropertyMapping($property, $attribute->newInstance()); |
|
74
|
|
|
yield $mapping->getKey() => $mapping; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param ReflectionProperty $property |
|
80
|
|
|
* |
|
81
|
|
|
* @return Generator |
|
82
|
|
|
*/ |
|
83
|
|
|
private function generateKeysFromNames(ReflectionProperty $property): Generator |
|
84
|
|
|
{ |
|
85
|
|
|
$mapping = new PropertyMapping($property, new HydrationKey($property->getName())); |
|
86
|
|
|
yield $mapping->getKey() => $mapping; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|