Completed
Push — master ( 8963b2...b89872 )
by Andy
04:37
created

PopoloObject::getKeyForHash()   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
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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
     *
21
     */
22 255
    public function __construct($data, $allPopolo)
23
    {
24 255
        $this->data = $data;
25 255
        $this->allPopolo = $allPopolo;
26 255
    }
27
28 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...
29
    {
30 255
        $properties = array_merge($this->baseProperties, $this->properties);
31 255
        if (in_array($prop, $properties)) {
32 255
            $getter = 'get' . ucfirst($prop);
33 255
            return $this->$getter();
34
        }
35 3
        trigger_error('Undefined property: '.__CLASS__.'::$'.$prop, E_USER_ERROR);
36 2
    }
37
38 222
    protected function getKeyForHash()
39
    {
40 222
        return $this->getId();
41
    }
42
43 2
    protected function getId()
44
    {
45 2
        return null;
46
    }
47
48 27
    private function getRelatedObjects($popoloArray)
49
    {
50 27
        return $this->arrGet($this->data, $popoloArray, []);
51
    }
52
53 35
    protected function getDate($attr, $default = null)
54
    {
55 35
        $d = $this->arrGet($this->data, $attr);
56 33
        if ($d) {
57 30
            return new DateTime($d);
58
        }
59 3
        return $default;
60 2
    }
61
62 27
    private function getRelatedValues($popoloArray, $infoTypeKey, $infoType, $infoValueKey)
63
    {
64
        /* Get a value from one of the Popolo related objects
65
         *
66
         * For example, if you have a person with related links, like
67
         * this:
68
         *
69
         *     {
70
         *         "name": "Dale Cooper",
71
         *         "links": [
72
         *             {
73
         *                 "note": "wikipedia",
74
         *                 "url": "https://en.wikipedia.org/wiki/Dale_Cooper"
75
         *             }
76
         *         ]
77
         *     }
78
         *
79
         * When calling this method to get the Wikipedia URL, you would use:
80
         *
81
         *     popoloArray  = 'links'
82
         *     infoTypeKey  = 'note'
83
         *     infoType     = 'wikipedia'
84
         *     infoValueKey = 'url'
85
         *
86
         * ... so the following would work:
87
         *
88
         *     $this->getRelatedValue('links', 'note', 'wikipedia', 'url')
89
         *     # => 'https://en.wikipedia.org/wiki/Dale_Cooper'
90
         */
91 27
        $relatedValues = [];
92 27
        $relatedObjects = $this->getRelatedObjects($popoloArray);
93 27
        foreach ($relatedObjects as $o) {
94 21
            if ($o[$infoTypeKey] === $infoType) {
95 23
                $relatedValues[] = $o[$infoValueKey];
96 14
            }
97 18
        }
98 27
        return $relatedValues;
99
    }
100
101 12
    public function identifierValues($scheme)
102
    {
103 12
        return $this->getRelatedValues('identifiers', 'scheme', $scheme, 'identifier');
104
    }
105
106 12
    public function identifierValue($scheme)
107
    {
108 12
        $identifierValues = $this->identifierValues($scheme);
109 12
        return (count($identifierValues) > 0) ? $identifierValues[0] : null;
110
    }
111
112 12
    public function linkValues($note)
113
    {
114 12
        return $this->getRelatedValues('links', 'note', $note, 'url');
115
    }
116
117 9
    public function linkValue($note)
118
    {
119 9
        $linkValues = $this->linkValues($note);
120 9
        return (count($linkValues) > 0) ? $linkValues[0] : null;
121
    }
122
123 12
    public function contactDetailValues($contactType)
124
    {
125 12
        return $this->getRelatedValues('contact_details', 'type', $contactType, 'value');
126
    }
127
128 12
    public function contactDetailValue($contactType)
129
    {
130 12
        $contactDetailValues = $this->contactDetailValues($contactType);
131 12
        return (count($contactDetailValues) > 0) ? $contactDetailValues[0] : null;
132
    }
133
134 44 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...
135
    {
136 44
        if (is_object($other) && get_class($other) == get_class($this)) {
137 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...
138
        }
139
        // TODO: Throw a custom exception here
140 3
        throw new \BadMethodCallException;
141
    }
142
143 51
    public function getRelatedObjectArr($popoloArray)
144
    {
145 51
        return $this->arrGet($this->data, $popoloArray, []);
146
    }
147
}
148