1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\SchemalessAttributes; |
4
|
|
|
|
5
|
|
|
use Countable; |
6
|
|
|
use ArrayAccess; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Illuminate\Database\Eloquent\Builder; |
9
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
10
|
|
|
|
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 data_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($attribute, $value = null) |
52
|
|
|
{ |
53
|
|
|
if (is_iterable($attribute)) { |
54
|
|
|
foreach($attribute as $attribute => $value) { |
55
|
|
|
$this->set($attribute, $value); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
data_set($this->schemalessAttributes, $attribute, $value); |
62
|
|
|
|
63
|
|
|
$this->model->{$this->sourceAttributeName} = $this->schemalessAttributes; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function has(string $name): bool |
67
|
|
|
{ |
68
|
|
|
return array_has($this->schemalessAttributes, $name); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function forget(string $name): self |
72
|
|
|
{ |
73
|
|
|
$this->model->{$this->sourceAttributeName} = array_except($this->schemalessAttributes, $name); |
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function all(): array |
79
|
|
|
{ |
80
|
|
|
return $this->getRawSchemalessAttributes(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function offsetExists($offset) |
84
|
|
|
{ |
85
|
|
|
return $this->has($offset); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function offsetGet($offset) |
89
|
|
|
{ |
90
|
|
|
return $this->$offset; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function offsetSet($offset, $value) |
94
|
|
|
{ |
95
|
|
|
$this->{$offset} = $value; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function offsetUnset($offset) |
99
|
|
|
{ |
100
|
|
|
$this->forget($offset); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function count() |
104
|
|
|
{ |
105
|
|
|
return count($this->schemalessAttributes); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function toArray(): array |
109
|
|
|
{ |
110
|
|
|
return $this->all(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public static function scopeWithSchemalessAttributes(string $attributeName): Builder |
114
|
|
|
{ |
115
|
|
|
$arguments = debug_backtrace()[1]['args']; |
116
|
|
|
|
117
|
|
|
if (count($arguments) === 1) { |
118
|
|
|
[$builder] = $arguments; |
|
|
|
|
119
|
|
|
$schemalessAttributes = []; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if (count($arguments) === 2) { |
123
|
|
|
[$builder, $schemalessAttributes] = $arguments; |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if (count($arguments) >= 3) { |
127
|
|
|
[$builder, $name, $value] = $arguments; |
|
|
|
|
128
|
|
|
$schemalessAttributes = [$name => $value]; |
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
foreach ($schemalessAttributes as $name => $value) { |
132
|
|
|
$builder->where("{$attributeName}->{$name}", $value); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $builder; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
protected function getRawSchemalessAttributes(): array |
139
|
|
|
{ |
140
|
|
|
return json_decode($this->model->getAttributes()[$this->sourceAttributeName] ?? '{}', true); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
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.