1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* DB backend for use with the {@link JSONText} DB field. Allows us to use DB-specific JSON query syntax within |
5
|
|
|
* the module. |
6
|
|
|
* |
7
|
|
|
* @package silverstripe-jsontext |
8
|
|
|
* @subpackage models |
9
|
|
|
* @author Russell Michell <[email protected]> |
10
|
|
|
* @see https://github.com/Peekmo/JsonPath/blob/master/tests/JsonStoreTest.php |
11
|
|
|
* @see http://goessner.net/articles/JsonPath/ |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace JSONText\Backends; |
15
|
|
|
|
16
|
|
|
use JSONText\Exceptions\JSONTextException; |
17
|
|
|
use JSONText\Fields\JSONText; |
18
|
|
|
|
19
|
|
|
abstract class JSONBackend |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $operand; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var JSONText |
29
|
|
|
*/ |
30
|
|
|
protected $jsonText; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* JSONBackend constructor. |
34
|
|
|
* |
35
|
|
|
* @param string $operand |
36
|
|
|
* @param JSONText $jsonText |
37
|
|
|
*/ |
38
|
|
|
public function __construct($operand, $jsonText) |
39
|
|
|
{ |
40
|
|
|
$this->operand = $operand; |
41
|
|
|
$this->jsonText = $jsonText; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Match on keys by INT. If >1 matches are found, an indexed array of all matches is returned. |
46
|
|
|
* |
47
|
|
|
* @return array |
48
|
|
|
* @throws JSONTextException |
49
|
|
|
*/ |
50
|
|
|
abstract public function matchOnInt(); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Match on keys by STRING. If >1 matches are found, an indexed array of all matches is returned. |
54
|
|
|
* |
55
|
|
|
* @return array |
56
|
|
|
* @throws JSONTextException |
57
|
|
|
*/ |
58
|
|
|
abstract function matchOnStr(); |
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Match on RDBMS-specific path operator. If >1 matches are found, an indexed array of all matches is returned. |
62
|
|
|
* |
63
|
|
|
* @return array |
64
|
|
|
* @throws \JSONText\Exceptions\JSONTextException |
65
|
|
|
*/ |
66
|
|
|
abstract public function matchOnPath(); |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Match on JSONPath expression. If >1 matches are found, an indexed array of all matches is returned. |
70
|
|
|
* |
71
|
|
|
* @return array |
72
|
|
|
* @throws JSONTextException |
73
|
|
|
*/ |
74
|
|
|
public function matchOnExpr() |
75
|
|
|
{ |
76
|
|
|
if (!is_string($this->operand)) { |
77
|
|
|
$msg = 'Non-string passed to: ' . __FUNCTION__; |
78
|
|
|
throw new JSONTextException($msg); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// Re-use existing field passed via constructor |
82
|
|
|
$expr = $this->operand; |
83
|
|
|
$fetch = $this->jsonText->getJSONStore()->get($expr); |
84
|
|
|
if (!$fetch) { |
|
|
|
|
85
|
|
|
return []; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $fetch; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.