1 | <?php |
||
19 | class SchemalessAttributes implements ArrayAccess, Arrayable, Countable, IteratorAggregate, Jsonable, JsonSerializable |
||
20 | { |
||
21 | /** @var \Illuminate\Database\Eloquent\Model */ |
||
22 | protected $model; |
||
23 | |||
24 | /** @var string */ |
||
25 | protected $sourceAttributeName; |
||
26 | |||
27 | /** @var Collection */ |
||
28 | protected $collection; |
||
29 | |||
30 | public static function createForModel(Model $model, string $sourceAttributeName): self |
||
31 | { |
||
32 | return new static($model, $sourceAttributeName); |
||
33 | } |
||
34 | |||
35 | public function __construct(Model $model, string $sourceAttributeName) |
||
36 | { |
||
37 | $this->model = $model; |
||
38 | |||
39 | $this->sourceAttributeName = $sourceAttributeName; |
||
40 | |||
41 | $this->collection = new Collection($this->getRawSchemalessAttributes()); |
||
42 | } |
||
43 | |||
44 | public function __call($name, $arguments) |
||
45 | { |
||
46 | $result = call_user_func_array([$this->collection, $name], $arguments); |
||
47 | |||
48 | $this->override($this->collection->toArray()); |
||
49 | |||
50 | return $result; |
||
51 | } |
||
52 | |||
53 | public function __get($name) |
||
54 | { |
||
55 | return $this->get($name); |
||
56 | } |
||
57 | |||
58 | public function __set($name, $value) |
||
59 | { |
||
60 | $this->set($name, $value); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @see Collection::get() |
||
65 | * |
||
66 | * @param $key |
||
67 | * @param null $default |
||
68 | * |
||
69 | * @return mixed |
||
70 | */ |
||
71 | public function get($key, $default = null) |
||
72 | { |
||
73 | return data_get($this->collection, $key, $default); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @see Collection::set() |
||
78 | * |
||
79 | * @param $key |
||
80 | * @param $value |
||
81 | * |
||
82 | * @return mixed |
||
83 | */ |
||
84 | public function set($key, $value = null) |
||
85 | { |
||
86 | if (is_iterable($key)) { |
||
87 | return $this->override($this->collection->merge($key)); |
||
88 | } |
||
89 | |||
90 | $items = $this->collection->toArray(); |
||
91 | |||
92 | return $this->override(data_set($items, $key, $value)); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @see Collection::forget() |
||
97 | * |
||
98 | * @param $keys |
||
99 | * |
||
100 | * @return SchemalessAttributes |
||
101 | */ |
||
102 | public function forget($keys) |
||
103 | { |
||
104 | $items = $this->collection->toArray(); |
||
105 | |||
106 | foreach ((array) $keys as $key) { |
||
107 | Arr::forget($items, $key); |
||
108 | } |
||
109 | |||
110 | return $this->override($items); |
||
111 | } |
||
112 | |||
113 | public static function scopeWithSchemalessAttributes(string $attributeName): Builder |
||
137 | |||
138 | public function offsetGet($offset) |
||
139 | { |
||
140 | return $this->get($offset); |
||
141 | } |
||
142 | |||
143 | public function offsetExists($offset) |
||
144 | { |
||
145 | return $this->collection->offsetExists($offset); |
||
146 | } |
||
147 | |||
148 | public function offsetSet($offset, $value) |
||
152 | |||
153 | public function offsetUnset($offset) |
||
157 | |||
158 | public function toArray() |
||
162 | |||
163 | public function toJson($options = 0) |
||
167 | |||
168 | public function jsonSerialize() |
||
172 | |||
173 | public function count() |
||
177 | |||
178 | public function getIterator() |
||
182 | |||
183 | protected function getRawSchemalessAttributes(): array |
||
187 | |||
188 | protected function override(iterable $collection) |
||
195 | } |
||
196 |
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.