-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppDecorator.php
337 lines (281 loc) · 11.5 KB
/
AppDecorator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
<?php
class AppDecorator implements IteratorAggregate, ArrayAccess, Countable /*, JsonSerializable /* PHP 5.4 */ {
public $decorate = array(); // List of associations to decorate on instantiation
public $children = array(); // Child nodes from threaded find results
protected $attributes = array();
protected $associations = array();
public $serializableAttributes = null;
public $serializableAssociations = null;
private $modelName = null;
private $isCollection = false;
private static $_HtmlHelper = null;
public function __construct($_attributes = null, $modelName = null) {
if ($modelName) {
$this->modelName = $modelName;
}
if ($_attributes) {
$this->set($_attributes);
}
}
public function count() {
if ($this->isCollection) {
return count($this->attributes);
} else {
return empty($this->attributes) ? 0 : 1;
}
}
/**
* @implements IteratorAggregate
* @return ArrayIterator
*/
public function getIterator() {
return new ArrayIterator($this->attributes);
}
/**
* @implements ArrayAccess
* @param mixed $offset
* @param mixed $value
*/
public function offsetSet($offset, $value) {
if (is_null($offset)) {
$this->attributes[] = $value;
} else {
$this->attributes[$offset] = $value;
}
}
/**
* @implements ArrayAccess
* @param mixed $offset
* @return bool
*/
public function offsetExists($offset) {
return isset($this->attributes[$offset]);
}
/**
* @implements ArrayAccess
* @param mixed $offset
*/
public function offsetUnset($offset) {
unset($this->attributes[$offset]);
}
/**
* @implements ArrayAccess
* @param mixed $offset
* @return null
*/
public function offsetGet($offset) {
return isset($this->attributes[$offset]) ? $this->attributes[$offset] : null;
}
public function set($_attributes) {
if (is_array($_attributes)) {
if (array_key_exists($this->getModelName(), $_attributes)) {
$modelAttributes = $_attributes[$this->getModelName()];
unset($_attributes[$this->getModelName()]);
// What remains must all be associations or 'children' (threaded results)
$this->associations = $_attributes;
$_attributes = $modelAttributes;
} else {
$decoratorClass = $this->getModelName() . 'Decorator';
foreach ($_attributes as $key => $data) {
if (is_int($key) && is_array($data)) {
$this->isCollection = true;
if ( ! class_exists($decoratorClass)) {
App::uses($decoratorClass, 'Decorator');
}
if (class_exists($decoratorClass)) {
$_attributes[$key] = new $decoratorClass($data);
} else {
$_attributes[$key] = new AppDecorator($data, $this->getModelName());
}
}
}
}
}
$this->attributes = $_attributes;
foreach ($this->associations as $associationName => $values) {
// Instantiate decorator for associated data
if ( ! $this->decorateAssociation($associationName, false)) {
// leave as array if no decorator is defined
$this->$associationName = $values;
}
}
// Decorate the association as requested by the parent decorator class
foreach ($this->decorate as $associationName) {
if ( ! isset($this->associations[$associationName])
&& isset($this->attributes[$associationName])) {
$this->associations[$associationName] = $this->attributes[$associationName];
}
$this->decorateAssociation($associationName, false);
}
}
public function getModelName() {
if ($this->modelName) {
return $this->modelName;
}
if (get_class($this) == 'AppDecorator' && ! empty($this->attributes)) {
$this->modelName = key(current($this->attributes));
} else {
$this->modelName = str_replace('Decorator', '', get_class($this));
}
return $this->modelName;
}
public function getAttribute($attribute) {
if (array_key_exists($attribute, $this->attributes)) {
return $this->attributes[$attribute];
}
return null;
}
public function getAttributes() {
return $this->attributes;
}
public function __get($attribute) {
if (method_exists($this, $attribute)) {
return $this->$attribute();
}
if (array_key_exists($attribute, $this->attributes)) {
// Note: isset() test is inappropriate as NULL is an acceptable value
return $this->attributes[$attribute];
}
if (isset($this->associations[$attribute])) {
return $this->associations[$attribute];
}
if ($attribute == 'Html') {
if (is_null(self::$_HtmlHelper)) {
App::uses('HtmlHelper', 'View/Helper');
if (class_exists('HtmlHelper')) {
self::$_HtmlHelper = new HtmlHelper(new View());
}
}
return self::$_HtmlHelper;
}
show_error("$attribute is not defined in {$this->getModelName()}", E_USER_NOTICE);
return null;
}
public function __isset($attribute) {
if (method_exists($this, $attribute)) {
return true;
}
return isset($this->attributes[$attribute]);
}
public function jsonSerialize($attributes = null, $associations = null) {
$return = array();
if ($this->isCollection) {
foreach ($this->attributes as $item) {
$return[] = $item->jsonSerialize($attributes, $associations);
}
return $return;
}
if ($attributes === false || $this->serializableAttributes === false) {
// do nothing, we are not returning any attributes or methods
} else if (func_num_args() > 0 || $this->serializableAttributes !== null) {
# passed attribute takes precedence over the class property
$keys = func_num_args() > 0 ? $attributes : $this->serializableAttributes;
# null or true will return all attributes
if ($keys === true || $keys === null) {
$return = $this->attributes;
} else if (is_array($keys)) {
# only return the requested methods and attributes
foreach ($keys as $key) {
if (method_exists($this, $key)) {
$return[$key] = $this->$key;
} else if (isset($this->attributes[$key])) {
$return[$key] = $this->attributes[$key];
}
}
}
} else {
$return = $this->attributes;
}
if ($associations === false || $this->serializableAssociations === false) {
$keys = array();
} else if ( ! empty($associations) || ! empty($this->serializableAssociations)) {
$keys = empty($associations) ? $this->serializableAssociations : $associations;
} else {
$keys = array_keys($this->associations);
}
foreach (array_intersect_key($this->associations, array_flip($keys)) as $name => $association) {
if (is_array($association)) {
foreach ($association as $i => $item) {
$return[$name][$i] = $item->jsonSerialize();
}
} else {
$return[$name] = $association->jsonSerialize();
}
}
return $return;
}
public function setAssociation($name, $data, $decorate = false) {
if (is_subclass_of($data, 'AppDecorator')) {
$this->associations[$name] = $data->attributes;
$this->$name = $data;
} else {
$this->associations[$name] = $data;
if ($decorate) {
$this->decorateAssociation($name);
}
}
}
public function decorateAssociation($name, $showErrors = true) {
if ($name == 'children'
&& isset($this->associations['children']) && is_array($this->associations['children'])
) {
// This data row contains children from a find('threaded') query
if ( ! empty($this->associations['children'])) {
$modelName = key(current($this->associations['children']));
$this->children = new AppDecorator($this->associations['children'], $modelName);
}
unset($this->associations['children']);
return true;
}
if (isset($this->associations[$name])
&& is_subclass_of($this->associations[$name], 'AppDecorator')
|| (isset($this->associations[$name])
&& is_array($this->associations[$name])
&& isset($this->associations[$name][0])
&& is_subclass_of($this->associations[$name][0], 'AppDecorator'))
) {
if ($showErrors) trigger_error("Association $name already decorated", E_USER_NOTICE);
return false;
}
if ( ! array_key_exists($name, $this->associations)) {
if ($showErrors) trigger_error("Association $name does not exist", E_USER_ERROR);
return false;
}
// Instantiate current model to figure out the association class name needed
$model = new $this->getModelName();
$className = $name;
if (isset($model->binds[$name]['className'])) {
$decoratorClass = $model->binds[$name]['className'] . 'Decorator';
$className = $model->binds[$name]['className'];
} else {
// Must be a temporary bind, try the array key as the class name
$decoratorClass = $name . 'Decorator';
}
if ( ! class_exists($decoratorClass)) {
App::uses($decoratorClass, 'Decorator');
}
if ( ! class_exists($decoratorClass)) {
$decoratorClass = 'AppDecorator';
} else if ( ! is_subclass_of($decoratorClass, 'AppDecorator')) {
if ($showErrors) trigger_error("$decoratorClass is not a subclass of AppDecorator", E_USER_ERROR);
return false;
}
// Check if we need to decorate hasMany/HABTM or hasOne/belongsTo
if (isset($model->binds[$name])
&& in_array($model->binds[$name]['bindType'], array('hasMany', 'hasAndBelongsToMany'))
&& is_array($this->associations[$name])
&& count(array_filter(array_keys($this->associations[$name]), 'is_string')) == 0 // Make sure we only have associative keys
) {
$collection = array();
foreach ($this->associations[$name] as $index => $values) {
$this->associations[$name][$index] = new $decoratorClass($this->associations[$name][$index], $className);
$collection[] = $this->associations[$name][$index];
}
$this->$name = $collection;
} else {
$this->associations[$name] = new $decoratorClass($this->associations[$name], $className);
$this->$name = $this->associations[$name];
}
return true;
}
}