|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Simple text-based database field for storing and querying JSON structured data. |
|
5
|
|
|
* |
|
6
|
|
|
* JSON sub-structures can be queried in a variety of ways using special operators who's syntax closely mimics those used |
|
7
|
|
|
* in native JSON queries in PostGreSQL v9.2+. |
|
8
|
|
|
* |
|
9
|
|
|
* Note: The extraction techniques employed here are simple key / value comparisons. They do not use any native JSON |
|
10
|
|
|
* features of your project's underlying RDBMS, e.g. those found either in PostGreSQL >= v9.2 or MySQL >= v5.7. As such |
|
11
|
|
|
* any JSON "queries" you construct will never be as performant as a native implementation. |
|
12
|
|
|
* |
|
13
|
|
|
* Example definition via {@link DataObject::$db} static: |
|
14
|
|
|
* |
|
15
|
|
|
* <code> |
|
16
|
|
|
* static $db = [ |
|
17
|
|
|
* 'MyJSONStructure' => 'JSONText' |
|
18
|
|
|
* ]; |
|
19
|
|
|
* </code> |
|
20
|
|
|
* |
|
21
|
|
|
* @package silverstripe-jsontext |
|
22
|
|
|
* @subpackage fields |
|
23
|
|
|
* @author Russell Michell <[email protected]> |
|
24
|
|
|
* @todo Make the current default of "strict mode" into ss config and default to strict. |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
namespace JSONText\Fields; |
|
28
|
|
|
|
|
29
|
|
|
use JSONText\Exceptions\JSONTextException; |
|
30
|
|
|
|
|
31
|
|
|
class JSONText extends \StringField |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* Which RDBMS backend are we using? The value set here changes the actual operators and operator-routines for the |
|
35
|
|
|
* given backend. |
|
36
|
|
|
* |
|
37
|
|
|
* @var string |
|
38
|
|
|
* @config |
|
39
|
|
|
*/ |
|
40
|
|
|
private static $backend = 'postgres'; |
|
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var array |
|
44
|
|
|
* @config |
|
45
|
|
|
* |
|
46
|
|
|
* [<backend>] => [ |
|
47
|
|
|
* [<method> => <operator>] |
|
48
|
|
|
* ]; // For use in query() method. |
|
49
|
|
|
*/ |
|
50
|
|
|
private static $allowed_operators = [ |
|
|
|
|
|
|
51
|
|
|
'postgres' => [ |
|
52
|
|
|
'matchIfKeyIsInt' => '->', |
|
53
|
|
|
'matchIfKeyIsStr' => '->>', |
|
54
|
|
|
'matchOnPath' => '#>' |
|
55
|
|
|
] |
|
56
|
|
|
]; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var string |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $returnType = 'json'; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Object cache for performance improvements. |
|
65
|
|
|
* |
|
66
|
|
|
* @var \RecursiveIteratorIterator |
|
67
|
|
|
*/ |
|
68
|
|
|
protected $data; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @var array |
|
72
|
|
|
*/ |
|
73
|
|
|
protected $cache = []; |
|
74
|
|
|
|
|
75
|
|
|
public function updateCache($val) { |
|
76
|
|
|
$this->cache[] = $val; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Returns an input field. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $name |
|
83
|
|
|
* @param null|string $title |
|
84
|
|
|
* @param string $value |
|
85
|
|
|
*/ |
|
86
|
|
|
public function __construct($name, $title = null, $value = '') |
|
87
|
|
|
{ |
|
88
|
|
|
parent::__construct($name, $title, $value); |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Taken from {@link TextField}. |
|
93
|
|
|
* @see DBField::requireField() |
|
94
|
|
|
* @return void |
|
95
|
|
|
*/ |
|
96
|
|
|
public function requireField() |
|
97
|
|
|
{ |
|
98
|
|
|
$parts = [ |
|
99
|
|
|
'datatype' => 'mediumtext', |
|
100
|
|
|
'character set' => 'utf8', |
|
101
|
|
|
'collate' => 'utf8_general_ci', |
|
102
|
|
|
'arrayValue' => $this->arrayValue |
|
103
|
|
|
]; |
|
104
|
|
|
|
|
105
|
|
|
$values = [ |
|
106
|
|
|
'type' => 'text', |
|
107
|
|
|
'parts' => $parts |
|
108
|
|
|
]; |
|
109
|
|
|
|
|
110
|
|
|
DB::require_field($this->tableName, $this->name, $values, $this->default); |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param string $title |
|
115
|
|
|
* @return HiddenField |
|
116
|
|
|
*/ |
|
117
|
|
|
public function scaffoldSearchField($title = null) |
|
118
|
|
|
{ |
|
119
|
|
|
return HiddenField::create($this->getName()); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param string $title |
|
124
|
|
|
* @return HiddenField |
|
125
|
|
|
*/ |
|
126
|
|
|
public function scaffoldFormField($title = null) |
|
127
|
|
|
{ |
|
128
|
|
|
return HiddenField::create($this->getName()); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Tell all class methods to return data as JSON or an array. |
|
133
|
|
|
* |
|
134
|
|
|
* @param string $type |
|
135
|
|
|
* @return \JSONText |
|
136
|
|
|
* @throws \JSONText\Exceptions\JSONTextException |
|
137
|
|
|
*/ |
|
138
|
|
|
public function setReturnType($type) |
|
139
|
|
|
{ |
|
140
|
|
|
if (!in_array($type, ['json', 'array'])) { |
|
141
|
|
|
$msg = 'Bad type: ' . $type . ' passed to ' . __FUNCTION__; |
|
142
|
|
|
throw new JSONTextException($msg); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
$this->returnType = $type; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @return string |
|
150
|
|
|
*/ |
|
151
|
|
|
public function getReturnType() |
|
152
|
|
|
{ |
|
153
|
|
|
return $this->returnType; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Returns the value of this field as an iterable. |
|
158
|
|
|
* |
|
159
|
|
|
* @return \RecursiveIteratorIterator |
|
160
|
|
|
* @throws \JSONText\Exceptions\JSONTextException |
|
161
|
|
|
*/ |
|
162
|
|
|
public function getValueAsIterable() |
|
163
|
|
|
{ |
|
164
|
|
|
if (!$json = $this->getValue()) { |
|
165
|
|
|
return []; |
|
|
|
|
|
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
if (!$this->isJson($json)) { |
|
169
|
|
|
$msg = 'DB data is munged.'; |
|
170
|
|
|
throw new JSONTextException($msg); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
if (!$this->data) { |
|
174
|
|
|
$this->data = new \RecursiveIteratorIterator( |
|
175
|
|
|
new \RecursiveArrayIterator(json_decode($json, true)), |
|
176
|
|
|
\RecursiveIteratorIterator::SELF_FIRST |
|
177
|
|
|
); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
return $this->data; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Returns the value of this field as a flattened array |
|
185
|
|
|
* |
|
186
|
|
|
* @return array |
|
187
|
|
|
*/ |
|
188
|
|
|
public function getValueAsArray() |
|
189
|
|
|
{ |
|
190
|
|
|
return iterator_to_array($this->getValueAsIterable()); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Utility method to determine whether the data is really JSON or not. |
|
195
|
|
|
* |
|
196
|
|
|
* @param string $value |
|
197
|
|
|
* @return boolean |
|
198
|
|
|
*/ |
|
199
|
|
|
public function isJson($value) |
|
200
|
|
|
{ |
|
201
|
|
|
return !is_null(json_decode($value, true)); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* @param array $value |
|
206
|
|
|
* @return mixed null|string |
|
207
|
|
|
*/ |
|
208
|
|
|
public function toJson($value) |
|
209
|
|
|
{ |
|
210
|
|
|
if (!is_array($value)) { |
|
211
|
|
|
$value = (array) $value; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
$opts = ( |
|
215
|
|
|
JSON_UNESCAPED_SLASHES |
|
216
|
|
|
); |
|
217
|
|
|
|
|
218
|
|
|
return json_encode($value, $opts); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* @param mixed $value |
|
223
|
|
|
* @return array |
|
224
|
|
|
* @throws \JSONText\Exceptions\JSONTextException |
|
225
|
|
|
*/ |
|
226
|
|
|
public function toArray($value) |
|
227
|
|
|
{ |
|
228
|
|
|
$decode = json_decode($value, true); |
|
229
|
|
|
|
|
230
|
|
|
if (is_null($decode)) { |
|
231
|
|
|
$msg = 'Decoded JSON is invalid.'; |
|
232
|
|
|
throw new JSONTextException($msg); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
return $decode; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* Return an array of the JSON key + value represented as first (top-level) JSON node. |
|
240
|
|
|
* |
|
241
|
|
|
* @return array |
|
242
|
|
|
*/ |
|
243
|
|
|
public function first() |
|
244
|
|
|
{ |
|
245
|
|
|
$data = $this->getValueAsIterable(); |
|
246
|
|
|
|
|
247
|
|
|
if (!$data) { |
|
248
|
|
|
return $this->returnAsType([]); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
$flattened = iterator_to_array($data, true); |
|
252
|
|
|
return $this->returnAsType([ |
|
253
|
|
|
array_keys($flattened)[0] => array_values($flattened)[0] |
|
254
|
|
|
]); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* Return an array of the JSON key + value represented as last JSON node. |
|
259
|
|
|
* |
|
260
|
|
|
* @return array |
|
261
|
|
|
*/ |
|
262
|
|
|
public function last() |
|
263
|
|
|
{ |
|
264
|
|
|
$data = $this->getValueAsIterable(); |
|
265
|
|
|
|
|
266
|
|
|
if (!$data) { |
|
267
|
|
|
return $this->returnAsType([]); |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
$flattened = iterator_to_array($data, true); |
|
271
|
|
|
return $this->returnAsType([ |
|
272
|
|
|
array_keys($flattened)[count($flattened) -1] => array_values($flattened)[count($flattened) -1] |
|
273
|
|
|
]); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* Return an array of the JSON key + value represented as the $n'th JSON node. |
|
278
|
|
|
* |
|
279
|
|
|
* @param int $n |
|
280
|
|
|
* @return mixed array |
|
281
|
|
|
* @throws \JSONText\Exceptions\JSONTextException |
|
282
|
|
|
*/ |
|
283
|
|
|
public function nth($n) |
|
284
|
|
|
{ |
|
285
|
|
|
$data = $this->getValueAsIterable(); |
|
286
|
|
|
|
|
287
|
|
|
if (!$data) { |
|
288
|
|
|
return $this->returnAsType([]); |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
if (!is_int($n)) { |
|
292
|
|
|
$msg = 'Argument passed to ' . __FUNCTION__ . ' must be an integer.'; |
|
293
|
|
|
throw new JSONTextException($msg); |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
$i = 0; |
|
297
|
|
|
foreach ($data as $key => $val) { |
|
298
|
|
|
if ($i === $n) { |
|
299
|
|
|
return $this->returnAsType([$key => $val]); |
|
300
|
|
|
} |
|
301
|
|
|
$i++; |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
return $this->returnAsType($data); |
|
|
|
|
|
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
/** |
|
308
|
|
|
* Return an array of the JSON key(s) + value(s) represented by $operator extracting relevant result in a JSON |
|
309
|
|
|
* node's value. |
|
310
|
|
|
* |
|
311
|
|
|
* @param string $operator |
|
312
|
|
|
* @param string $operand |
|
313
|
|
|
* @return mixed null|array |
|
314
|
|
|
* @throws \JSONText\Exceptions\JSONTextException |
|
315
|
|
|
* @todo How to increment an interator for each depth using $data->getDepth() and $i ?? |
|
316
|
|
|
*/ |
|
317
|
|
|
public function query($operator, $operand) |
|
|
|
|
|
|
318
|
|
|
{ |
|
319
|
|
|
$data = $this->getValueAsIterable(); |
|
320
|
|
|
|
|
321
|
|
|
if (!$data) { |
|
322
|
|
|
return $this->returnAsType([]); |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
if (!$this->isValidOperator($operator)) { |
|
326
|
|
|
$msg = 'JSON operator: ' . $operator . ' is invalid.'; |
|
327
|
|
|
throw new JSONTextException($msg); |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
$i = 0; |
|
331
|
|
|
foreach ($data as $key => $val) { |
|
332
|
|
|
if ($marshalled = $this->marshallQuery($key, $val, $i, func_get_args())) { |
|
333
|
|
|
return $this->returnAsType($marshalled); |
|
334
|
|
|
} |
|
335
|
|
|
|
|
336
|
|
|
$i++; |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
return $this->returnAsType([]); |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
/** |
|
343
|
|
|
* Alias of self::query(). |
|
344
|
|
|
* |
|
345
|
|
|
* @param string $operator |
|
346
|
|
|
* @return mixed string|array |
|
347
|
|
|
* @throws \JSONText\Exceptions\JSONTextException |
|
348
|
|
|
*/ |
|
349
|
|
|
public function extract($operator) |
|
350
|
|
|
{ |
|
351
|
|
|
return $this->extract($operator); |
|
352
|
|
|
} |
|
353
|
|
|
|
|
354
|
|
|
/** |
|
355
|
|
|
* @param mixed $key |
|
356
|
|
|
* @param mixed $val |
|
357
|
|
|
* @param int $idx |
|
358
|
|
|
* @param array $args |
|
359
|
|
|
* @return array |
|
360
|
|
|
* @throws \JSONText\Exceptions\JSONTextException |
|
361
|
|
|
*/ |
|
362
|
|
|
private function marshallQuery($key, $val, $idx, $args) |
|
363
|
|
|
{ |
|
364
|
|
|
$backend = $this->config()->backend; |
|
365
|
|
|
$operator = $args[0]; |
|
366
|
|
|
$operand = $args[1]; |
|
367
|
|
|
$operators = $this->config()->allowed_operators[$backend]; |
|
368
|
|
|
|
|
369
|
|
|
if (!in_array($operator, $operators)) { |
|
370
|
|
|
$msg = 'Invalid ' . $backend . ' operator: ' . $operator . ', used for JSON query.'; |
|
371
|
|
|
throw new JSONTextException($msg); |
|
372
|
|
|
} |
|
373
|
|
|
|
|
374
|
|
|
foreach ($operators as $routine => $backendOperator) { |
|
375
|
|
|
$backendDBApiInst = \Injector::inst()->createWithArgs( |
|
376
|
|
|
'\JSONText\Backends\JSONBackend', [ |
|
377
|
|
|
$key, |
|
378
|
|
|
$val, |
|
379
|
|
|
$idx, |
|
380
|
|
|
$backendOperator, |
|
381
|
|
|
$operand, |
|
382
|
|
|
$this |
|
383
|
|
|
]); |
|
384
|
|
|
|
|
385
|
|
|
if ($operator === $backendOperator && $result = $backendDBApiInst->$routine()) { |
|
386
|
|
|
return $result; |
|
387
|
|
|
} |
|
388
|
|
|
} |
|
389
|
|
|
|
|
390
|
|
|
return []; |
|
391
|
|
|
} |
|
392
|
|
|
|
|
393
|
|
|
/** |
|
394
|
|
|
* @param array $data |
|
395
|
|
|
* @return mixed |
|
396
|
|
|
*/ |
|
397
|
|
|
private function returnAsType(array $data) |
|
398
|
|
|
{ |
|
399
|
|
|
if (($this->getReturnType() === 'array')) { |
|
400
|
|
|
return $data; |
|
401
|
|
|
} |
|
402
|
|
|
|
|
403
|
|
|
if (($this->getReturnType() === 'json')) { |
|
404
|
|
|
return $this->toJson($data); |
|
405
|
|
|
} |
|
406
|
|
|
} |
|
407
|
|
|
|
|
408
|
|
|
/** |
|
409
|
|
|
* Is the passed JSON operator valid? |
|
410
|
|
|
* |
|
411
|
|
|
* @param string $operator |
|
412
|
|
|
* @return boolean |
|
413
|
|
|
*/ |
|
414
|
|
|
private function isValidOperator($operator) |
|
415
|
|
|
{ |
|
416
|
|
|
$backend = $this->config()->backend; |
|
417
|
|
|
|
|
418
|
|
|
return $operator && in_array($operator, $this->config()->allowed_operators[$backend], true); |
|
419
|
|
|
} |
|
420
|
|
|
|
|
421
|
|
|
} |
|
422
|
|
|
|
|
423
|
|
|
/** |
|
424
|
|
|
* @package silverstripe-advancedcontent |
|
425
|
|
|
* @author Russell Michell 2016 <[email protected]> |
|
426
|
|
|
*/ |
|
427
|
|
|
|
|
428
|
|
|
namespace JSONText\Exceptions; |
|
429
|
|
|
|
|
430
|
|
|
class JSONTextException extends \Exception |
|
|
|
|
|
|
431
|
|
|
{ |
|
432
|
|
|
} |
|
433
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.