Completed
Push — master ( 10afaf...ad9c2a )
by Phil
6s
created

AbstractEntity::toArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 8
Bugs 0 Features 0
Metric Value
c 8
b 0
f 0
dl 0
loc 12
ccs 6
cts 7
cp 0.8571
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
crap 2.0116
1
<?php
2
3
namespace Percy\Entity;
4
5
use InvalidArgumentException;
6
use RuntimeException;
7
use Percy\Store\StoreInterface;
8
9
abstract class AbstractEntity implements EntityInterface
10
{
11
    /**
12
     * @var array
13
     */
14
    protected $data = [];
15
16
    /**
17
     * @var array
18
     */
19
    protected $mapping = [];
20
21
    /**
22
     * @var array
23
     */
24
    protected $relationshipMap = [];
25
26
    /**
27
     * @var array
28
     */
29
    protected $relationships = [];
30
31
    /**
32
     * @var array
33
     */
34
    protected $decorators = [];
35
36
    /**
37
     * @var string
38
     */
39
    protected $validator;
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 1
    public function toArray(array $scopes = [])
45
    {
46
        $data = [
47 1
            '_relationships' => []
48 1
        ];
49
50 1
        foreach ($this->getRelationships() as $key => $value) {
51
            $data['_relationships'][$key] = $value->toArray($scopes);
52 1
        }
53
54 1
        return array_merge($this->getData($scopes, false), $data);
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 2
    public function getData(array $scopes = [], $toPersist = true)
61
    {
62
        // @todo filter by scopes
63
64 2
        if ($toPersist === false) {
65 1
            return $this->data;
66
        }
67
68 1
        $data = [];
69
70 1
        foreach ($this->mapping as $prop => $options) {
71 1
            if (array_key_exists('persist', $options) && $options['persist'] === false) {
72 1
                continue;
73
            }
74
75 1
            if (array_key_exists($prop, $this->data)) {
76 1
                $data[$prop] = $this->data[$prop];
77 1
            }
78 1
        }
79
80 1
        return $data;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function getMapping()
87
    {
88 4
        return array_combine(array_keys($this->mapping), array_map(function ($value) {
89 4
            return (array_key_exists('type', $value)) ? $value['type'] : null;
90 4
        }, $this->mapping));
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 1
    public function getRelationshipMap()
97
    {
98 1
        return $this->relationshipMap;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 1
    public function getRelationships()
105
    {
106 1
        return $this->relationships;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function addRelationship($relationship, Collection $collection)
113
    {
114
        $this->relationships[$relationship] = $collection;
115
116
        return $this;
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122 3
    public function getDecorators($action = null)
123
    {
124 3
        $decorators = array_replace([
125 3
            StoreInterface::ON_CREATE => [],
126 3
            StoreInterface::ON_READ   => [],
127 3
            StoreInterface::ON_UPDATE => [],
128 3
            StoreInterface::ON_DELETE => []
129 3
        ], $this->decorators);
130
131 3
        return (is_null($action)) ? $decorators : $decorators[$action];
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137 1
    public function getValidator()
138
    {
139 1
        return $this->validator;
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145 4
    public function hydrate(array $data)
146
    {
147 4
        foreach ($data as $key => $value) {
148 2
            $this[$key] = $value;
149 4
        }
150
151 4
        return $this;
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157 3
    public function offsetSet($offset, $value)
158
    {
159 3
        $mapping = $this->getMapping();
160
161 3
        if (! array_key_exists($offset, $mapping)) {
162 1
            throw new InvalidArgumentException(
163 1
                sprintf('(%s) is not an accepted field for (%s)', $offset, get_class($this))
164 1
            );
165
        }
166
167 2
        if (array_key_exists($offset, $mapping) && ! is_null($mapping[$offset]) && ! is_null($value)) {
168 2
            settype($value, $mapping[$offset]);
169 2
        }
170
171 2
        $this->data[$offset] = (! isset($value)) ? null : $value;
172 2
    }
173
174
    /**
175
     * {@inheritdoc}
176
     */
177 2
    public function offsetGet($offset)
178
    {
179 2
        if (isset($this->data[$offset])) {
180 1
            return $this->data[$offset];
181
        }
182
183 1
        throw new InvalidArgumentException(
184 1
            sprintf('Undefined offset (%s) on (%s)', $offset, get_class($this))
185 1
        );
186
    }
187
188
    /**
189
     * {@inheritdoc}
190
     */
191 1
    public function offsetExists($offset)
192
    {
193 1
        return isset($this->data);
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199 1
    public function offsetUnset($offset)
200
    {
201 1
        unset($this->data[$offset]);
202 1
    }
203
}
204