|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Maps\DataAccess; |
|
4
|
|
|
|
|
5
|
|
|
use FileFetcher\FileFetcher; |
|
6
|
|
|
use FileFetcher\FileFetchingException; |
|
7
|
|
|
use Maps\MapsFactory; |
|
8
|
|
|
use ValueParsers\ParseException; |
|
9
|
|
|
use ValueParsers\ValueParser; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Returns the content of the JSON file at the specified location as array. |
|
13
|
|
|
* Empty array is returned on failure. |
|
14
|
|
|
* |
|
15
|
|
|
* @licence GNU GPL v2+ |
|
16
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
17
|
|
|
*/ |
|
18
|
|
|
class JsonFileParser implements ValueParser { |
|
19
|
|
|
|
|
20
|
|
|
private $fileFetcher; |
|
21
|
|
|
private $pageContentFetcher; |
|
22
|
|
|
private $defaultNamespace; |
|
23
|
|
|
|
|
24
|
7 |
|
public function __construct( $fileFetcher = null, PageContentFetcher $pageContentFetcher = null ) { |
|
25
|
7 |
|
$this->fileFetcher = $fileFetcher instanceof FileFetcher |
|
26
|
7 |
|
? $fileFetcher : MapsFactory::newDefault()->getFileFetcher(); |
|
27
|
|
|
|
|
28
|
7 |
|
$this->pageContentFetcher = $pageContentFetcher instanceof PageContentFetcher |
|
29
|
7 |
|
? $pageContentFetcher : MapsFactory::newDefault()->getPageContentFetcher(); |
|
30
|
|
|
|
|
31
|
7 |
|
$this->defaultNamespace = NS_GEO_JSON; |
|
32
|
7 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param string $fileLocation |
|
36
|
|
|
* |
|
37
|
|
|
* @return array |
|
38
|
|
|
* @throws ParseException |
|
39
|
|
|
*/ |
|
40
|
7 |
|
public function parse( $fileLocation ) { |
|
41
|
7 |
|
$jsonString = $this->getJsonString( $fileLocation ); |
|
42
|
|
|
|
|
43
|
7 |
|
if ( $jsonString === null ) { |
|
44
|
3 |
|
return []; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
4 |
|
$json = json_decode( $jsonString, true ); |
|
48
|
|
|
|
|
49
|
4 |
|
if ( $json === null ) { |
|
50
|
1 |
|
return []; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
3 |
|
return $json; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
7 |
|
private function getJsonString( string $fileLocation ): ?string { |
|
57
|
7 |
|
$content = $this->pageContentFetcher->getPageContent( $fileLocation, $this->defaultNamespace ); |
|
58
|
|
|
|
|
59
|
7 |
|
if ( $content instanceof \JsonContent ) { |
|
|
|
|
|
|
60
|
2 |
|
return $content->getNativeData(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
// Prevent reading JSON files on the server |
|
64
|
5 |
|
if( !filter_var( $fileLocation, FILTER_VALIDATE_URL) ) { |
|
65
|
2 |
|
return null; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
try { |
|
69
|
3 |
|
return $this->fileFetcher->fetchFile( $fileLocation ); |
|
70
|
|
|
} |
|
71
|
1 |
|
catch ( FileFetchingException $ex ) { |
|
72
|
1 |
|
return null; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.jsonfile (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.jsonto be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
requireorrequire-devsection?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceofchecks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.