|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the json-query-wrapper package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Enrico Stahn <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace JsonQueryWrapper; |
|
13
|
|
|
|
|
14
|
|
|
use JsonQueryWrapper\DataProvider\DataProviderInterface; |
|
15
|
|
|
use JsonQueryWrapper\Exception\DataProviderMissingException; |
|
16
|
|
|
use Symfony\Component\Process\ProcessBuilder; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class JsonQuery. |
|
20
|
|
|
*/ |
|
21
|
|
|
class JsonQuery |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var ProcessBuilder |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $builder; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var DataTypeMapper |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $mapper; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var DataProviderInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $dataProvider; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Path to the jq command. |
|
40
|
|
|
* |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $cmd = 'jq'; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* JsonQuery constructor. |
|
47
|
|
|
* |
|
48
|
|
|
* @param ProcessBuilder $builder |
|
49
|
|
|
* @param DataTypeMapper $dataTypeMapper |
|
50
|
|
|
*/ |
|
51
|
3 |
|
public function __construct(ProcessBuilder $builder, DataTypeMapper $dataTypeMapper) |
|
52
|
|
|
{ |
|
53
|
3 |
|
$this->builder = $builder; |
|
54
|
3 |
|
$this->mapper = $dataTypeMapper; |
|
55
|
3 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Set the path to the jq command. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $cmd |
|
61
|
|
|
*/ |
|
62
|
|
|
public function setCmd($cmd) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->cmd = $cmd; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Runs the command-line and returns. |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $filter |
|
71
|
|
|
* |
|
72
|
|
|
* @throws DataProviderMissingException |
|
73
|
|
|
* |
|
74
|
|
|
* @return mixed |
|
75
|
|
|
*/ |
|
76
|
2 |
|
public function run($filter) |
|
77
|
|
|
{ |
|
78
|
2 |
|
if (!$this->dataProvider instanceof DataProviderInterface) { |
|
79
|
1 |
|
throw new DataProviderMissingException('A data provider such as file or text is missing.'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
$builder = $this->builder; |
|
83
|
|
|
$builder |
|
84
|
1 |
|
->setPrefix($this->cmd) |
|
85
|
1 |
|
->setArguments(['-M', $filter, $this->dataProvider->getPath()]); |
|
86
|
|
|
|
|
87
|
1 |
|
$process = $builder->getProcess(); |
|
88
|
1 |
|
$process->run(); |
|
89
|
|
|
|
|
90
|
1 |
|
$result = trim($process->getOutput()); |
|
91
|
|
|
|
|
92
|
1 |
|
return $this->mapper->map($result); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param DataProviderInterface $provider |
|
97
|
|
|
*/ |
|
98
|
2 |
|
public function setDataProvider(DataProviderInterface $provider) |
|
99
|
|
|
{ |
|
100
|
2 |
|
$this->dataProvider = $provider; |
|
101
|
2 |
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|