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