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
|
2 |
|
public function toArray(array $scopes = null) |
40
|
|
|
{ |
41
|
|
|
// @todo filter by scopes |
42
|
2 |
|
foreach ($this->data as $key => $value) { |
43
|
2 |
|
if ($value instanceof Collection) { |
44
|
1 |
|
$this->data[$key] = $value->toArray($scopes); |
|
|
|
|
45
|
1 |
|
} |
46
|
2 |
|
} |
47
|
|
|
|
48
|
2 |
|
return $this->data; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
public function getMapping() |
55
|
|
|
{ |
56
|
5 |
|
return array_combine(array_keys($this->mapping), array_map(function ($value) { |
57
|
5 |
|
return (array_key_exists('type', $value)) ? $value['type'] : null; |
58
|
5 |
|
}, $this->mapping)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
7 |
|
public function getRelationships() |
65
|
|
|
{ |
66
|
7 |
|
return $this->relationships; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
1 |
|
public function getDecorators($action = null) |
73
|
|
|
{ |
74
|
1 |
|
$decorators = array_replace([ |
75
|
1 |
|
StoreInterface::ON_CREATE => [], |
76
|
1 |
|
StoreInterface::ON_READ => [], |
77
|
1 |
|
StoreInterface::ON_UPDATE => [], |
78
|
1 |
|
StoreInterface::ON_DELETE => [] |
79
|
1 |
|
], $this->decorators); |
80
|
|
|
|
81
|
1 |
|
return (is_null($action)) ? $decorators : $decorators[$action]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
1 |
|
public function getValidator() |
88
|
|
|
{ |
89
|
1 |
|
return $this->validator; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
4 |
|
public function hydrate(array $data) |
96
|
|
|
{ |
97
|
4 |
|
foreach ($data as $key => $value) { |
98
|
2 |
|
$this[$key] = $value; |
99
|
4 |
|
} |
100
|
|
|
|
101
|
4 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
4 |
|
public function offsetSet($offset, $value) |
108
|
|
|
{ |
109
|
4 |
|
$mapping = $this->getMapping(); |
110
|
|
|
|
111
|
4 |
|
if (! array_key_exists($offset, $mapping) && ! array_key_exists($offset, $this->getRelationships())) { |
112
|
1 |
|
throw new InvalidArgumentException( |
113
|
1 |
|
sprintf('(%s) is not an accepted field for (%s)', $offset, get_class($this)) |
114
|
1 |
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
3 |
|
if (array_key_exists($offset, $mapping) && ! is_null($mapping[$offset])) { |
118
|
1 |
|
settype($value, $mapping[$offset]); |
119
|
1 |
|
} |
120
|
|
|
|
121
|
3 |
|
$this->data[$offset] = $value; |
122
|
3 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritdoc} |
126
|
|
|
*/ |
127
|
3 |
|
public function offsetGet($offset) |
128
|
|
|
{ |
129
|
3 |
|
if (isset($this->data[$offset])) { |
130
|
2 |
|
return $this->data[$offset]; |
131
|
|
|
} |
132
|
|
|
|
133
|
1 |
|
throw new InvalidArgumentException( |
134
|
1 |
|
sprintf('Undefined offset (%s) on (%s)', $offset, get_class($this)) |
135
|
1 |
|
); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* {@inheritdoc} |
140
|
|
|
*/ |
141
|
2 |
|
public function offsetExists($offset) |
142
|
|
|
{ |
143
|
2 |
|
return isset($this->data); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* {@inheritdoc} |
148
|
|
|
*/ |
149
|
1 |
|
public function offsetUnset($offset) |
150
|
|
|
{ |
151
|
1 |
|
unset($this->data[$offset]); |
152
|
1 |
|
} |
153
|
|
|
} |
154
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.