|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Coco\SourceWatcher\Core\Extractors; |
|
4
|
|
|
|
|
5
|
|
|
use Coco\SourceWatcher\Core\Extractor; |
|
6
|
|
|
use Coco\SourceWatcher\Core\IO\Inputs\FileInput; |
|
7
|
|
|
use Coco\SourceWatcher\Core\Row; |
|
8
|
|
|
use Coco\SourceWatcher\Core\SourceWatcherException; |
|
9
|
|
|
use Coco\SourceWatcher\Utils\Internationalization; |
|
10
|
|
|
use Flow\JSONPath\JSONPath; |
|
11
|
|
|
use Flow\JSONPath\JSONPathException; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class JsonExtractor |
|
15
|
|
|
* |
|
16
|
|
|
* @package Coco\SourceWatcher\Core\Extractors |
|
17
|
|
|
*/ |
|
18
|
|
|
class JsonExtractor extends Extractor |
|
19
|
|
|
{ |
|
20
|
|
|
protected array $columns = []; |
|
21
|
|
|
|
|
22
|
|
|
protected array $availableOptions = [ "columns" ]; |
|
23
|
|
|
|
|
24
|
|
|
public function getColumns () : array |
|
25
|
|
|
{ |
|
26
|
|
|
return $this->columns; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function setColumns ( array $columns ) : void |
|
30
|
|
|
{ |
|
31
|
|
|
$this->columns = $columns; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return array |
|
36
|
|
|
* @throws SourceWatcherException |
|
37
|
|
|
*/ |
|
38
|
|
|
public function extract () : array |
|
39
|
|
|
{ |
|
40
|
|
|
if ( $this->input == null ) { |
|
41
|
|
|
throw new SourceWatcherException( Internationalization::getInstance()->getText( JsonExtractor::class, |
|
42
|
|
|
"No_Input_Provided" ) ); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$inputIsFileInput = $this->input instanceof FileInput; |
|
46
|
|
|
|
|
47
|
|
|
if ( !$inputIsFileInput ) { |
|
48
|
|
|
throw new SourceWatcherException( sprintf( Internationalization::getInstance()->getText( JsonExtractor::class, |
|
49
|
|
|
"Input_Not_Instance_Of_File_Input" ), FileInput::class ) ); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$this->result = []; |
|
53
|
|
|
|
|
54
|
|
|
if ( !file_exists( $this->input->getInput() ) ) { |
|
55
|
|
|
throw new SourceWatcherException( sprintf( Internationalization::getInstance()->getText( JsonExtractor::class, |
|
56
|
|
|
"File_Input_File_Not_Found" ), $this->input->getInput() ) ); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$data = json_decode( file_get_contents( $this->input->getInput() ), true ); |
|
60
|
|
|
|
|
61
|
|
|
if ( $this->columns ) { |
|
|
|
|
|
|
62
|
|
|
$jsonPath = new JSONPath( $data ); |
|
63
|
|
|
|
|
64
|
|
|
try { |
|
65
|
|
|
foreach ( $this->columns as $key => $path ) { |
|
66
|
|
|
$this->columns[$key] = $jsonPath->find( $path )->getData(); |
|
67
|
|
|
} |
|
68
|
|
|
} catch ( JSONPathException $jsonPathException ) { |
|
69
|
|
|
throw new SourceWatcherException( sprintf( Internationalization::getInstance()->getText( JsonExtractor::class, |
|
70
|
|
|
"JSON_Path_Exception" ), $jsonPathException->getMessage() ) ); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$data = $this->transpose( $this->columns ); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
foreach ( $data as $row ) { |
|
77
|
|
|
array_push( $this->result, new Row( $row ) ); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $this->result; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
private function transpose ( $columns ) : array |
|
84
|
|
|
{ |
|
85
|
|
|
$data = []; |
|
86
|
|
|
|
|
87
|
|
|
foreach ( $columns as $column => $items ) { |
|
88
|
|
|
foreach ( $items as $row => $item ) { |
|
89
|
|
|
$data[$row][$column] = $item; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $data; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
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.