1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpJsonRpc\Common\TypeAdapter; |
4
|
|
|
|
5
|
|
|
class TypeAdapter |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var Rule[] |
9
|
|
|
*/ |
10
|
|
|
private $rules = []; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @param Rule |
14
|
|
|
* |
15
|
|
|
* @return $this |
16
|
|
|
*/ |
17
|
|
|
public function register(Rule $rule) |
18
|
|
|
{ |
19
|
|
|
$this->rules[$rule->getClass()] = $rule; |
20
|
|
|
return $this; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param string $class |
25
|
|
|
* |
26
|
|
|
* @return bool |
27
|
|
|
*/ |
28
|
|
|
public function isClassRegistered(string $class): bool |
29
|
|
|
{ |
30
|
|
|
return array_key_exists($class, $this->rules); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param mixed $value Array |
35
|
|
|
* |
36
|
|
|
* @return mixed Instance of registered class |
37
|
|
|
*/ |
38
|
|
|
public function toObject($value) |
39
|
|
|
{ |
40
|
|
|
if (!is_array($value)) { |
41
|
|
|
throw new TypeCastException('Is not array'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
foreach ($this->rules as $class => $rule) { |
45
|
|
|
if ($this->isMatch($rule, $value)) { |
46
|
|
|
return $this->createInstance($class, $value); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
throw new TypeCastException('Rule not found'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param mixed $value Instance of registered class |
55
|
|
|
* |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
|
|
public function toArray($value) |
59
|
|
|
{ |
60
|
|
|
if (!is_object($value)) { |
61
|
|
|
throw new TypeCastException('Is not object'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
foreach ($this->rules as $class => $rule) |
65
|
|
|
{ |
66
|
|
|
if (get_class($value) === $class) { |
|
|
|
|
67
|
|
|
return $this->createMap($class, $value); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
throw new TypeCastException('Rule not found'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
|
|
public function isEmpty(): bool |
78
|
|
|
{ |
79
|
|
|
return count($this->rules) === 0; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param Rule $expected Transform rule |
84
|
|
|
* @param array $value Raw unknown value |
85
|
|
|
* |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
|
|
private function isMatch(Rule $expected, array $value): bool |
89
|
|
|
{ |
90
|
|
|
return 0 === count(array_diff(array_keys($expected->getReflectedMap()), array_keys($value))); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $class |
95
|
|
|
* @param array $data |
96
|
|
|
* |
97
|
|
|
* @return object |
98
|
|
|
*/ |
99
|
|
|
private function createInstance(string $class, array $data) |
100
|
|
|
{ |
101
|
|
|
$rule = $this->rules[$class]; |
102
|
|
|
|
103
|
|
|
$reflectionClass = new \ReflectionClass($class); |
104
|
|
|
$object = $reflectionClass->newInstanceWithoutConstructor(); |
105
|
|
|
$reflectionObject = new \ReflectionObject($object); |
106
|
|
|
|
107
|
|
View Code Duplication |
foreach ($rule->getMap() as $property => $key) { |
|
|
|
|
108
|
|
|
$reflectionProperty = $reflectionObject->getProperty($property); |
109
|
|
|
$reflectionProperty->setAccessible(true); |
110
|
|
|
$reflectionProperty->setValue($object, $data[$key]); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $object; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $class |
118
|
|
|
* @param object $object |
119
|
|
|
* |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
|
|
private function createMap(string $class, $object) |
123
|
|
|
{ |
124
|
|
|
$rule = $this->rules[$class]; |
125
|
|
|
$reflectionObject = new \ReflectionObject($object); |
126
|
|
|
$result = []; |
127
|
|
|
|
128
|
|
View Code Duplication |
foreach ($rule->getMap() as $property => $key) { |
|
|
|
|
129
|
|
|
$reflectionProperty = $reflectionObject->getProperty($property); |
130
|
|
|
$reflectionProperty->setAccessible(true); |
131
|
|
|
$result[$key] = $reflectionProperty->getValue($object); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $result; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|