AbstractEntity::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Chadicus\Entity;
4
5
use Chadicus\Spl\Exceptions\UndefinedPropertyException;
6
use DominionEnterprises\Filterer;
7
use DominionEnterprises\Util;
8
9
/**
10
 * Adds additional functionality to entity implementations.
11
 */
12
abstract class AbstractEntity implements EntityInterface
13
{
14
    /**
15
     * The data for this AbstractEntity
16
     *
17
     * @var array
18
     */
19
    private $data = [];
20
21
    /**
22
     * Create a new AbstractEntity based on the given $input array.
23
     *
24
     * @param array $input The data for the EntityInterface.
25
     */
26
    final public function __construct(array $input = [])
27
    {
28
        list($success, $filtered, $error) = Filterer::filter($this->getFilters(), $input, ['allowUnknowns' => true]);
29
        Util::ensure(true, $success, '\InvalidArgumentException', [$error]);
30
31
        foreach ($filtered as $key => $value) {
32
            $this->data[$key] = $value;
33
        }
34
    }
35
36
    /**
37
     * Get an AbstractEntity property.
38
     *
39
     * @param string $name The name of the property.
40
     *
41
     * @return mixed
42
     *
43
     * @throws UndefinedPropertyException Thrown if $name is not a valid class property.
44
     */
45
    final public function __get($name)
46
    {
47
        if (!array_key_exists($name, $this->data)) {
48
            $class = get_called_class();
49
            throw new UndefinedPropertyException("Undefined Property {$class}::\${$name}");
50
        }
51
52
        return $this->data[$name];
53
    }
54
55
    /**
56
     * Allows for getX() method calls.
57
     *
58
     * @param string $name      The name of the method being called.
59
     * @param array  $arguments The arguments being passed to the method. This parameter is unused.
60
     *
61
     * @return mixed
62
     *
63
     * @throws \BadMethodCallException Thrown if the property being accessed does not exist.
64
     */
65
    final public function __call($name, array $arguments = [])
66
    {
67
        if (substr($name, 0, 3) !== 'get') {
68
            $class = get_called_class();
69
            throw new \BadMethodCallException("Method {$class}::{$name}() does not exist");
70
        }
71
72
        $key = lcfirst(substr($name, 3));
73
74
        if (!array_key_exists($key, $this->data)) {
75
            $class = get_called_class();
76
            throw new \BadMethodCallException("Method {$class}::{$name}() does not exist");
77
        }
78
79
        return $this->data[$key];
80
    }
81
82
    /**
83
     * Create an array of new AbstractEntity based on the given $input arrays.
84
     *
85
     * @param array[] $inputs The value to be filtered.
86
     *
87
     * @return EntityCollection
88
     */
89
    final public static function fromArrays(array $inputs)
90
    {
91
        Util::throwIfNotType(['array' => $inputs]);
92
93
        $entities = new EntityCollection();
94
        foreach ($inputs as $key => $input) {
95
            $entities[$key] = new static($input);
96
        }
97
98
        return $entities;
99
    }
100
101
    /**
102
     * Create a new AbstractEntity based on the given $input array.
103
     *
104
     * @param array $input The data for the AbstractEntity.
105
     *
106
     * @return AbstractEntity
107
     */
108
    final public static function fromArray(array $input)
109
    {
110
        return new static($input);
111
    }
112
113
    /**
114
     * Returns the data for this object which can be serialized to JSON.
115
     *
116
     * @return array
117
     */
118
    public function jsonSerialize()
119
    {
120
        return $this->data;
121
    }
122
123
    /**
124
     * Returns an array of filters suitable for use with \DominionEnterprises\Filterer::filter().
125
     *
126
     * @return array
127
     */
128
    abstract protected function getFilters();
129
}
130