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
|
|
|
} |
36
|
60 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* String representation of {@link EveryPolitician} |
40
|
|
|
* |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
9 |
|
public function __toString() |
44
|
|
|
{ |
45
|
9 |
|
$str = $this->url ?: $this->filename; |
46
|
9 |
|
return '<EveryPolitician: '.$str.'>'; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Return a new {@link EveryPolitician} from a filename |
51
|
|
|
* of a Popolo json file |
52
|
|
|
* |
53
|
|
|
* @param string $filename filename of a Popolo json file |
54
|
|
|
* |
55
|
|
|
* @return static |
56
|
|
|
*/ |
57
|
6 |
|
public static function fromFilename($filename) |
58
|
|
|
{ |
59
|
6 |
|
return new static($filename, static::TYPE_FILENAME); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Return a new {@link EveryPolitician} from a URL |
64
|
|
|
* pointing to a Popolo json file |
65
|
|
|
* |
66
|
|
|
* @param string $url URL pointing to a Popolo json file |
67
|
|
|
* |
68
|
|
|
* @return static |
69
|
|
|
*/ |
70
|
3 |
|
public static function fromUrl($url) |
71
|
|
|
{ |
72
|
3 |
|
return new static($url, static::TYPE_URL); |
73
|
|
|
} |
74
|
|
|
|
75
|
51 |
|
private function countriesJsonData() |
76
|
|
|
{ |
77
|
51 |
|
if (!is_null($this->countriesJsonData)) { |
78
|
3 |
|
return $this->countriesJsonData; |
79
|
|
|
} |
80
|
51 |
|
if (!is_null($this->filename)) { |
81
|
3 |
|
$f = file_get_contents($this->filename); |
82
|
3 |
|
$this->countriesJsonData = json_decode($f, true); |
83
|
1 |
|
} else { |
84
|
48 |
|
$client = new GuzzleHttp\Client(); |
85
|
48 |
|
$response = $client->get($this->url); |
86
|
48 |
|
$this->countriesJsonData = json_decode($response->getBody(), true); |
87
|
|
|
} |
88
|
51 |
|
return $this->countriesJsonData; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Return a list of all known {@link Country}s |
93
|
|
|
* |
94
|
|
|
* @return Country[] |
95
|
|
|
*/ |
96
|
51 |
|
public function countries() |
97
|
|
|
{ |
98
|
51 |
|
$countries = []; |
99
|
51 |
|
$countriesData = $this->countriesJsonData(); |
100
|
51 |
|
foreach ($countriesData as $countryData) { |
101
|
51 |
|
$countries[] = new Country($countryData); |
102
|
17 |
|
} |
103
|
51 |
|
return $countries; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Return a {@link Country} object from a country slug |
108
|
|
|
* |
109
|
|
|
* @param string $countrySlug slug identifying a country |
110
|
|
|
* |
111
|
|
|
* @return Country |
112
|
|
|
*/ |
113
|
42 |
|
public function country($countrySlug) |
114
|
|
|
{ |
115
|
42 |
|
foreach ($this->countries() as $c) { |
116
|
42 |
|
if ($c->slug == $countrySlug) { |
117
|
39 |
|
return $c; |
118
|
|
|
} |
119
|
12 |
|
} |
120
|
9 |
|
throw new Exceptions\NotFoundException("Couldn't find the country with slug '$countrySlug'"); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Return an array containing a {@link Country} and a |
125
|
|
|
* {@link Legislature} object from their slugs |
126
|
|
|
* |
127
|
|
|
* @param string $countrySlug slug identifying a country |
128
|
|
|
* @param string $legislatureSlug slug identifying a legislature |
129
|
|
|
* |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
12 |
|
public function countryLegislature($countrySlug, $legislatureSlug) |
133
|
|
|
{ |
134
|
12 |
|
$country = $this->country($countrySlug); |
135
|
6 |
|
$legislature = $country->legislature($legislatureSlug); |
136
|
3 |
|
return [$country, $legislature]; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|