1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpJsonRpc\Common\TypeAdapter; |
4
|
|
|
|
5
|
|
|
class Rule |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var string |
9
|
|
|
*/ |
10
|
|
|
private $class; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Property => Key |
14
|
|
|
* |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
private $map = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Key => Property |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $reflectedMap = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private $constructors = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private $serializers = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Rule constructor. |
38
|
|
|
* |
39
|
|
|
* @param string $class |
40
|
|
|
*/ |
41
|
|
|
public function __construct($class) |
42
|
|
|
{ |
43
|
|
|
$this->class = $class; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Create new rule |
48
|
|
|
* |
49
|
|
|
* @param string $class |
50
|
|
|
* |
51
|
|
|
* @return Rule |
52
|
|
|
*/ |
53
|
|
|
public static function create(string $class): Rule |
54
|
|
|
{ |
55
|
|
|
return new Rule($class); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Create rule with one-to-one mapping |
60
|
|
|
* |
61
|
|
|
* @param string $class |
62
|
|
|
* |
63
|
|
|
* @return Rule |
64
|
|
|
*/ |
65
|
|
|
public static function createDefault(string $class): Rule |
66
|
|
|
{ |
67
|
|
|
$rule = new Rule($class); |
68
|
|
|
|
69
|
|
|
$reflection = new \ReflectionClass($class); |
70
|
|
|
$properties = $reflection->getProperties(); |
71
|
|
|
|
72
|
|
|
foreach ($properties as $property) { |
73
|
|
|
/** @var \ReflectionProperty $property */ |
74
|
|
|
$rule->assign($property->getName(), $property->getName()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $rule; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Simple property-key pair assigning |
82
|
|
|
* |
83
|
|
|
* @param string $property Object property |
84
|
|
|
* @param string $key Map key |
85
|
|
|
* |
86
|
|
|
* @return $this |
87
|
|
|
*/ |
88
|
|
|
public function assign(string $property, string $key) |
89
|
|
|
{ |
90
|
|
|
$this->doAssign($property, $key); |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Assign property constructor |
97
|
|
|
* |
98
|
|
|
* @param string $property |
99
|
|
|
* @param string $key |
100
|
|
|
* @param callable $fx |
101
|
|
|
* |
102
|
|
|
* @return $this |
103
|
|
|
*/ |
104
|
|
|
public function assignConstructor(string $property, string $key, callable $fx) |
105
|
|
|
{ |
106
|
|
|
$this->doAssign($property, $key); |
107
|
|
|
$this->constructors[$property] = $fx; |
108
|
|
|
|
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Assign property serializer |
114
|
|
|
* |
115
|
|
|
* @param string $property |
116
|
|
|
* @param string $key |
117
|
|
|
* @param callable $fx |
118
|
|
|
* |
119
|
|
|
* @return $this |
120
|
|
|
*/ |
121
|
|
|
public function assignSerializer(string $property, string $key, callable $fx) |
122
|
|
|
{ |
123
|
|
|
$this->doAssign($property, $key); |
124
|
|
|
$this->serializers[$property] = $fx; |
125
|
|
|
|
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
public function getClass(): string |
133
|
|
|
{ |
134
|
|
|
return $this->class; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
|
|
public function getMap(): array |
141
|
|
|
{ |
142
|
|
|
return $this->map; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return array |
147
|
|
|
*/ |
148
|
|
|
public function getReflectedMap(): array |
149
|
|
|
{ |
150
|
|
|
return $this->reflectedMap; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param string $property |
155
|
|
|
* |
156
|
|
|
* @return callable|null |
157
|
|
|
*/ |
158
|
|
|
public function getConstructor(string $property) |
159
|
|
|
{ |
160
|
|
|
return $this->constructors[$property] ?? null; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param string $property |
165
|
|
|
* |
166
|
|
|
* @return callable|null |
167
|
|
|
*/ |
168
|
|
|
public function getSerializer(string $property) |
169
|
|
|
{ |
170
|
|
|
return $this->serializers[$property] ?? null; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param string $property |
175
|
|
|
* @param string $key |
176
|
|
|
*/ |
177
|
|
|
private function doAssign(string $property, string $key) |
178
|
|
|
{ |
179
|
|
|
$this->map[$property] = $key; |
180
|
|
|
$this->reflectedMap[$key] = $property; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|