|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Coco\SourceWatcher\Core\Extractors; |
|
4
|
|
|
|
|
5
|
|
|
use Coco\SourceWatcher\Core\Extractor; |
|
6
|
|
|
use Coco\SourceWatcher\Core\Row; |
|
7
|
|
|
use Coco\SourceWatcher\Core\SourceWatcherException; |
|
8
|
|
|
use Flow\JSONPath\JSONPath; |
|
9
|
|
|
use Flow\JSONPath\JSONPathException; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class JsonExtractor |
|
13
|
|
|
* @package Coco\SourceWatcher\Core\Extractors |
|
14
|
|
|
*/ |
|
15
|
|
|
class JsonExtractor extends Extractor |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
private array $columns = []; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected array $availableOptions = [ "columns" ]; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @return array |
|
29
|
|
|
*/ |
|
30
|
|
|
public function getColumns () : array |
|
31
|
|
|
{ |
|
32
|
|
|
return $this->columns; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param array $columns |
|
37
|
|
|
*/ |
|
38
|
|
|
public function setColumns ( array $columns ) : void |
|
39
|
|
|
{ |
|
40
|
|
|
$this->columns = $columns; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return array|mixed |
|
45
|
|
|
* @throws JSONPathException |
|
46
|
|
|
* @throws SourceWatcherException |
|
47
|
|
|
*/ |
|
48
|
|
|
public function extract () |
|
49
|
|
|
{ |
|
50
|
|
|
if ( $this->input == null ) { |
|
51
|
|
|
throw new SourceWatcherException( "An input must be provided." ); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$result = array(); |
|
55
|
|
|
|
|
56
|
|
|
if ( !file_exists( $this->input ) ) { |
|
57
|
|
|
throw new SourceWatcherException( "The file " . $this->input . " could not be found." ); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$data = json_decode( file_get_contents( $this->input ), true ); |
|
61
|
|
|
|
|
62
|
|
|
if ( $this->columns ) { |
|
|
|
|
|
|
63
|
|
|
try { |
|
64
|
|
|
$jsonPath = new JSONPath( $data ); |
|
65
|
|
|
} catch ( JSONPathException $jsonPathException ) { |
|
66
|
|
|
throw new SourceWatcherException( "Something went wrong trying to extract the JSON file: " . $jsonPathException->getMessage() ); |
|
67
|
|
|
} catch ( Exception $exception ) { |
|
|
|
|
|
|
68
|
|
|
throw new SourceWatcherException( "Something unexpected went wrong trying to extract the JSON file: " . $exception->getMessage() ); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
foreach ( $this->columns as $key => $path ) { |
|
72
|
|
|
$this->columns[$key] = $jsonPath->find( $path )->data(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$data = $this->transpose( $this->columns ); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
foreach ( $data as $row ) { |
|
79
|
|
|
array_push( $result, new Row( $row ) ); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $result; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param $columns |
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
|
|
private function transpose ( $columns ) |
|
90
|
|
|
{ |
|
91
|
|
|
$data = []; |
|
92
|
|
|
|
|
93
|
|
|
foreach ( $columns as $column => $items ) { |
|
94
|
|
|
foreach ( $items as $row => $item ) { |
|
95
|
|
|
$data[$row][$column] = $item; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $data; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.