Completed
Push — master ( 9432d1...9243b0 )
by Andy
02:48
created

PopoloObject::getRelatedValues()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 38
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 10
cts 10
cp 1
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 4
crap 3
1
<?php
2
3
namespace EveryPolitician\EveryPoliticianPopolo\Objects;
4
5
use \DateTime;
6
use \EveryPolitician\EveryPoliticianPopolo\Traits\ArrayGetterTrait;
7
8
class PopoloObject
9
{
10
    use ArrayGetterTrait;
11
12
    protected $properties = [];
13
    protected $baseProperties = [
14
        'keyForHash',
15
    ];
16
    protected $data;
17
    protected $allPopolo;
18
19
    /**
20
     * Creates a new instance
21
     */
22 255
    public function __construct($data, $allPopolo)
23
    {
24 255
        $this->data = $data;
25 255
        $this->allPopolo = $allPopolo;
26 255
    }
27
28
    /**
29
     * Getter for public attributes
30
     *
31
     * @param string $prop the attribute to get
32
     *
33
     * @return mixed
34
     */
35 255 View Code Duplication
    public function __get($prop)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
36 1
    {
37 255
        $properties = array_merge($this->baseProperties, $this->properties);
38 255
        if (in_array($prop, $properties)) {
39 255
            $getter = 'get' . ucfirst($prop);
40 255
            return $this->$getter();
41 1
        }
42 3
        trigger_error('Undefined property: '.__CLASS__.'::$'.$prop, E_USER_ERROR);
43
    }
44
45 222
    protected function getKeyForHash()
46
    {
47 222
        return $this->getId();
48
    }
49
50
    protected function getId()
51
    {
52
        return null;
53
    }
54
55 27
    private function getRelatedObjects($popoloArray)
56
    {
57 27
        return $this->arrGet($this->data, $popoloArray, []);
58
    }
59
60 33
    protected function getDate($attr, $default = null)
61
    {
62 33
        $d = $this->arrGet($this->data, $attr);
63 33
        if ($d) {
64 30
            return new DateTime($d);
65 1
        }
66 3
        return $default;
67
    }
68
69 28
    private function getRelatedValues($popoloArray, $infoTypeKey, $infoType, $infoValueKey)
70 1
    {
71
        /* Get a value from one of the Popolo related objects
72
         *
73
         * For example, if you have a person with related links, like
74
         * this:
75
         *
76
         *     {
77
         *         "name": "Dale Cooper",
78
         *         "links": [
79
         *             {
80
         *                 "note": "wikipedia",
81
         *                 "url": "https://en.wikipedia.org/wiki/Dale_Cooper"
82
         *             }
83
         *         ]
84
         *     }
85
         *
86
         * When calling this method to get the Wikipedia URL, you would use:
87
         *
88
         *     popoloArray  = 'links'
89
         *     infoTypeKey  = 'note'
90
         *     infoType     = 'wikipedia'
91
         *     infoValueKey = 'url'
92
         *
93
         * ... so the following would work:
94
         *
95
         *     $this->getRelatedValue('links', 'note', 'wikipedia', 'url')
96
         *     # => 'https://en.wikipedia.org/wiki/Dale_Cooper'
97
         */
98 27
        $relatedValues = [];
99 27
        $relatedObjects = $this->getRelatedObjects($popoloArray);
100 28
        foreach ($relatedObjects as $o) {
101 21
            if ($o[$infoTypeKey] === $infoType) {
102 21
                $relatedValues[] = $o[$infoValueKey];
103 7
            }
104 9
        }
105 27
        return $relatedValues;
106
    }
107
108 12
    public function identifierValues($scheme)
109
    {
110 12
        return $this->getRelatedValues('identifiers', 'scheme', $scheme, 'identifier');
111
    }
112
113 12
    public function identifierValue($scheme)
114
    {
115 12
        $identifierValues = $this->identifierValues($scheme);
116 12
        return (count($identifierValues) > 0) ? $identifierValues[0] : null;
117
    }
118
119 12
    public function linkValues($note)
120
    {
121 12
        return $this->getRelatedValues('links', 'note', $note, 'url');
122
    }
123
124 9
    public function linkValue($note)
125
    {
126 9
        $linkValues = $this->linkValues($note);
127 9
        return (count($linkValues) > 0) ? $linkValues[0] : null;
128
    }
129
130 12
    public function contactDetailValues($contactType)
131
    {
132 12
        return $this->getRelatedValues('contact_details', 'type', $contactType, 'value');
133
    }
134
135 12
    public function contactDetailValue($contactType)
136
    {
137 12
        $contactDetailValues = $this->contactDetailValues($contactType);
138 12
        return (count($contactDetailValues) > 0) ? $contactDetailValues[0] : null;
139
    }
140
141 42 View Code Duplication
    public function equals($other)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
142
    {
143 42
        if (is_object($other) && get_class($other) == get_class($this)) {
144 39
            return $this->id == $other->id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<EveryPolitician\E...o\Objects\PopoloObject>. 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...
145
        }
146
        // TODO: Throw a custom exception here
147 3
        throw new \BadMethodCallException;
148
    }
149
150 51
    public function getRelatedObjectArr($popoloArray)
151
    {
152 51
        return $this->arrGet($this->data, $popoloArray, []);
153
    }
154
}
155