|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Maiorano\ObjectHydrator\Strategies\Reflection; |
|
4
|
|
|
|
|
5
|
|
|
use Generator; |
|
6
|
|
|
use Maiorano\ObjectHydrator\Attributes\HydrationKey; |
|
7
|
|
|
use Maiorano\ObjectHydrator\Mappings\MethodMapping; |
|
8
|
|
|
use Maiorano\ObjectHydrator\Strategies\DirectKeyAccessTrait; |
|
9
|
|
|
use Maiorano\ObjectHydrator\Strategies\HydrationStrategyInterface; |
|
10
|
|
|
use Maiorano\ObjectHydrator\Strategies\RecursiveCheckTrait; |
|
11
|
|
|
use ReflectionClass; |
|
12
|
|
|
use ReflectionMethod; |
|
13
|
|
|
|
|
14
|
|
|
final class MethodsStrategy implements HydrationStrategyInterface |
|
15
|
|
|
{ |
|
16
|
|
|
use DirectKeyAccessTrait; |
|
17
|
|
|
use RecursiveCheckTrait; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var int|null |
|
21
|
|
|
*/ |
|
22
|
|
|
private ?int $methodTypes; |
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
private string $prefix; |
|
27
|
|
|
/** |
|
28
|
|
|
* @var ReflectionClass |
|
29
|
|
|
*/ |
|
30
|
|
|
private ReflectionClass $reflectionClass; |
|
31
|
|
|
/** |
|
32
|
|
|
* @var MethodMapping[] |
|
33
|
|
|
*/ |
|
34
|
|
|
private array $mappings; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param int|null $methodTypes |
|
38
|
|
|
* @param string $prefix |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(?int $methodTypes = ReflectionMethod::IS_PUBLIC, string $prefix = 'set') |
|
41
|
|
|
{ |
|
42
|
|
|
$this->methodTypes = $methodTypes; |
|
43
|
|
|
$this->prefix = $prefix; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param object $object |
|
48
|
|
|
* |
|
49
|
|
|
* @return void |
|
50
|
|
|
*/ |
|
51
|
|
|
public function initialize(object $object): void |
|
52
|
|
|
{ |
|
53
|
|
|
$this->reflectionClass = new ReflectionClass($object); |
|
54
|
|
|
$this->mappings = iterator_to_array($this->generateKeyMap()); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return Generator |
|
59
|
|
|
*/ |
|
60
|
|
|
private function generateKeyMap(): Generator |
|
61
|
|
|
{ |
|
62
|
|
|
foreach ($this->reflectionClass->getMethods($this->methodTypes) as $reflector) { |
|
63
|
|
|
$attributes = $reflector->getAttributes(HydrationKey::class); |
|
64
|
|
|
yield from $attributes |
|
65
|
|
|
? $this->generateKeysFromAttributes($reflector, $attributes) |
|
66
|
|
|
: $this->generateKeysFromNames($reflector); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param ReflectionMethod $method |
|
72
|
|
|
* @param array $attributes |
|
73
|
|
|
* |
|
74
|
|
|
* @return Generator |
|
75
|
|
|
*/ |
|
76
|
|
|
private function generateKeysFromAttributes(ReflectionMethod $method, array $attributes): Generator |
|
77
|
|
|
{ |
|
78
|
|
|
foreach ($attributes as $attribute) { |
|
79
|
|
|
$mapping = new MethodMapping($method, $attribute->newInstance()); |
|
80
|
|
|
yield $mapping->getKey() => $mapping; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param ReflectionMethod $method |
|
86
|
|
|
* |
|
87
|
|
|
* @return Generator |
|
88
|
|
|
*/ |
|
89
|
|
|
private function generateKeysFromNames(ReflectionMethod $method): Generator |
|
90
|
|
|
{ |
|
91
|
|
|
$maybeProp = preg_match("/^{$this->prefix}(.*)/i", $method->getName(), $matches); |
|
92
|
|
|
if ($maybeProp && isset($matches[1])) { |
|
93
|
|
|
yield from $this->checkPropertyNames($method, $this->cleanName($matches[1])); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param ReflectionMethod $method |
|
99
|
|
|
* @param string $name |
|
100
|
|
|
* |
|
101
|
|
|
* @return Generator |
|
102
|
|
|
*/ |
|
103
|
|
|
private function checkPropertyNames(ReflectionMethod $method, string $name): Generator |
|
104
|
|
|
{ |
|
105
|
|
|
foreach ($this->reflectionClass->getProperties() as $property) { |
|
106
|
|
|
if ($name === $this->cleanName($property->getName())) { |
|
107
|
|
|
$mapping = new MethodMapping($method, new HydrationKey($name)); |
|
108
|
|
|
yield $mapping->getKey() => $mapping; |
|
109
|
|
|
break; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param string $name |
|
116
|
|
|
* |
|
117
|
|
|
* @return string |
|
118
|
|
|
*/ |
|
119
|
|
|
private function cleanName(string $name): string |
|
120
|
|
|
{ |
|
121
|
|
|
return lcfirst(preg_replace('/[^a-zA-Z]/', '', $name)); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|