1 | <?php |
||
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) |
|
76 | |||
77 | /** |
||
78 | * Set the path to the jq command |
||
79 | * |
||
80 | * @param string $cmd |
||
81 | */ |
||
82 | public function setCmd($cmd) |
||
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) |
|
120 | } |
||
121 |