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