1
|
|
|
<?php |
2
|
|
|
namespace einfach\representer; |
3
|
|
|
|
4
|
|
|
use einfach\representer\serializer\ArraySerializer; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Trait Representer |
8
|
|
|
* |
9
|
|
|
* @package einfach\representer |
10
|
|
|
*/ |
11
|
|
|
trait Representer |
12
|
|
|
{ |
13
|
|
|
use ArraySerializer; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Object that is being represented |
17
|
|
|
* or a collection handler |
18
|
|
|
*/ |
19
|
|
|
protected $source; |
20
|
|
|
/** |
21
|
|
|
* Class name to be restored |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $targetClassName; |
26
|
|
|
/** |
27
|
|
|
* Strategy indicator |
28
|
|
|
* 1 = one |
29
|
|
|
* 2 = collection |
30
|
|
|
* 3 = restore one |
31
|
|
|
* 4 = restore collection |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $strategy; |
36
|
|
|
|
37
|
10 |
|
public function __construct($source, $strategy) |
38
|
|
|
{ |
39
|
10 |
|
if (is_null($strategy)) { |
40
|
|
|
throw new \Exception('Representer can not be initialized without a strategy param'); |
41
|
|
|
} |
42
|
|
|
|
43
|
10 |
|
$this->source = $source; |
44
|
10 |
|
$this->strategy = $strategy; |
45
|
10 |
|
} |
46
|
|
|
|
47
|
|
|
public function rules() |
48
|
|
|
{ |
49
|
|
|
return []; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Return property name to wrap a collection representation |
54
|
|
|
* If `null` - no wrapper added |
55
|
|
|
* @return null | string |
56
|
|
|
*/ |
57
|
2 |
|
public function collectionWrapper() |
58
|
|
|
{ |
59
|
2 |
|
return null; |
60
|
|
|
} |
61
|
|
|
|
62
|
5 |
|
public function setTargetClassName($name) |
63
|
|
|
{ |
64
|
5 |
|
$this->targetClassName = $name; |
65
|
5 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param $name |
69
|
|
|
* @return PropertyRule |
70
|
|
|
*/ |
71
|
10 |
|
public function property($name) |
72
|
|
|
{ |
73
|
10 |
|
return new PropertyRule($this->source, $name); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Represent one instance |
78
|
|
|
* |
79
|
|
|
* @param $source |
80
|
|
|
* @return static |
81
|
|
|
*/ |
82
|
8 |
|
public static function one($source) |
83
|
|
|
{ |
84
|
8 |
|
return new static($source, 1); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Represent collection of instances |
89
|
|
|
* |
90
|
|
|
* @param array $array |
91
|
|
|
* @return static |
92
|
|
|
*/ |
93
|
4 |
|
public static function collection(array $array) |
94
|
|
|
{ |
95
|
4 |
|
return new static($array, 2); |
96
|
|
|
} |
97
|
|
|
|
98
|
4 |
|
protected function getCollectionRepresentation() |
99
|
|
|
{ |
100
|
4 |
|
if (is_array($this->source) && count($this->source) > 0) { |
101
|
|
|
$representation = array_map(function ($object) { |
102
|
4 |
|
return static::one($object)->getOneRepresentation(); |
103
|
4 |
|
}, $this->source); |
104
|
|
|
|
105
|
4 |
|
if ($wrapperName = $this->collectionWrapper()) { |
|
|
|
|
106
|
2 |
|
return [$wrapperName => $representation]; |
107
|
|
|
} else { |
108
|
2 |
|
return $representation; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
8 |
View Code Duplication |
protected function getOneRepresentation() |
|
|
|
|
114
|
|
|
{ |
115
|
8 |
|
$rules = $this->rules(); |
116
|
8 |
|
if (empty($rules)) { |
117
|
|
|
throw new \Exception("There are rules specified in " . static::class . " representer"); |
118
|
|
|
} |
119
|
|
|
|
120
|
8 |
|
$represented = []; |
121
|
|
|
|
122
|
8 |
|
foreach ($rules as $rule) { |
123
|
|
|
/** @var $rule PropertyRule */ |
124
|
8 |
|
$resultArray = $rule->compile(); |
125
|
|
|
|
126
|
8 |
|
reset($resultArray); |
127
|
8 |
|
$key = key($resultArray); |
128
|
8 |
|
$value = $resultArray[$key]; |
129
|
|
|
|
130
|
8 |
|
$represented[$key] = $value; |
131
|
8 |
|
} |
132
|
|
|
|
133
|
8 |
|
return $represented; |
134
|
|
|
} |
135
|
|
|
|
136
|
8 |
|
protected function getRepresentation() |
137
|
|
|
{ |
138
|
8 |
|
switch ($this->strategy) { |
139
|
8 |
|
case 1: |
140
|
4 |
|
return $this->getOneRepresentation(); |
141
|
4 |
|
case 2: |
142
|
4 |
|
return $this->getCollectionRepresentation(); |
143
|
|
|
default: |
144
|
|
|
throw new \Exception('Wrong representation strategy selected. Maybe you have accidentally called `toJSON` instead of `fromJSON`?'); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param $className |
152
|
|
|
* @return static |
153
|
|
|
*/ |
154
|
5 |
|
public static function restore($className) |
155
|
|
|
{ |
156
|
5 |
|
$instance = new static(null, 3); |
157
|
5 |
|
$instance->setTargetClassName($className); |
158
|
5 |
|
return $instance; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param string $className |
163
|
|
|
* @return static |
164
|
|
|
*/ |
165
|
2 |
|
public static function restoreCollection($className) |
166
|
|
|
{ |
167
|
2 |
|
$instance = new static(null, 4); |
168
|
2 |
|
$instance->setTargetClassName($className); |
169
|
2 |
|
return $instance; |
170
|
|
|
} |
171
|
|
|
|
172
|
5 |
|
protected function getReverseRepresentation($projection) |
173
|
|
|
{ |
174
|
5 |
|
switch ($this->strategy) { |
175
|
5 |
|
case 3: |
176
|
3 |
|
return $this->getOneReverseRepresentation($projection); |
177
|
2 |
|
case 4: |
178
|
2 |
|
return $this->getCollectionReverseRepresentation($projection); |
179
|
|
|
default: |
180
|
|
|
throw new \Exception('Reverse representation strategy not defined'); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
} |
184
|
|
|
|
185
|
5 |
View Code Duplication |
protected function getOneReverseRepresentation($projection) |
|
|
|
|
186
|
|
|
{ |
187
|
5 |
|
$rules = $this->rules(); |
188
|
5 |
|
if (empty($rules)) { |
189
|
|
|
throw new \Exception("There are rules specified in " . static::class . " representer"); |
190
|
|
|
} |
191
|
|
|
|
192
|
5 |
|
$target = new $this->targetClassName(); |
193
|
|
|
|
194
|
5 |
|
foreach ($rules as $rule) { |
195
|
|
|
/** @var $rule PropertyRule */ |
196
|
5 |
|
$resultArray = $rule->reverseCompile($projection); |
197
|
|
|
|
198
|
5 |
|
reset($resultArray); |
199
|
5 |
|
$key = key($resultArray); |
200
|
5 |
|
$value = $resultArray[$key]; |
201
|
|
|
|
202
|
5 |
|
$target->$key = $value; |
203
|
5 |
|
} |
204
|
|
|
|
205
|
5 |
|
return $target; |
206
|
|
|
} |
207
|
|
|
|
208
|
2 |
|
protected function getCollectionReverseRepresentation($projectionArray) |
209
|
|
|
{ |
210
|
2 |
|
if ($wrapperName = $this->collectionWrapper()) { |
|
|
|
|
211
|
1 |
|
if (!isset($projectionArray[$wrapperName])) { |
212
|
|
|
$siblingKeys = join(',', array_keys($projectionArray)); |
213
|
|
|
throw new \Exception("Collection wrapper `{$wrapperName}` not found during restore (instead following keys found: {$siblingKeys} ). In " . static::class . " representer"); |
214
|
|
|
} |
215
|
1 |
|
$projectionArray = $projectionArray[$wrapperName]; |
216
|
1 |
|
} |
217
|
|
|
|
218
|
2 |
|
if (is_array($projectionArray) && count($projectionArray) > 0) { |
219
|
2 |
|
return array_map(function ($projection) { |
220
|
2 |
|
return static::restore($this->targetClassName)->getOneReverseRepresentation($projection); |
221
|
2 |
|
}, $projectionArray); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.