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
|
10 |
|
public function __construct(int $propertyTypes = ReflectionProperty::IS_PUBLIC) |
36
|
|
|
{ |
37
|
10 |
|
$this->propertyTypes = $propertyTypes; |
38
|
10 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param object $object |
42
|
|
|
* |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
10 |
|
public function initialize(object $object): void |
46
|
|
|
{ |
47
|
10 |
|
$this->reflectionClass = new ReflectionClass($object); |
48
|
10 |
|
$this->mappings = iterator_to_array($this->generateKeyMap()); |
49
|
10 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return Generator |
53
|
|
|
*/ |
54
|
10 |
|
private function generateKeyMap(): Generator |
55
|
|
|
{ |
56
|
10 |
|
foreach ($this->reflectionClass->getProperties($this->propertyTypes) as $property) { |
57
|
10 |
|
$attributes = $property->getAttributes(HydrationKey::class); |
58
|
10 |
|
yield from $attributes |
59
|
7 |
|
? $this->generateKeysFromAttributes($property, $attributes) |
60
|
10 |
|
: $this->generateKeysFromNames($property); |
61
|
|
|
} |
62
|
10 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param ReflectionProperty $property |
66
|
|
|
* @param array $attributes |
67
|
|
|
* |
68
|
|
|
* @return Generator |
69
|
|
|
*/ |
70
|
7 |
|
private function generateKeysFromAttributes(ReflectionProperty $property, array $attributes): Generator |
71
|
|
|
{ |
72
|
7 |
|
foreach ($attributes as $attribute) { |
73
|
7 |
|
$mapping = new PropertyMapping($property, $attribute->newInstance()); |
74
|
7 |
|
yield $mapping->getKey() => $mapping; |
75
|
|
|
} |
76
|
7 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param ReflectionProperty $property |
80
|
|
|
* |
81
|
|
|
* @return Generator |
82
|
|
|
*/ |
83
|
10 |
|
private function generateKeysFromNames(ReflectionProperty $property): Generator |
84
|
|
|
{ |
85
|
10 |
|
$mapping = new PropertyMapping($property, new HydrationKey($property->getName())); |
86
|
10 |
|
yield $mapping->getKey() => $mapping; |
87
|
10 |
|
} |
88
|
|
|
} |
89
|
|
|
|