|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EveryPolitician\EveryPolitician; |
|
4
|
|
|
|
|
5
|
|
|
use \GuzzleHttp; |
|
6
|
|
|
|
|
7
|
|
|
class EveryPolitician |
|
8
|
|
|
{ |
|
9
|
|
|
private $url; |
|
10
|
|
|
private $filename; |
|
11
|
|
|
private $countriesJsonData; |
|
12
|
|
|
|
|
13
|
|
|
const TYPE_FILENAME = 0; |
|
14
|
|
|
const TYPE_URL = 1; |
|
15
|
|
|
const DEFAULT_COUNTRIES_JSON_URL = 'https://raw.githubusercontent.com/' |
|
16
|
|
|
.'everypolitician/everypolitician-data/master/countries.json'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Creates a new instance |
|
20
|
|
|
* |
|
21
|
|
|
* @param string $urlOrFilename URL or filename of Popolo json |
|
22
|
|
|
* @param int $type the delimiters to consider |
|
23
|
|
|
*/ |
|
24
|
60 |
|
public function __construct($urlOrFilename = self::DEFAULT_COUNTRIES_JSON_URL, $type = self::TYPE_URL) |
|
25
|
|
|
{ |
|
26
|
|
|
switch ($type) { |
|
27
|
60 |
|
case static::TYPE_FILENAME: |
|
28
|
6 |
|
$this->filename = $urlOrFilename; |
|
29
|
6 |
|
break; |
|
30
|
54 |
|
case static::TYPE_URL: |
|
31
|
54 |
|
$this->url = $urlOrFilename; |
|
32
|
54 |
|
break; |
|
33
|
|
|
default: |
|
34
|
|
|
throw new \Exception('Type must be TYPE_URL or TYPE_FILENAME'); |
|
35
|
|
|
break; |
|
|
|
|
|
|
36
|
|
|
} |
|
37
|
60 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* String representation of {@link EveryPolitician} |
|
41
|
|
|
* |
|
42
|
|
|
* @return string |
|
43
|
|
|
*/ |
|
44
|
9 |
|
public function __toString() |
|
45
|
|
|
{ |
|
46
|
9 |
|
$str = $this->url ?: $this->filename; |
|
47
|
9 |
|
return '<EveryPolitician: '.$str.'>'; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Return a new {@link EveryPolitician} from a filename |
|
52
|
|
|
* of a Popolo json file |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $filename filename of a Popolo json file |
|
55
|
|
|
* |
|
56
|
|
|
* @return static |
|
57
|
|
|
*/ |
|
58
|
6 |
|
public static function fromFilename($filename) |
|
59
|
|
|
{ |
|
60
|
6 |
|
return new static($filename, static::TYPE_FILENAME); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Return a new {@link EveryPolitician} from a URL |
|
65
|
|
|
* pointing to a Popolo json file |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $url URL pointing to a Popolo json file |
|
68
|
|
|
* |
|
69
|
|
|
* @return static |
|
70
|
|
|
*/ |
|
71
|
3 |
|
public static function fromUrl($url) |
|
72
|
|
|
{ |
|
73
|
3 |
|
return new static($url, static::TYPE_URL); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
51 |
|
private function countriesJsonData() |
|
77
|
|
|
{ |
|
78
|
51 |
|
if (!is_null($this->countriesJsonData)) { |
|
79
|
3 |
|
return $this->countriesJsonData; |
|
80
|
|
|
} |
|
81
|
51 |
|
if (!is_null($this->filename)) { |
|
82
|
3 |
|
$f = file_get_contents($this->filename); |
|
83
|
3 |
|
$this->countriesJsonData = json_decode($f, true); |
|
84
|
1 |
|
} else { |
|
85
|
48 |
|
$client = new GuzzleHttp\Client(); |
|
86
|
48 |
|
$response = $client->get($this->url); |
|
87
|
48 |
|
$this->countriesJsonData = json_decode($response->getBody(), true); |
|
88
|
|
|
} |
|
89
|
51 |
|
return $this->countriesJsonData; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Return a list of all known {@link Country}s |
|
94
|
|
|
* |
|
95
|
|
|
* @return Country[] |
|
96
|
|
|
*/ |
|
97
|
51 |
|
public function countries() |
|
98
|
|
|
{ |
|
99
|
51 |
|
$countries = []; |
|
100
|
51 |
|
$countriesData = $this->countriesJsonData(); |
|
101
|
51 |
|
foreach ($countriesData as $countryData) { |
|
102
|
51 |
|
$countries[] = new Country($countryData); |
|
103
|
17 |
|
} |
|
104
|
51 |
|
return $countries; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Return a {@link Country} object from a country slug |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $countrySlug slug identifying a country |
|
111
|
|
|
* |
|
112
|
|
|
* @return Country |
|
113
|
|
|
*/ |
|
114
|
42 |
|
public function country($countrySlug) |
|
115
|
|
|
{ |
|
116
|
42 |
|
foreach ($this->countries() as $c) { |
|
117
|
42 |
|
if ($c->slug == $countrySlug) { |
|
118
|
39 |
|
return $c; |
|
119
|
|
|
} |
|
120
|
12 |
|
} |
|
121
|
9 |
|
throw new Exceptions\NotFoundException("Couldn't find the country with slug '$countrySlug'"); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Return an array containing a {@link Country} and a |
|
126
|
|
|
* {@link Legislature} object from their slugs |
|
127
|
|
|
* |
|
128
|
|
|
* @param string $countrySlug slug identifying a country |
|
129
|
|
|
* @param string $legislatureSlug slug identifying a legislature |
|
130
|
|
|
* |
|
131
|
|
|
* @return array |
|
132
|
|
|
*/ |
|
133
|
12 |
|
public function countryLegislature($countrySlug, $legislatureSlug) |
|
134
|
|
|
{ |
|
135
|
12 |
|
$country = $this->country($countrySlug); |
|
136
|
6 |
|
$legislature = $country->legislature($legislatureSlug); |
|
137
|
3 |
|
return [$country, $legislature]; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return,dieorexitstatements that have been added for debug purposes.In the above example, the last
return falsewill never be executed, because a return statement has already been met in every possible execution path.