Completed
Push — master ( f39c28...23ff15 )
by Phil
04:40
created

AbstractEntity::getMapping()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4286
cc 2
eloc 4
nc 1
nop 0
crap 2
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
     * @var string
33
     */
34
    protected $validator;
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function toArray(array $scopes = [])
40
    {
41
        $data = [
42
            '_relationships' => []
43
        ];
44
45
        foreach ($this->getRelationships() as $relationship) {
46
            $data['_relationships'][$relationship] = [
47
                '_links' => [
48
                    'self' => [
49
                        'href' => sprintf(
50
                            '/%s/%s/%s',
51
                            $this->getDataSource(),
52
                            $this[$this->getPrimary()],
53
                            $relationship
54
                        )
55
                    ]
56
                ]
57
            ];
58
        }
59
60
        $data['_relationships']['all'] = [
61
            '_links' => [
62
                'self' => [
63
                    'href' => sprintf(
64
                        '/%s/%s/%s',
65
                        $this->getDataSource(),
66
                        $this[$this->getPrimary()],
67
                        implode(',', $this->getRelationships())
68
                    )
69
                ]
70
            ]
71
        ];
72
73
        return array_merge($this->getData($scopes), $data);
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 1
    public function getData(array $scopes = [])
80
    {
81
        // @todo filter by scopes
82 1
        return $this->data;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function getMapping()
89
    {
90 3
        return array_combine(array_keys($this->mapping), array_map(function ($value) {
91 3
            return (array_key_exists('type', $value)) ? $value['type'] : null;
92 3
        }, $this->mapping));
93
}
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 1
    public function getRelationships()
99
    {
100 1
        return $this->relationships;
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 1
    public function getDecorators($action = null)
107
    {
108 1
        $decorators = array_replace([
109 1
            StoreInterface::ON_CREATE => [],
110 1
            StoreInterface::ON_READ   => [],
111 1
            StoreInterface::ON_UPDATE => [],
112 1
            StoreInterface::ON_DELETE => []
113 1
        ], $this->decorators);
114
115 1
        return (is_null($action)) ? $decorators : $decorators[$action];
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121 1
    public function getValidator()
122
    {
123 1
        return $this->validator;
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129 3
    public function hydrate(array $data)
130
    {
131 3
        foreach ($data as $key => $value) {
132 1
            $this[$key] = $value;
133 3
        }
134
135 3
        return $this;
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141 2
    public function offsetSet($offset, $value)
142
    {
143 2
        $mapping = $this->getMapping();
144
145 2
        if (! array_key_exists($offset, $mapping)) {
146 1
            throw new InvalidArgumentException(
147 1
                sprintf('(%s) is not an accepted field for (%s)', $offset, get_class($this))
148 1
            );
149
        }
150
151 1
        if (array_key_exists($offset, $mapping) && ! is_null($mapping[$offset]) && ! is_null($value)) {
152 1
            settype($value, $mapping[$offset]);
153 1
        }
154
155 1
        $this->data[$offset] = (empty($value)) ? null : $value;
156 1
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161 2
    public function offsetGet($offset)
162
    {
163 2
        if (isset($this->data[$offset])) {
164 1
            return $this->data[$offset];
165
        }
166
167 1
        throw new InvalidArgumentException(
168 1
            sprintf('Undefined offset (%s) on (%s)', $offset, get_class($this))
169 1
        );
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175 1
    public function offsetExists($offset)
176
    {
177 1
        return isset($this->data);
178
    }
179
180
    /**
181
     * {@inheritdoc}
182
     */
183 1
    public function offsetUnset($offset)
184
    {
185 1
        unset($this->data[$offset]);
186 1
    }
187
}
188