|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* JSON backend for Postgres using the {@link JSONText} DB field. Allows us to use Postgres JSON query syntax within |
|
5
|
|
|
* the module. |
|
6
|
|
|
* |
|
7
|
|
|
* @package silverstripe-jsontext |
|
8
|
|
|
* @subpackage models |
|
9
|
|
|
* @author Russell Michell <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace JSONText\Backends; |
|
13
|
|
|
|
|
14
|
|
|
use JSONText\Exceptions\JSONTextException; |
|
15
|
|
|
use JSONText\Fields\JSONText; |
|
16
|
|
|
|
|
17
|
|
|
class JSONBackend |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var mixed |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $key; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var mixed |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $val; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var int |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $idx; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $operand; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Not used. |
|
41
|
|
|
* |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $operator; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var JSONText |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $jsonText; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* PostgresJSONBackend constructor. |
|
53
|
|
|
* |
|
54
|
|
|
* @param mixed $key |
|
55
|
|
|
* @param mixed $val |
|
56
|
|
|
* @param int $idx |
|
57
|
|
|
* @param string $operand |
|
58
|
|
|
* @param JSONText $jsonText |
|
59
|
|
|
*/ |
|
60
|
|
|
public function __construct($key, $val, $idx, $operator, $operand, $jsonText) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->key = $key; |
|
63
|
|
|
$this->val = $val; |
|
64
|
|
|
$this->idx = $idx; |
|
65
|
|
|
$this->operator = $operator; |
|
66
|
|
|
$this->operand = $operand; |
|
67
|
|
|
$this->jsonText = $jsonText; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Match on keys by INT. |
|
72
|
|
|
* |
|
73
|
|
|
* @return array |
|
74
|
|
|
*/ |
|
75
|
|
|
public function matchIfKeyIsInt() |
|
76
|
|
|
{ |
|
77
|
|
|
if (is_int($this->operand) && $this->idx === $this->operand) { |
|
78
|
|
|
return [$this->key => $this->val]; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return []; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Match on keys by STRING. |
|
86
|
|
|
* |
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
|
|
public function matchIfKeyIsStr() |
|
90
|
|
|
{ |
|
91
|
|
|
// operand can be a numeric string if it wants here |
|
92
|
|
|
if (is_string($this->operand) && $this->key === $this->operand) { |
|
93
|
|
|
return [$this->key => $this->val]; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return []; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Match on path. |
|
101
|
|
|
* |
|
102
|
|
|
* @return array |
|
103
|
|
|
* @throws \JSONText\Exceptions\JSONTextException |
|
104
|
|
|
*/ |
|
105
|
|
|
public function matchOnPath() |
|
106
|
|
|
{ |
|
107
|
|
|
if (!is_string($this->operand) || !$this->jsonText->isJson($this->operand)) { |
|
108
|
|
|
$msg = 'Invalid JSON passed as operand on RHS.'; |
|
109
|
|
|
throw new JSONTextException($msg); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$operandAsArray = $this->jsonText->toArray($this->operand); |
|
113
|
|
|
|
|
114
|
|
|
// Empty is OK..could've been accidental... |
|
115
|
|
|
if (!count($operandAsArray)) { |
|
116
|
|
|
return []; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$keys = array_keys($operandAsArray); |
|
120
|
|
|
if (count($keys) >1) { |
|
121
|
|
|
$msg = 'Sorry. I can\'t handle complex operands.'; |
|
122
|
|
|
throw new JSONTextException($msg); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$vals = array_values($operandAsArray); |
|
126
|
|
|
if (count($vals) >1) { |
|
127
|
|
|
$msg = 'Sorry. I can\'t handle complex operands.'; |
|
128
|
|
|
throw new JSONTextException($msg); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
if ($this->key === $keys[0] && is_array($this->val) && !empty($this->val[$vals[0]])) { |
|
132
|
|
|
return $this->val[$vals[0]]; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return []; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
} |
|
139
|
|
|
|