|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Pixie\JSON; |
|
4
|
|
|
|
|
5
|
|
|
use Pixie\Exception; |
|
6
|
|
|
use Pixie\Connection; |
|
7
|
|
|
use Pixie\HasConnection; |
|
8
|
|
|
use Pixie\QueryBuilder\TablePrefixer; |
|
9
|
|
|
|
|
10
|
|
|
class JsonSelectorHandler implements HasConnection |
|
11
|
|
|
{ |
|
12
|
|
|
use TablePrefixer; |
|
13
|
|
|
|
|
14
|
|
|
/** @var Connection */ |
|
15
|
|
|
protected $connection; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct(Connection $connection) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->connection = $connection; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Returns the current connection instance. |
|
24
|
|
|
* |
|
25
|
|
|
* @return connection |
|
26
|
|
|
*/ |
|
27
|
|
|
public function getConnection(): Connection |
|
28
|
|
|
{ |
|
29
|
|
|
return $this->connection; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Checks if the passed expression is for JSON |
|
34
|
|
|
* this->denotes->json |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $expression |
|
37
|
|
|
* @return bool |
|
38
|
|
|
*/ |
|
39
|
|
|
public function isJsonSelector($expression): bool |
|
40
|
|
|
{ |
|
41
|
|
|
return is_string($expression) |
|
42
|
|
|
&& 2 <= count(array_diff(explode('->', $expression), array(""))); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Gets the column name form a potential array |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $expression |
|
49
|
|
|
* @return string |
|
50
|
|
|
* @throws Exception If invalid JSON Selector string passed. |
|
51
|
|
|
*/ |
|
52
|
|
|
public function getColumn(string $expression): string |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->toJsonSelector($expression)->getColumn(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Gets all JSON object keys while removing the column name. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $expression |
|
61
|
|
|
* @return string[] |
|
62
|
|
|
* @throws Exception If invalid JSON Selector string passed. |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getNodes(string $expression): array |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->toJsonSelector($expression)->getNodes(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Casts a valid JSON selector to a JsonSelector object. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $expression |
|
73
|
|
|
* @return JsonSelector |
|
74
|
|
|
* @throws Exception If invalid JSON Selector string passed. |
|
75
|
|
|
*/ |
|
76
|
|
|
public function toJsonSelector(string $expression): JsonSelector |
|
77
|
|
|
{ |
|
78
|
|
|
if (! $this->isJsonSelector($expression)) { |
|
79
|
|
|
throw new Exception('JSON expression must contain at least 2 values, the table column and at least 1 node.', 1); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** @var string[] Check done above. */ |
|
83
|
|
|
$parts = array_diff(explode('->', $expression), array("")); |
|
84
|
|
|
|
|
85
|
|
|
$column = array_shift($parts); |
|
86
|
|
|
$nodes = $parts; |
|
87
|
|
|
|
|
88
|
|
|
if (! is_string($column)) { |
|
89
|
|
|
throw new Exception('JSON expression must contain a valid column name', 1); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return new JsonSelector($column, $nodes); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|