|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EveryPolitician\EveryPolitician; |
|
4
|
|
|
|
|
5
|
|
|
use \GuzzleHttp; |
|
6
|
|
|
use \League\Csv; |
|
7
|
|
|
|
|
8
|
|
|
class LegislativePeriod |
|
9
|
|
|
{ |
|
10
|
|
|
public $id; |
|
11
|
|
|
public $name; |
|
12
|
|
|
public $slug; |
|
13
|
|
|
protected $legislature; |
|
14
|
|
|
protected $country; |
|
15
|
|
|
protected $legislativePeriodData; |
|
16
|
|
|
|
|
17
|
8 |
|
public function __construct($legislativePeriodData, $legislature, $country) |
|
18
|
|
|
{ |
|
19
|
8 |
|
$properties = ['id', 'name', 'slug']; |
|
20
|
8 |
|
foreach ($properties as $k) { |
|
21
|
8 |
|
$this->$k = $legislativePeriodData[$k]; |
|
22
|
5 |
|
} |
|
23
|
8 |
|
$this->legislature = $legislature; |
|
24
|
8 |
|
$this->country = $country; |
|
25
|
8 |
|
$this->legislativePeriodData = $legislativePeriodData; |
|
26
|
8 |
|
} |
|
27
|
|
|
|
|
28
|
8 |
|
public function __get($prop) |
|
29
|
|
|
{ |
|
30
|
8 |
|
if (in_array($prop, ['startDate', 'endDate', 'csvUrl'])) { |
|
31
|
8 |
|
$getter = 'get' . ucfirst($prop); |
|
32
|
8 |
|
return $this->$getter(); |
|
33
|
|
|
} |
|
34
|
|
|
trigger_error('Undefined property: '.__CLASS__.'::$'.$prop, E_USER_ERROR); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Return the start date of the legislative period |
|
39
|
|
|
* |
|
40
|
|
|
* If this is unknown, it returns null. |
|
41
|
|
|
*/ |
|
42
|
3 |
|
private function getStartDate() |
|
43
|
|
|
{ |
|
44
|
3 |
|
if (array_key_exists('start_date', $this->legislativePeriodData)) { |
|
45
|
3 |
|
return $this->legislativePeriodData['start_date']; |
|
46
|
|
|
} |
|
47
|
|
|
return null; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Return the end date of the legislative period |
|
52
|
|
|
* |
|
53
|
|
|
* If this is unknown, it returns null. |
|
54
|
|
|
*/ |
|
55
|
3 |
|
private function getEndDate() |
|
56
|
|
|
{ |
|
57
|
3 |
|
if (array_key_exists('end_date', $this->legislativePeriodData)) { |
|
58
|
|
|
return $this->legislativePeriodData['end_date']; |
|
59
|
|
|
} |
|
60
|
3 |
|
return null; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Return the URL to CSV of members during this legislative period |
|
65
|
|
|
*/ |
|
66
|
2 |
|
private function getCsvUrl() |
|
67
|
|
|
{ |
|
68
|
|
|
return 'https://raw.githubusercontent.com/everypolitician' |
|
69
|
2 |
|
.'/everypolitician-data/'.$this->legislature->sha |
|
70
|
2 |
|
.'/'.$this->legislativePeriodData['csv']; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Return parsed data from the CSV of members during the period |
|
75
|
|
|
* |
|
76
|
|
|
* This returns a list of one dict per row of the CSV file, where |
|
77
|
|
|
* the keys are the column headers. |
|
78
|
|
|
*/ |
|
79
|
2 |
|
public function csv() |
|
80
|
|
|
{ |
|
81
|
2 |
|
$client = new GuzzleHttp\Client(); |
|
82
|
2 |
|
$response = $client->get($this->csvUrl); |
|
|
|
|
|
|
83
|
2 |
|
$reader = Csv\Reader::createFromString($response->getBody()); |
|
84
|
2 |
|
return iterator_to_array($reader->fetchAssoc(), false); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.