|
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 PhpTek\JSONText\Backend; |
|
15
|
|
|
|
|
16
|
|
|
use PhpTek\JSONText\Exceptions\JSONTextInvalidArgsException; |
|
17
|
|
|
use PhpTek\JSONText\ORM\FieldType\JSONText; |
|
18
|
|
|
use SilverStripe\Core\Config\Configurable; |
|
19
|
|
|
|
|
20
|
|
|
abstract class JSONBackend |
|
21
|
|
|
{ |
|
22
|
|
|
use Configurable; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $operand; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var JSONText |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $jsonText; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* JSONBackend constructor. |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $operand |
|
38
|
|
|
* @param JSONText $jsonText |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct($operand, $jsonText) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->operand = $operand; |
|
43
|
|
|
$this->jsonText = $jsonText; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Match on keys by INT. If >1 matches are found, an indexed array of all matches is returned. |
|
48
|
|
|
* |
|
49
|
|
|
* @return array |
|
50
|
|
|
* @throws JSONTextInvalidArgsException |
|
51
|
|
|
*/ |
|
52
|
|
|
abstract public function matchOnInt(); |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Match on keys by STRING. If >1 matches are found, an indexed array of all matches is returned. |
|
56
|
|
|
* |
|
57
|
|
|
* @return array |
|
58
|
|
|
* @throws JSONTextInvalidArgsException |
|
59
|
|
|
*/ |
|
60
|
|
|
abstract public function matchOnStr(); |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Match on RDBMS-specific path operator. If >1 matches are found, an indexed array of all matches is returned. |
|
64
|
|
|
* |
|
65
|
|
|
* @return array |
|
66
|
|
|
* @throws JSONTextException |
|
67
|
|
|
*/ |
|
68
|
|
|
abstract public function matchOnPath(); |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Match on JSONPath expression. If >1 matches are found, an indexed array of all matches is returned. |
|
72
|
|
|
* |
|
73
|
|
|
* @return array |
|
74
|
|
|
* @throws JSONTextInvalidArgsException |
|
75
|
|
|
*/ |
|
76
|
|
|
public function matchOnExpr() |
|
77
|
|
|
{ |
|
78
|
|
|
if (!is_string($this->operand)) { |
|
79
|
|
|
$msg = 'Non-string operand passed to: ' . __FUNCTION__ . '()'; |
|
80
|
|
|
throw new JSONTextInvalidArgsException($msg); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// Re-use existing field passed via constructor |
|
84
|
|
|
$expr = $this->operand; |
|
85
|
|
|
$fetch = $this->jsonText->getJSONStore()->get($expr); |
|
86
|
|
|
if (empty($fetch)) { |
|
87
|
|
|
return []; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $fetch; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|