1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tacone\Bees\Attribute; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder; |
8
|
|
|
use Illuminate\Database\Query\Builder as QueryBuilder; |
9
|
|
|
use Traversable; |
10
|
|
|
|
11
|
|
|
class ArrAttribute extends AbstractAttribute implements \Countable, \IteratorAggregate, \ArrayAccess |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Required by DelegatedArrayTrait, must return the |
15
|
|
|
* storage array. |
16
|
|
|
*/ |
17
|
|
|
public function getDelegatedStorage() |
18
|
|
|
{ |
19
|
|
|
return !empty($this->storage[$this->path]) |
20
|
|
|
? $this->storage[$this->path] |
21
|
|
|
: []; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function handle($arguments) |
25
|
|
|
{ |
26
|
|
|
if (!count($arguments)) { |
27
|
|
|
return $this->get(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$this->set($arguments); |
31
|
|
|
|
32
|
|
|
return $this->object; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function get() |
36
|
|
|
{ |
37
|
|
|
return $this->getDelegatedStorage(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function set($arguments) |
41
|
|
|
{ |
42
|
|
|
$array = $arguments[0]; |
43
|
|
|
$this->exchangeArray($array); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// --- cast |
47
|
|
|
|
48
|
|
|
protected function castToArray($array) |
49
|
|
|
{ |
50
|
|
|
switch (true) { |
51
|
|
|
case is_array($array): |
52
|
|
|
return $array; |
53
|
|
|
case $array instanceof Arrayable: |
54
|
|
|
case $array instanceof Model: |
|
|
|
|
55
|
|
|
return $array->toArray(); |
56
|
|
|
|
57
|
|
|
case $array instanceof EloquentBuilder: |
|
|
|
|
58
|
|
|
case $array instanceof QueryBuilder: |
|
|
|
|
59
|
|
|
return $array->get()->toArray(); |
60
|
|
|
|
61
|
|
|
case $array instanceof \ArrayIterator: |
62
|
|
|
case $array instanceof \ArrayObject: |
63
|
|
|
return $array->getArrayCopy(); |
64
|
|
|
|
65
|
|
|
case is_null($array): |
66
|
|
|
return []; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
throw new \RuntimeException(sprintf( |
70
|
|
|
'ArrAttribute does not supports type: %s%s', |
71
|
|
|
gettype($array), |
72
|
|
|
is_object($array) ? ' - '.get_class($array) : '' |
73
|
|
|
)); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// --- Array methods |
77
|
|
|
|
78
|
|
|
public function count() |
79
|
|
|
{ |
80
|
|
|
return $this->getDelegatedStorage()->count(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function offsetExists($name) |
84
|
|
|
{ |
85
|
|
|
return isset($this->getDelegatedStorage()[$name]); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function offsetSet($name, $value) |
89
|
|
|
{ |
90
|
|
|
$array = $this->getDelegatedStorage(); |
91
|
|
|
$array[$name] = $value; |
92
|
|
|
$this->exchangeArray($array); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function offsetUnset($name) |
96
|
|
|
{ |
97
|
|
|
return $this->getDelegatedStorage()->offsetUnset($name); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function offsetGet($name) |
101
|
|
|
{ |
102
|
|
|
return $this->getDelegatedStorage()[$name]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* (PHP 5 >= 5.0.0)<br/> |
107
|
|
|
* Retrieve an external iterator. |
108
|
|
|
* |
109
|
|
|
* @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
110
|
|
|
* |
111
|
|
|
* @return Traversable An instance of an object implementing <b>Iterator</b> or |
112
|
|
|
* <b>Traversable</b> |
113
|
|
|
*/ |
114
|
|
|
public function getIterator() |
115
|
|
|
{ |
116
|
|
|
return $this->getDelegatedStorage(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function toArray() |
120
|
|
|
{ |
121
|
|
|
return $this->getDelegatedStorage(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function exchangeArray($array) |
125
|
|
|
{ |
126
|
|
|
$array = $this->castToArray($array); |
127
|
|
|
$this->storage[$this->path] = $array; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.