1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EveryPolitician\EveryPolitician; |
4
|
|
|
|
5
|
|
|
use \GuzzleHttp; |
6
|
|
|
use \League\Csv; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @property string|null $startDate |
10
|
|
|
* @property string|null $endDate |
11
|
|
|
* @property string $csvUrl |
12
|
|
|
*/ |
13
|
|
|
class LegislativePeriod |
14
|
|
|
{ |
15
|
|
|
public $id; |
16
|
|
|
public $name; |
17
|
|
|
public $slug; |
18
|
|
|
protected $legislature; |
19
|
|
|
protected $country; |
20
|
|
|
protected $legislativePeriodData; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Creates a new instance |
24
|
|
|
* |
25
|
|
|
* @param array $legislativePeriodData Popolo legislative period data |
26
|
|
|
* @param Legislature $legislature legislature for this legislative period |
27
|
|
|
* @param Country @country country for this legislative period |
28
|
|
|
*/ |
29
|
9 |
|
public function __construct($legislativePeriodData, $legislature, $country) |
30
|
|
|
{ |
31
|
9 |
|
$properties = ['id', 'name', 'slug']; |
32
|
9 |
|
foreach ($properties as $k) { |
33
|
9 |
|
$this->$k = $legislativePeriodData[$k]; |
34
|
3 |
|
} |
35
|
9 |
|
$this->legislature = $legislature; |
36
|
9 |
|
$this->country = $country; |
37
|
9 |
|
$this->legislativePeriodData = $legislativePeriodData; |
38
|
9 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Getter for public attributes |
42
|
|
|
* |
43
|
|
|
* @param string $prop the attribute to get |
44
|
|
|
* |
45
|
|
|
* @return mixed |
46
|
|
|
*/ |
47
|
9 |
|
public function __get($prop) |
48
|
|
|
{ |
49
|
9 |
|
if (in_array($prop, ['startDate', 'endDate', 'csvUrl'])) { |
50
|
9 |
|
$getter = 'get' . ucfirst($prop); |
51
|
9 |
|
return $this->$getter(); |
52
|
|
|
} |
53
|
|
|
trigger_error('Undefined property: '.__CLASS__.'::$'.$prop, E_USER_ERROR); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Return the start date of the legislative period |
58
|
|
|
* |
59
|
|
|
* If this is unknown, it returns null. |
60
|
|
|
* |
61
|
|
|
* @return string|null |
62
|
|
|
*/ |
63
|
3 |
|
private function getStartDate() |
64
|
|
|
{ |
65
|
3 |
|
if (array_key_exists('start_date', $this->legislativePeriodData)) { |
66
|
3 |
|
return $this->legislativePeriodData['start_date']; |
67
|
|
|
} |
68
|
|
|
return null; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Return the end date of the legislative period |
73
|
|
|
* |
74
|
|
|
* If this is unknown, it returns null. |
75
|
|
|
* |
76
|
|
|
* @return string|null |
77
|
|
|
*/ |
78
|
3 |
|
private function getEndDate() |
79
|
|
|
{ |
80
|
3 |
|
if (array_key_exists('end_date', $this->legislativePeriodData)) { |
81
|
|
|
return $this->legislativePeriodData['end_date']; |
82
|
|
|
} |
83
|
3 |
|
return null; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Return the URL to CSV of members during this legislative period |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
3 |
|
private function getCsvUrl() |
92
|
|
|
{ |
93
|
|
|
return 'https://raw.githubusercontent.com/everypolitician' |
94
|
3 |
|
.'/everypolitician-data/'.$this->legislature->sha |
95
|
3 |
|
.'/'.$this->legislativePeriodData['csv']; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Return parsed data from the CSV of members during the period |
100
|
|
|
* |
101
|
|
|
* This returns a list of one dict per row of the CSV file, where |
102
|
|
|
* the keys are the column headers. |
103
|
|
|
* |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
3 |
|
public function csv() |
107
|
|
|
{ |
108
|
3 |
|
$client = new GuzzleHttp\Client(); |
109
|
3 |
|
$response = $client->get($this->csvUrl); |
110
|
3 |
|
$reader = Csv\Reader::createFromString($response->getBody()); |
111
|
3 |
|
return iterator_to_array($reader->fetchAssoc(), false); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|