Completed
Push — master ( 6c04ca...a08b36 )
by Phil
08:12
created

AbstractEntity::offsetExists()   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 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
c 1
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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 $relationships = [];
25
26
    /**
27
     * @var array
28
     */
29
    protected $decorators = [];
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 2
    public function toArray()
35
    {
36 2
        foreach ($this->data as $key => $value) {
37 2
            if ($value instanceof Collection) {
38 1
                $this->data[$key] = $value->toArray();
39 1
            }
40 2
        }
41
42 2
        return $this->data;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 5
    public function getMapping()
49
    {
50
        return array_combine(array_keys($this->mapping), array_map(function ($value) {
51 5
            return (array_key_exists('type', $value)) ? $value['type'] : null;
52 5
        }, $this->mapping));
53
}
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 7
    public function getRelationships()
59
    {
60 7
        return $this->relationships;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function getDecorators($action = null)
67
    {
68
        $decorators = array_merge([
69
            StoreInterface::ON_CREATE => [],
70
            StoreInterface::ON_READ   => [],
71
            StoreInterface::ON_UPDATE => [],
72
            StoreInterface::ON_DELETE => []
73
        ], $this->decorators);
74
75
        return (is_null($action)) ? $decorators : $decorators[$type];
0 ignored issues
show
Bug introduced by
The variable $type does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function getValidationRules()
82
    {
83 1
        return array_combine(array_keys($this->mapping), array_map(function ($value) {
84 1
            return (array_key_exists('validation', $value)) ? $value['validation'] : null;
85 1
        }, $this->mapping));
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 4
    public function hydrate(array $data)
92
    {
93 4
        foreach ($data as $key => $value) {
94 2
            $this[$key] = $value;
95 4
        }
96
97 4
        return $this;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103 4
    public function offsetSet($offset, $value)
104
    {
105 4
        $mapping = $this->getMapping();
106
107 4
        if (! array_key_exists($offset, $mapping) && ! array_key_exists($offset, $this->getRelationships())) {
108 1
            throw new InvalidArgumentException(
109 1
                sprintf('(%s) is not an accepted field for (%s)', $offset, get_class($this))
110 1
            );
111
        }
112
113 3
        if (array_key_exists($offset, $mapping) && ! is_null($mapping[$offset])) {
114 1
            settype($value, $mapping[$offset]);
115 1
        }
116
117 3
        $this->data[$offset] = $value;
118 3
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 3
    public function offsetGet($offset)
124
    {
125 3
        if (isset($this->data[$offset])) {
126 2
            return $this->data[$offset];
127
        }
128
129 1
        throw new InvalidArgumentException(
130 1
            sprintf('Undefined offset (%s) on (%s)', $offset, get_class($this))
131 1
        );
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137 2
    public function offsetExists($offset)
138
    {
139 2
        return isset($this->data);
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145 1
    public function offsetUnset($offset)
146
    {
147 1
        unset($this->data[$offset]);
148 1
    }
149
}
150