|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Superdesk Web Publisher Templates System. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright 2015 Sourcefabric z.ú. and contributors. |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please see the |
|
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* @copyright 2015 Sourcefabric z.ú |
|
12
|
|
|
* @license http://www.superdesk.org/license |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace SWP\Component\TemplatesSystem\Gimme\Meta; |
|
16
|
|
|
|
|
17
|
|
|
use SWP\Component\TemplatesSystem\Gimme\Context\Context; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class Meta. |
|
21
|
|
|
*/ |
|
22
|
|
|
class Meta implements MetaInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Original Meta values (json|array|object). |
|
26
|
|
|
* |
|
27
|
|
|
* @var mixed |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $values; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var Context |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $context; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $configuration; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var array |
|
43
|
|
|
*/ |
|
44
|
|
|
private $copiedValues = []; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Create Meta class from provided configuration and values. |
|
48
|
20 |
|
* |
|
49
|
|
|
* @param Context $context |
|
50
|
20 |
|
* @param string|array|object $values |
|
51
|
20 |
|
* @param array $configuration |
|
52
|
20 |
|
*/ |
|
53
|
|
|
public function __construct(Context $context, $values, $configuration) |
|
54
|
20 |
|
{ |
|
55
|
2 |
|
$this->context = $context; |
|
56
|
18 |
|
$this->values = $values; |
|
57
|
|
|
$this->configuration = $configuration; |
|
58
|
18 |
|
|
|
59
|
18 |
|
$this->context->registerMeta($this); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
20 |
|
/** |
|
63
|
20 |
|
* Use to_string property from configuration if provided, json with exposed properties otherwise. |
|
64
|
|
|
* |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
|
|
public function __toString() |
|
68
|
|
|
{ |
|
69
|
|
|
if (array_key_exists('to_string', $this->configuration)) { |
|
70
|
10 |
|
$toStringProperty = $this->configuration['to_string']; |
|
71
|
|
|
$this->__load($toStringProperty); |
|
72
|
10 |
|
if (isset($this->copiedValues[$toStringProperty])) { |
|
73
|
10 |
|
return $this->copiedValues[$toStringProperty]; |
|
74
|
|
|
} |
|
75
|
10 |
|
} |
|
76
|
5 |
|
|
|
77
|
|
|
return gettype($this->values); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
10 |
|
/** |
|
81
|
|
|
* @param string $name |
|
82
|
|
|
* |
|
83
|
|
|
* @return bool |
|
84
|
|
|
*/ |
|
85
|
|
|
public function __isset(string $name) |
|
86
|
|
|
{ |
|
87
|
19 |
|
$this->__load($name); |
|
88
|
|
|
if (array_key_exists($name, $this->copiedValues)) { |
|
89
|
19 |
|
return true; |
|
90
|
9 |
|
} |
|
91
|
|
|
|
|
92
|
9 |
|
return false; |
|
93
|
1 |
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
9 |
|
* @param string $name |
|
97
|
|
|
* @param mixed $value |
|
98
|
9 |
|
*/ |
|
99
|
|
|
public function __set($name, $value) |
|
100
|
|
|
{ |
|
101
|
19 |
|
if ($value instanceof \Traversable || is_array($value)) { |
|
102
|
19 |
|
$newValue = []; |
|
103
|
|
|
|
|
104
|
19 |
|
foreach ($value as $key => $item) { |
|
105
|
|
|
$newValue[$key] = $this->getValueOrMeta($item); |
|
106
|
19 |
|
} |
|
107
|
8 |
|
|
|
108
|
|
|
$this->copiedValues[$name] = $newValue; |
|
109
|
|
|
|
|
110
|
19 |
|
return; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$this->copiedValues[$name] = $this->getValueOrMeta($value); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
10 |
|
/** |
|
117
|
|
|
* @param string $name |
|
118
|
10 |
|
* |
|
119
|
|
|
* @return mixed|null |
|
120
|
|
|
*/ |
|
121
|
|
|
public function __get(string $name) |
|
122
|
|
|
{ |
|
123
|
|
|
if (array_key_exists($name, $this->copiedValues)) { |
|
124
|
20 |
|
return $this->copiedValues[$name]; |
|
125
|
|
|
} |
|
126
|
20 |
|
$this->__load($name); |
|
127
|
|
|
|
|
128
|
|
|
return $this->copiedValues[$name]; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @return array|object|string |
|
133
|
|
|
*/ |
|
134
|
|
|
public function getValues() |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->values; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
1 |
|
* @return array |
|
141
|
|
|
*/ |
|
142
|
1 |
|
public function getConfiguration() |
|
143
|
|
|
{ |
|
144
|
|
|
return $this->configuration; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param array $configuration |
|
149
|
|
|
*/ |
|
150
|
|
|
public function setConfiguration($configuration) |
|
151
|
|
|
{ |
|
152
|
|
|
$this->configuration = $configuration; |
|
153
|
2 |
|
} |
|
154
|
|
|
|
|
155
|
2 |
|
/** |
|
156
|
2 |
|
* @return Context |
|
157
|
|
|
*/ |
|
158
|
|
|
public function getContext() |
|
159
|
2 |
|
{ |
|
160
|
|
|
return $this->context; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Don't serialize values, context and configuration. |
|
165
|
|
|
* |
|
166
|
|
|
* @return array |
|
167
|
|
|
*/ |
|
168
|
|
|
public function __sleep() |
|
169
|
|
|
{ |
|
170
|
18 |
|
unset($this->values); |
|
171
|
|
|
unset($this->context); |
|
172
|
18 |
|
unset($this->configuration); |
|
173
|
17 |
|
|
|
174
|
17 |
|
return array_keys(get_object_vars($this)); |
|
175
|
17 |
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @param array $values |
|
179
|
18 |
|
* @param array $configuration |
|
180
|
|
|
* @param string $name |
|
181
|
18 |
|
* |
|
182
|
|
|
* @return bool |
|
183
|
|
|
*/ |
|
184
|
|
|
private function fillFromArray(array $values, array $configuration, string $name) |
|
185
|
|
|
{ |
|
186
|
|
|
if (count($values) > 0 && isset($configuration['properties'][$name]) && isset($values[$name])) { |
|
187
|
|
|
$this->$name = $values[$name]; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
return true; |
|
191
|
|
|
} |
|
192
|
2 |
|
|
|
193
|
|
|
/** |
|
194
|
2 |
|
* Fill Meta from object. Object must have public getters for properties. |
|
195
|
2 |
|
* |
|
196
|
2 |
|
* @param mixed $values Object with public getters for properties |
|
197
|
2 |
|
* @param array $configuration |
|
198
|
2 |
|
* |
|
199
|
|
|
* @return bool |
|
200
|
|
|
*/ |
|
201
|
|
|
private function fillFromObject($values, array $configuration, string $name) |
|
202
|
|
|
{ |
|
203
|
2 |
|
if (isset($configuration['properties'][$name])) { |
|
204
|
|
|
$getterName = 'get'.ucfirst($name); |
|
205
|
|
|
if (method_exists($values, $getterName)) { |
|
206
|
|
|
$this->$name = $values->$getterName(); |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
unset($values, $configuration); |
|
211
|
|
|
|
|
212
|
|
|
return true; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Check if string is JSON. |
|
217
|
|
|
* |
|
218
|
|
|
* @param string |
|
219
|
|
|
* |
|
220
|
|
|
* @return bool |
|
221
|
|
|
*/ |
|
222
|
|
|
private function isJson($string) |
|
223
|
|
|
{ |
|
224
|
|
|
json_decode($string); |
|
225
|
|
|
|
|
226
|
|
|
return JSON_ERROR_NONE === json_last_error(); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* @param $value |
|
231
|
|
|
* |
|
232
|
|
|
* @return Meta |
|
233
|
|
|
*/ |
|
234
|
|
|
private function getValueOrMeta($value) |
|
235
|
|
|
{ |
|
236
|
|
|
if ($this->context->isSupported($value)) { |
|
237
|
|
|
return $this->context->getMetaForValue($value); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
return $value; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* @param string $name |
|
245
|
|
|
*/ |
|
246
|
|
|
private function __load(string $name) |
|
247
|
|
|
{ |
|
248
|
|
|
if (is_array($this->values)) { |
|
249
|
|
|
$this->fillFromArray($this->values, $this->configuration, $name); |
|
250
|
|
|
} elseif (is_string($this->values) && $this->isJson($this->values)) { |
|
251
|
|
|
$this->fillFromArray(json_decode($this->values, true), $this->configuration, $name); |
|
252
|
|
|
} elseif (is_object($this->values)) { |
|
253
|
|
|
$this->fillFromObject($this->values, $this->configuration, $name); |
|
254
|
|
|
} |
|
255
|
|
|
} |
|
256
|
|
|
} |
|
257
|
|
|
|