1 | <?php |
||
11 | class SchemalessAttributes implements ArrayAccess, Countable, Arrayable |
||
12 | { |
||
13 | /** @var \Illuminate\Database\Eloquent\Model */ |
||
14 | protected $model; |
||
15 | |||
16 | /** @var string */ |
||
17 | protected $sourceAttributeName; |
||
18 | |||
19 | /** @var array */ |
||
20 | protected $schemalessAttributes = []; |
||
21 | |||
22 | public static function createForModel(Model $model, string $sourceAttributeName): self |
||
23 | { |
||
24 | return new static($model, $sourceAttributeName); |
||
25 | } |
||
26 | |||
27 | public function __construct(Model $model, string $sourceAttributeName) |
||
28 | { |
||
29 | $this->model = $model; |
||
30 | |||
31 | $this->sourceAttributeName = $sourceAttributeName; |
||
32 | |||
33 | $this->schemalessAttributes = $this->getRawSchemalessAttributes(); |
||
34 | } |
||
35 | |||
36 | public function __get(string $name) |
||
37 | { |
||
38 | return $this->get($name); |
||
39 | } |
||
40 | |||
41 | public function get(string $name, $default = null) |
||
42 | { |
||
43 | return array_get($this->schemalessAttributes, $name, $default); |
||
44 | } |
||
45 | |||
46 | public function __set(string $name, $value) |
||
47 | { |
||
48 | $this->set($name, $value); |
||
49 | } |
||
50 | |||
51 | public function set(string $name, $value) |
||
52 | { |
||
53 | array_set($this->schemalessAttributes, $name, $value); |
||
54 | |||
55 | $this->model->{$this->sourceAttributeName} = $this->schemalessAttributes; |
||
56 | } |
||
57 | |||
58 | public function has(string $name): bool |
||
59 | { |
||
60 | return array_has($this->schemalessAttributes, $name); |
||
61 | } |
||
62 | |||
63 | public function forget(string $name): self |
||
69 | |||
70 | public function all(): array |
||
74 | |||
75 | public function offsetExists($offset) |
||
76 | { |
||
77 | return $this->has($offset); |
||
78 | } |
||
79 | |||
80 | public function offsetGet($offset) |
||
81 | { |
||
82 | return $this->$offset; |
||
84 | |||
85 | public function offsetSet($offset, $value) |
||
89 | |||
90 | public function offsetUnset($offset) |
||
94 | |||
95 | public function count() |
||
99 | |||
100 | public function toArray(): array |
||
104 | |||
105 | public static function scopeWithSchemalessAttributes(string $attributeName): Builder |
||
129 | |||
130 | protected function getRawSchemalessAttributes(): array |
||
134 | } |
||
135 |
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.