1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace mySociety\EveryPoliticianPopolo\Objects; |
4
|
|
|
|
5
|
|
|
class PopoloObject |
6
|
|
|
{ |
7
|
|
|
use \mySociety\EveryPoliticianPopolo\Traits\ArrayGetterTrait; |
8
|
|
|
|
9
|
|
|
protected $properties = []; |
10
|
|
|
protected $baseProperties = [ |
11
|
|
|
'keyForHash', |
12
|
|
|
]; |
13
|
|
|
protected $data; |
14
|
|
|
protected $allPopolo; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* |
18
|
|
|
*/ |
19
|
255 |
|
public function __construct($data, $allPopolo) |
20
|
|
|
{ |
21
|
255 |
|
$this->data = $data; |
22
|
255 |
|
$this->allPopolo = $allPopolo; |
23
|
255 |
|
} |
24
|
|
|
|
25
|
255 |
View Code Duplication |
public function __get($prop) |
|
|
|
|
26
|
|
|
{ |
27
|
255 |
|
$properties = array_merge($this->baseProperties, $this->properties); |
28
|
255 |
|
if (in_array($prop, $properties)) { |
29
|
255 |
|
$getter = 'get' . ucfirst($prop); |
30
|
255 |
|
return $this->$getter(); |
31
|
|
|
} |
32
|
3 |
|
trigger_error('Undefined property: '.__CLASS__.'::$'.$prop, E_USER_ERROR); |
33
|
|
|
} |
34
|
|
|
|
35
|
222 |
|
protected function getKeyForHash() |
36
|
2 |
|
{ |
37
|
222 |
|
return $this->getId(); |
38
|
|
|
} |
39
|
|
|
|
40
|
2 |
|
protected function getId() |
41
|
|
|
{ |
42
|
|
|
return null; |
43
|
2 |
|
} |
44
|
|
|
|
45
|
27 |
|
private function getRelatedObjects($popoloArray) |
46
|
|
|
{ |
47
|
27 |
|
return $this->arrGet($this->data, $popoloArray, []); |
48
|
|
|
} |
49
|
|
|
|
50
|
35 |
|
protected function getDate($attr, $default = null) |
51
|
|
|
{ |
52
|
33 |
|
$d = $this->arrGet($this->data, $attr); |
53
|
35 |
|
if ($d) { |
54
|
30 |
|
return new \DateTime($d); |
55
|
|
|
} |
56
|
3 |
|
return $default; |
57
|
|
|
} |
58
|
|
|
|
59
|
27 |
|
private function getRelatedValues($popoloArray, $infoTypeKey, $infoType, $infoValueKey) |
60
|
|
|
{ |
61
|
|
|
/* Get a value from one of the Popolo related objects |
62
|
|
|
* |
63
|
|
|
* For example, if you have a person with related links, like |
64
|
|
|
* this: |
65
|
|
|
* |
66
|
|
|
* { |
67
|
|
|
* "name": "Dale Cooper", |
68
|
|
|
* "links": [ |
69
|
|
|
* { |
70
|
|
|
* "note": "wikipedia", |
71
|
|
|
* "url": "https://en.wikipedia.org/wiki/Dale_Cooper" |
72
|
|
|
* } |
73
|
|
|
* ] |
74
|
|
|
* } |
75
|
|
|
* |
76
|
|
|
* When calling this method to get the Wikipedia URL, you would use: |
77
|
|
|
* |
78
|
|
|
* popoloArray = 'links' |
79
|
|
|
* infoTypeKey = 'note' |
80
|
|
|
* infoType = 'wikipedia' |
81
|
|
|
* infoValueKey = 'url' |
82
|
|
|
* |
83
|
|
|
* ... so the following would work: |
84
|
|
|
* |
85
|
|
|
* $this->getRelatedValue('links', 'note', 'wikipedia', 'url') |
86
|
|
|
* # => 'https://en.wikipedia.org/wiki/Dale_Cooper' |
87
|
|
|
*/ |
88
|
27 |
|
$relatedValues = []; |
89
|
27 |
|
$relatedObjects = $this->getRelatedObjects($popoloArray); |
90
|
27 |
|
foreach ($relatedObjects as $o) { |
91
|
21 |
|
if ($o[$infoTypeKey] === $infoType) { |
92
|
21 |
|
$relatedValues[] = $o[$infoValueKey]; |
93
|
16 |
|
} |
94
|
18 |
|
} |
95
|
27 |
|
return $relatedValues; |
96
|
|
|
} |
97
|
|
|
|
98
|
12 |
|
public function identifierValues($scheme) |
99
|
|
|
{ |
100
|
12 |
|
return $this->getRelatedValues('identifiers', 'scheme', $scheme, 'identifier'); |
101
|
|
|
} |
102
|
|
|
|
103
|
12 |
|
public function identifierValue($scheme) |
104
|
|
|
{ |
105
|
12 |
|
$identifierValues = $this->identifierValues($scheme); |
106
|
12 |
|
return (count($identifierValues) > 0) ? $identifierValues[0] : null; |
107
|
|
|
} |
108
|
|
|
|
109
|
12 |
|
public function linkValues($note) |
110
|
|
|
{ |
111
|
12 |
|
return $this->getRelatedValues('links', 'note', $note, 'url'); |
112
|
|
|
} |
113
|
|
|
|
114
|
9 |
|
public function linkValue($note) |
115
|
|
|
{ |
116
|
9 |
|
$linkValues = $this->linkValues($note); |
117
|
9 |
|
return (count($linkValues) > 0) ? $linkValues[0] : null; |
118
|
|
|
} |
119
|
|
|
|
120
|
12 |
|
public function contactDetailValues($contactType) |
121
|
|
|
{ |
122
|
12 |
|
return $this->getRelatedValues('contact_details', 'type', $contactType, 'value'); |
123
|
|
|
} |
124
|
|
|
|
125
|
12 |
|
public function contactDetailValue($contactType) |
126
|
|
|
{ |
127
|
12 |
|
$contactDetailValues = $this->contactDetailValues($contactType); |
128
|
12 |
|
return (count($contactDetailValues) > 0) ? $contactDetailValues[0] : null; |
129
|
|
|
} |
130
|
|
|
|
131
|
42 |
View Code Duplication |
public function equals($other) |
|
|
|
|
132
|
|
|
{ |
133
|
42 |
|
if (is_object($other) && get_class($other) == get_class($this)) { |
134
|
39 |
|
return $this->id == $other->id; |
|
|
|
|
135
|
2 |
|
} |
136
|
3 |
|
throw new \BadMethodCallException; |
137
|
|
|
} |
138
|
|
|
|
139
|
51 |
|
public function getRelatedObjectArr($popoloArray) |
140
|
|
|
{ |
141
|
51 |
|
return $this->arrGet($this->data, $popoloArray, []); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.