1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\SchemalessAttributes; |
4
|
|
|
|
5
|
|
|
use Countable; |
6
|
|
|
use ArrayAccess; |
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
9
|
|
|
|
10
|
|
|
class SchemalessAttributes implements ArrayAccess, Countable |
11
|
|
|
{ |
12
|
|
|
/** @var \Illuminate\Database\Eloquent\Model */ |
13
|
|
|
protected $model; |
14
|
|
|
|
15
|
|
|
/** @var string */ |
16
|
|
|
protected $sourceAttributeName; |
17
|
|
|
|
18
|
|
|
/** @var array */ |
19
|
|
|
protected $schemalessAttributes = []; |
20
|
|
|
|
21
|
|
|
public static function createForModel(Model $model, string $sourceAttributeName): self |
22
|
|
|
{ |
23
|
|
|
return new static($model, $sourceAttributeName); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function __construct(Model $model, string $sourceAttributeName) |
27
|
|
|
{ |
28
|
|
|
$this->model = $model; |
29
|
|
|
|
30
|
|
|
$this->sourceAttributeName = $sourceAttributeName; |
31
|
|
|
|
32
|
|
|
$this->schemalessAttributes = $this->getRawSchemalessAttributes(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function __get(string $name) |
36
|
|
|
{ |
37
|
|
|
return $this->get($name); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function get(string $name) |
41
|
|
|
{ |
42
|
|
|
return array_get($this->schemalessAttributes, $name); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function __set(string $name, $value) |
46
|
|
|
{ |
47
|
|
|
$this->set($name, $value); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function set(string $name, $value) |
51
|
|
|
{ |
52
|
|
|
array_set($this->schemalessAttributes, $name, $value); |
53
|
|
|
|
54
|
|
|
$this->model->{$this->sourceAttributeName} = $this->schemalessAttributes; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function forget(string $name): self |
58
|
|
|
{ |
59
|
|
|
$this->model->{$this->sourceAttributeName} = array_except($this->schemalessAttributes, $name); |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function all(): array |
65
|
|
|
{ |
66
|
|
|
return $this->getRawSchemalessAttributes(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function getRawSchemalessAttributes(): array |
70
|
|
|
{ |
71
|
|
|
return json_decode($this->model->getAttributes()[$this->sourceAttributeName] ?? '{}', true); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function offsetExists($offset) |
75
|
|
|
{ |
76
|
|
|
return array_has($this->schemalessAttributes, $offset); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function offsetGet($offset) |
80
|
|
|
{ |
81
|
|
|
return $this->$offset; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function offsetSet($offset, $value) |
85
|
|
|
{ |
86
|
|
|
$this->{$offset} = $value; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function offsetUnset($offset) |
90
|
|
|
{ |
91
|
|
|
$this->forget($offset); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function count() |
95
|
|
|
{ |
96
|
|
|
return count($this->schemalessAttributes); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public static function scopeWithSchemalessAttributes(string $attributeName): Builder |
100
|
|
|
{ |
101
|
|
|
$arguments = debug_backtrace()[1]['args']; |
102
|
|
|
|
103
|
|
|
if (count($arguments) === 1) { |
104
|
|
|
[$builder] = $arguments; |
|
|
|
|
105
|
|
|
$schemalessAttributes = []; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if (count($arguments) === 2) { |
109
|
|
|
[$builder, $schemalessAttributes] = $arguments; |
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if (count($arguments) >= 3) { |
113
|
|
|
[$builder, $name, $value] = $arguments; |
|
|
|
|
114
|
|
|
$schemalessAttributes = [$name => $value]; |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
foreach ($schemalessAttributes as $name => $value) { |
118
|
|
|
$builder->where("{$attributeName}->{$name}", $value); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $builder; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
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.