Completed
Push — master ( 3dee16...2bf36e )
by Andy
02:57
created

Person::getGivenName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace EveryPolitician\EveryPoliticianPopolo\Objects;
4
5
use \EveryPolitician\EveryPoliticianPopolo\Parse;
6
7
class Person extends PopoloObject
8
{
9
    use \EveryPolitician\EveryPoliticianPopolo\Traits\ArrayGetterTrait;
10
11
    protected $properties = [
12
        'id',
13
        'email',
14
        'gender',
15
        'honorificPrefix',
16
        'honorificSuffix',
17
        'image',
18
        'name',
19
        'sortName',
20
        'nationalIdentity',
21
        'summary',
22
        'biography',
23
        'birthDate',
24
        'deathDate',
25
        'familyName',
26
        'givenName',
27
        'wikidata',
28
        'twitter',
29
        'twitterAll',
30
        'phone',
31
        'phoneAll',
32
        'facebook',
33
        'facebookAll',
34
        'fax',
35
        'faxAll',
36
37
        'links',
38
        'contactDetails',
39
        'identifiers',
40
        'images',
41
        'otherNames',
42
        'sources',
43
        'memberships',
44
    ];
45
46 6
    public function __toString()
47
    {
48 6
        return "<Person: ".$this->name.">";
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<EveryPolitician\E...nPopolo\Objects\Person>. Since you implemented __get, maybe consider adding a @property annotation.

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 @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

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.

Loading history...
49
    }
50
51 90
    protected function getId()
52
    {
53 90
         return $this->arrGet($this->data, 'id');
54
    }
55
56 3
    protected function getEmail()
57
    {
58 3
         return $this->arrGet($this->data, 'email');
59
    }
60
61 3
    protected function getGender()
62
    {
63 3
         return $this->arrGet($this->data, 'gender');
64
    }
65
66 3
    protected function getHonorificPrefix()
67
    {
68 3
         return $this->arrGet($this->data, 'honorific_prefix');
69
    }
70
71 3
    protected function getHonorificSuffix()
72
    {
73 3
         return $this->arrGet($this->data, 'honorific_suffix');
74
    }
75
76 6
    protected function getImage()
77
    {
78 6
         return $this->arrGet($this->data, 'image');
79
    }
80
81 33
    protected function getName()
82
    {
83 33
         return $this->arrGet($this->data, 'name');
84
    }
85
86 3
    protected function getSortName()
87
    {
88 3
         return $this->arrGet($this->data, 'sort_name');
89
    }
90
91 3
    protected function getNationalIdentity()
92
    {
93 3
         return $this->arrGet($this->data, 'national_identity');
94
    }
95
96 3
    protected function getSummary()
97
    {
98 3
         return $this->arrGet($this->data, 'summary');
99
    }
100
101 3
    protected function getBiography()
102
    {
103 3
         return $this->arrGet($this->data, 'biography');
104
    }
105
106 6
    protected function getBirthDate()
107
    {
108 6
        return $this->getDate('birth_date');
109
    }
110
111 6
    protected function getDeathDate()
112
    {
113 6
        return $this->getDate('death_date');
114
    }
115
116 3
    protected function getFamilyName()
117
    {
118 3
         return $this->arrGet($this->data, 'family_name');
119
    }
120
121 3
    protected function getGivenName()
122
    {
123 3
         return $this->arrGet($this->data, 'given_name');
124
    }
125
126 3
    protected function getWikidata()
127
    {
128 3
        return $this->identifierValue('wikidata');
129
    }
130
131 9
    protected function getTwitter()
132
    {
133 9
        $usernameOrUrl = $this->contactDetailValue('twitter') ?: $this->linkValue('twitter');
134 9
        if ($usernameOrUrl) {
135 6
            return Parse::extractTwitterUsername($usernameOrUrl);
136
        }
137 3
        return null;
138
    }
139
140 6
    protected function getTwitterAll()
141
    {
142
        // The Twitter screen names in contact_details and links will
143
        // in most cases be the same, so remove duplicates:
144 6
        $allTwitters = [];
145 6
        $rawTwitters = $this->contactDetailValues('twitter') + $this->linkValues('twitter');
146 6
        foreach ($rawTwitters as $rawTwitter) {
147 6
            $twitter = Parse::extractTwitterUsername($rawTwitter);
148 6
            if (!in_array($twitter, $allTwitters)) {
149 6
                $allTwitters[] = $twitter;
150 4
            }
151 4
        }
152 6
        return $allTwitters;
153
    }
154
155 3
    protected function getPhone()
156
    {
157 3
        return $this->contactDetailValue('phone');
158
    }
159
160 3
    protected function getPhoneAll()
161
    {
162 3
        return $this->contactDetailValues('phone');
163
    }
164
165 3
    protected function getFacebook()
166
    {
167 3
        return $this->linkValue('facebook');
168
    }
169
170 3
    protected function getFacebookAll()
171
    {
172 3
        return $this->linkValues('facebook');
173
    }
174
175 3
    protected function getFax()
176
    {
177 3
        return $this->contactDetailValue('fax');
178
    }
179
180 3
    protected function getFaxAll()
181
    {
182 3
        return $this->contactDetailValues('fax');
183
    }
184
185 3
    protected function getLinks()
186
    {
187 3
         return $this->getRelatedObjectArr('links');
188
    }
189
190 3
    protected function getContactDetails()
191
    {
192 3
         return $this->getRelatedObjectArr('contact_details');
193
    }
194
195 3
    protected function getIdentifiers()
196
    {
197 3
         return $this->getRelatedObjectArr('identifiers');
198
    }
199
200 3
    protected function getImages()
201
    {
202 3
         return $this->getRelatedObjectArr('images');
203
    }
204
205 21
    protected function getOtherNames()
206
    {
207 21
         return $this->getRelatedObjectArr('other_names');
208
    }
209
210 3
    protected function getSources()
211
    {
212 3
         return $this->getRelatedObjectArr('sources');
213
    }
214
215 3
    protected function getMemberships()
216
    {
217 3
        $memberships = [];
218 3
        foreach ($this->allPopolo->memberships as $m) {
219 3
            if ($m->personId == $this->id) {
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<EveryPolitician\E...nPopolo\Objects\Person>. Since you implemented __get, maybe consider adding a @property annotation.

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 @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

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.

Loading history...
220 3
                $memberships[] = $m;
221 2
            }
222 2
        }
223 3
        return $memberships;
224
    }
225
226 9
    private function isNameCurrentAt($name, $dateStr)
227
    {
228 9
        $startRange = $this->arrGet($name, 'start_date') ?: '0001-01-01';
229 9
        $endRange = $this->arrGet($name, 'end_date') ?: '9999-12-31';
230 9
        return $dateStr >= $startRange && $dateStr <= $endRange;
231
    }
232
233 15
    public function nameAt($particularDate)
234
    {
235 15
        $historicNames = [];
236 15
        foreach ($this->otherNames as $otherName) {
0 ignored issues
show
Documentation introduced by
The property otherNames does not exist on object<EveryPolitician\E...nPopolo\Objects\Person>. Since you implemented __get, maybe consider adding a @property annotation.

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 @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

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.

Loading history...
237 12
            if (array_key_exists('end_date', $otherName)) {
238 10
                $historicNames[] = $otherName;
239 6
            }
240 10
        }
241 15
        if (empty($historicNames)) {
242 6
            return $this->name;
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<EveryPolitician\E...nPopolo\Objects\Person>. Since you implemented __get, maybe consider adding a @property annotation.

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 @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

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.

Loading history...
243
        }
244 9
        $namesAtDate = [];
245 9
        foreach ($historicNames as $n) {
246 9
            if ($this->isNameCurrentAt($n, $particularDate->format('Y-m-d'))) {
247 7
                $namesAtDate[] = $n;
248 4
            }
249 6
        }
250 9
        if (empty($namesAtDate)) {
251 3
            return $this->name;
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<EveryPolitician\E...nPopolo\Objects\Person>. Since you implemented __get, maybe consider adding a @property annotation.

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 @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

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.

Loading history...
252
        }
253 6
        if (count($namesAtDate) > 1) {
254 3
            $msg = 'Multiple names for ' . (string) $this;
255 3
            $msg .= ' found at date ' . $particularDate->format('Y-m-d');
256 3
            throw new \Exception($msg);
257
        }
258 3
        return $namesAtDate[0]['name'];
259
    }
260
}
261