1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* JSON Query Wrapper |
4
|
|
|
* |
5
|
|
|
* (The MIT license) |
6
|
|
|
* Copyright (c) 2016 Enrico Stahn |
7
|
|
|
* |
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
9
|
|
|
* of this software and associated * documentation files (the "Software"), to |
10
|
|
|
* deal in the Software without restriction, including without limitation the |
11
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
12
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is |
13
|
|
|
* furnished to do so, subject to the following conditions: |
14
|
|
|
* |
15
|
|
|
* The above copyright notice and this permission notice shall be included in |
16
|
|
|
* all copies or substantial portions of the Software. |
17
|
|
|
* |
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
21
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
22
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
23
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
24
|
|
|
* IN THE SOFTWARE. |
25
|
|
|
* |
26
|
|
|
* @package JsonQueryWrapper |
27
|
|
|
* @link http://github.com/estahn/json-query-wrapper for the canonical source repository |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
namespace JsonQueryWrapper; |
31
|
|
|
|
32
|
|
|
use JsonQueryWrapper\DataProvider\DataProviderInterface; |
33
|
|
|
use JsonQueryWrapper\Exception\DataProviderMissingException; |
34
|
|
|
use Symfony\Component\Process\ProcessBuilder; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Class JsonQuery |
38
|
|
|
* |
39
|
|
|
* @package JsonQueryWrapper |
40
|
|
|
*/ |
41
|
|
|
class JsonQuery |
42
|
|
|
{ |
43
|
|
|
/** |
44
|
|
|
* @var ProcessBuilder |
45
|
|
|
*/ |
46
|
|
|
protected $builder; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var DataTypeMapper |
50
|
|
|
*/ |
51
|
|
|
protected $mapper; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var DataProviderInterface |
55
|
|
|
*/ |
56
|
|
|
protected $dataProvider; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Path to the jq command |
60
|
|
|
* |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
protected $cmd = 'jq'; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* JsonQuery constructor. |
67
|
|
|
* |
68
|
|
|
* @param ProcessBuilder $builder |
69
|
|
|
* @param DataTypeMapper $dataTypeMapper |
70
|
|
|
*/ |
71
|
3 |
|
public function __construct(ProcessBuilder $builder, DataTypeMapper $dataTypeMapper) |
72
|
|
|
{ |
73
|
3 |
|
$this->builder = $builder; |
74
|
3 |
|
$this->mapper = $dataTypeMapper; |
75
|
3 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Set the path to the jq command |
79
|
|
|
* |
80
|
|
|
* @param string $cmd |
81
|
|
|
*/ |
82
|
|
|
public function setCmd($cmd) |
83
|
|
|
{ |
84
|
|
|
$this->cmd = $cmd; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Runs the command-line and returns |
89
|
|
|
* |
90
|
|
|
* @param string $filter |
91
|
|
|
* @return mixed |
92
|
|
|
* @throws DataProviderMissingException |
93
|
|
|
*/ |
94
|
2 |
|
public function run($filter) |
95
|
|
|
{ |
96
|
2 |
|
if (!$this->dataProvider instanceof DataProviderInterface) { |
97
|
1 |
|
throw new DataProviderMissingException('A data provider such as file or text is missing.'); |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
$builder = $this->builder; |
101
|
|
|
$builder |
102
|
1 |
|
->setPrefix($this->cmd) |
103
|
1 |
|
->setArguments([$filter, $this->dataProvider->getPath()]); |
104
|
|
|
|
105
|
1 |
|
$process = $builder->getProcess(); |
106
|
1 |
|
$process->run(); |
107
|
|
|
|
108
|
1 |
|
$result = trim($process->getOutput()); |
109
|
|
|
|
110
|
1 |
|
return $this->mapper->map($result); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param DataProviderInterface $provider |
115
|
|
|
*/ |
116
|
2 |
|
public function setDataProvider(DataProviderInterface $provider) |
117
|
|
|
{ |
118
|
2 |
|
$this->dataProvider = $provider; |
119
|
2 |
|
} |
120
|
|
|
} |
121
|
|
|
|