|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EveryPolitician\EveryPolitician; |
|
4
|
|
|
|
|
5
|
|
|
class EveryPolitician |
|
6
|
|
|
{ |
|
7
|
|
|
private $countriesJsonFilename; |
|
8
|
|
|
private $countriesJsonUrl; |
|
9
|
|
|
private $countriesJsonData; |
|
10
|
|
|
|
|
11
|
|
|
const DEFAULT_COUNTRIES_JSON_URL = 'https://raw.githubusercontent.com/' |
|
12
|
|
|
.'everypolitician/everypolitician-data/master/countries.json'; |
|
13
|
|
|
|
|
14
|
36 |
|
public function __construct($options = []) |
|
15
|
|
|
{ |
|
16
|
36 |
|
if (!array_key_exists('filename', $options)) { |
|
17
|
30 |
|
if (array_key_exists('url', $options)) { |
|
18
|
3 |
|
$this->countriesJsonUrl = $options['url']; |
|
19
|
2 |
|
} else { |
|
20
|
28 |
|
$this->countriesJsonUrl = self::DEFAULT_COUNTRIES_JSON_URL; |
|
21
|
|
|
} |
|
22
|
20 |
|
} else { |
|
23
|
6 |
|
$this->countriesJsonFilename = $options['filename']; |
|
24
|
|
|
} |
|
25
|
36 |
|
} |
|
26
|
|
|
|
|
27
|
9 |
|
public function __toString() |
|
28
|
|
|
{ |
|
29
|
9 |
|
$body = !is_null($this->countriesJsonUrl) ? $this->countriesJsonUrl : $this->countriesJsonFilename; |
|
30
|
9 |
|
return "<EveryPolitician: $body>"; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Construct from filename |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $filename name of Popolo json file |
|
37
|
|
|
* |
|
38
|
|
|
* @return $this |
|
39
|
|
|
*/ |
|
40
|
6 |
|
public static function fromFilename($filename) |
|
41
|
|
|
{ |
|
42
|
6 |
|
return new self(['filename' => $filename]); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Construct from URL |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $url location of Popolo json file |
|
49
|
|
|
* |
|
50
|
|
|
* @return $this |
|
51
|
|
|
*/ |
|
52
|
3 |
|
public static function fromUrl($url) |
|
53
|
|
|
{ |
|
54
|
3 |
|
return new self(['url' => $url]); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
27 |
|
private function getCountriesJsonData() |
|
58
|
|
|
{ |
|
59
|
27 |
|
if (!is_null($this->countriesJsonData)) { |
|
60
|
3 |
|
return $this->countriesJsonData; |
|
61
|
|
|
} |
|
62
|
27 |
|
if (!is_null($this->countriesJsonFilename)) { |
|
63
|
3 |
|
$f = file_get_contents($this->countriesJsonFilename); |
|
64
|
3 |
|
$this->countriesJsonData = json_decode($f, true); |
|
65
|
2 |
|
} else { |
|
66
|
24 |
|
$client = new \GuzzleHttp\Client(); |
|
67
|
24 |
|
$response = $client->get($this->countriesJsonUrl); |
|
68
|
24 |
|
$this->countriesJsonData = json_decode($response->getBody(), true); |
|
69
|
|
|
} |
|
70
|
27 |
|
return $this->countriesJsonData; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Return a list of all known countries |
|
75
|
|
|
*/ |
|
76
|
27 |
|
public function countries() |
|
77
|
|
|
{ |
|
78
|
27 |
|
$countries = []; |
|
79
|
27 |
|
$countriesData = $this->getCountriesJsonData(); |
|
80
|
27 |
|
foreach ($countriesData as $countryData) { |
|
81
|
27 |
|
$countries[] = new Country($countryData); |
|
82
|
18 |
|
} |
|
83
|
27 |
|
return $countries; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Return a Country object from a country slug |
|
88
|
|
|
*/ |
|
89
|
18 |
|
public function country($countrySlug) |
|
90
|
|
|
{ |
|
91
|
18 |
|
foreach ($this->countries() as $c) { |
|
92
|
18 |
|
if ($c->slug == $countrySlug) { |
|
93
|
12 |
|
return $c; |
|
94
|
|
|
} |
|
95
|
12 |
|
} |
|
96
|
9 |
|
throw new Exceptions\NotFoundException("Couldn't find the country with slug '$countrySlug'"); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Return an array of Country and Legislature objects from their slugs |
|
101
|
|
|
*/ |
|
102
|
12 |
|
public function countryLegislature($countrySlug, $legislatureSlug) |
|
103
|
|
|
{ |
|
104
|
12 |
|
$country = $this->country($countrySlug); |
|
105
|
6 |
|
$legislature = $country->legislature($legislatureSlug); |
|
106
|
3 |
|
return [$country, $legislature]; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|