|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Luminark\Rivet\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Luminark\Rivet\Models\Rivet; |
|
6
|
|
|
use Luminark\Rivet\Events\AttachingToModel; |
|
7
|
|
|
use Luminark\Rivet\Events\AttachedToModel; |
|
8
|
|
|
use Rutorika\Sortable\MorphToSortedManyTrait; |
|
9
|
|
|
use InvalidArgumentException; |
|
10
|
|
|
|
|
11
|
|
|
trait HasRivetsTrait |
|
12
|
|
|
{ |
|
13
|
|
|
use MorphToSortedManyTrait; |
|
14
|
|
|
|
|
15
|
|
|
protected function getRivetsConfig() |
|
16
|
|
|
{ |
|
17
|
|
|
return $this->rivets ?: ['rivets' => []]; |
|
|
|
|
|
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
protected function getRivetClass() |
|
21
|
|
|
{ |
|
22
|
|
|
return Rivet::class; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function attach($attribute, $rivet, $relationShouldLoad = false) |
|
26
|
|
|
{ |
|
27
|
|
|
list($type, $class) = $this->getRivetConfig($attribute); |
|
28
|
|
|
|
|
29
|
|
|
if (is_numeric($rivet)) { |
|
30
|
|
|
$rivet = $class::findOrFail($rivet); |
|
31
|
|
|
} elseif ( ! $rivet instanceof $class) { |
|
32
|
|
|
throw new InvalidArgumentException("Invalid object provided for attaching to $attribute. Expected $class, got " . get_class($rivet) . "."); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
if ($type == 'property') { |
|
36
|
|
|
$this->attachAsProperty($attribute, $rivet); |
|
37
|
|
|
} elseif ($type == 'collection') { |
|
38
|
|
|
$this->attachToCollection($attribute, $rivet); |
|
39
|
|
|
} else { |
|
40
|
|
|
throw new InvalidArgumentException("Invalid type [$type] provided for attaching to $attribute."); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
if ($relationShouldLoad) { |
|
44
|
|
|
$this->load($attribute); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
protected function attachAsProperty($attribute, Rivet $rivet) |
|
49
|
|
|
{ |
|
50
|
|
|
if ($this->$attribute) { |
|
51
|
|
|
$this->removeRivet($attribute, $this->$attribute->id); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $this->attachToCollection($attribute, $rivet); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
protected function attachToCollection($collection, Rivet $rivet) |
|
58
|
|
|
{ |
|
59
|
|
|
$dispatcher = static::getEventDispatcher(); |
|
60
|
|
|
$dispatcher->fire(new AttachingToModel($this, $collection, $rivet)); |
|
61
|
|
|
|
|
62
|
|
|
$rivet->save(); |
|
63
|
|
|
$this->$collection()->attach($rivet, ['collection' => $collection]); |
|
64
|
|
|
|
|
65
|
|
|
$dispatcher->fire(new AttachedToModel($this, $collection, $rivet)); |
|
66
|
|
|
|
|
67
|
|
|
return $rivet; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function removeRivet($attribute, $rivet, $relationShouldLoad = false) |
|
71
|
|
|
{ |
|
72
|
|
|
list(, $class) = $this->getRivetConfig($attribute); |
|
73
|
|
|
|
|
74
|
|
|
$result = null; |
|
|
|
|
|
|
75
|
|
|
if (is_numeric($rivet)) { |
|
76
|
|
|
$result = $this->$attribute()->detach($rivet); |
|
77
|
|
|
} elseif ($rivet instanceof $class) { |
|
78
|
|
|
$result = $this->$attribute()->detach($rivet->id); |
|
79
|
|
|
} else { |
|
80
|
|
|
throw new InvalidArgumentException('Only an ID or a Rivet object can be passed to removeRivet method.'); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
if ( ! $result) { |
|
84
|
|
|
throw new InvalidArgumentException('Unrelated ID or Rivet object passed to removeRivet method.'); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
if ($relationShouldLoad) { |
|
88
|
|
|
$this->load($attribute); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $this; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function __call($name, $arguments) |
|
95
|
|
|
{ |
|
96
|
|
View Code Duplication |
if (preg_match('/^set(\w+)/', $name, $matches) |
|
|
|
|
|
|
97
|
|
|
&& in_array($property = lcfirst($matches[1]), array_keys($this->getRivetsConfig())) |
|
98
|
|
|
) { |
|
99
|
|
|
array_unshift($arguments, $property); |
|
100
|
|
|
return call_user_func_array([$this, 'attach'], $arguments); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
View Code Duplication |
if (preg_match('/^add(\w+)/', $name, $matches) |
|
|
|
|
|
|
104
|
|
|
&& in_array($collection = str_plural(lcfirst($matches[1])), array_keys($this->getRivetsConfig())) |
|
105
|
|
|
) { |
|
106
|
|
|
array_unshift($arguments, $collection); |
|
107
|
|
|
return call_user_func_array([$this, 'attach'], $arguments); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
View Code Duplication |
if (preg_match('/^unset(\w+)/', $name, $matches) |
|
|
|
|
|
|
111
|
|
|
&& in_array($property = lcfirst($matches[1]), array_keys($this->getRivetsConfig())) |
|
112
|
|
|
) { |
|
113
|
|
|
array_unshift($arguments, $property); |
|
114
|
|
|
return call_user_func_array([$this, 'removeRivet'], $arguments); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
View Code Duplication |
if (preg_match('/^remove(\w+)/', $name, $matches) |
|
|
|
|
|
|
118
|
|
|
&& in_array($collection = str_plural(lcfirst($matches[1])), array_keys($this->getRivetsConfig())) |
|
119
|
|
|
) { |
|
120
|
|
|
array_unshift($arguments, $collection); |
|
121
|
|
|
return call_user_func_array([$this, 'removeRivet'], $arguments); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
if ( ! in_array($name, array_keys($this->getRivetsConfig()))) { |
|
125
|
|
|
return parent::__call($name, $arguments); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
list($type, $class) = $this->getRivetConfig($name); |
|
129
|
|
|
|
|
130
|
|
|
if ($type == 'property') { |
|
131
|
|
|
return $this->getRivetProperty($class, $name); |
|
132
|
|
|
} |
|
133
|
|
|
if ($type == 'collection') { |
|
134
|
|
|
return $this->getRivetCollection($class, $name); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function __get($name) |
|
139
|
|
|
{ |
|
140
|
|
|
$result = parent::__get($name); |
|
141
|
|
|
|
|
142
|
|
|
if ( ! in_array($name, array_keys($this->getRivetsConfig()))) { |
|
143
|
|
|
return $result; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
if ( ! $result) { |
|
147
|
|
|
$this->load($name); |
|
148
|
|
|
$result = $this->getAttribute($name); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
list($type) = $this->getRivetConfig($name); |
|
152
|
|
|
if ($type == 'property') { |
|
153
|
|
|
return $result->first(); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
return $result; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
protected function getRivetConfig($name) |
|
160
|
|
|
{ |
|
161
|
|
|
return $this->getRivetsConfig()[$name] |
|
162
|
|
|
+ ['collection', $this->getRivetClass()]; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
protected function getRivetProperty($class, $name) |
|
166
|
|
|
{ |
|
167
|
|
|
return $this->getRivetCollection($class, $name); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
protected function getRivetCollection($class, $name) |
|
171
|
|
|
{ |
|
172
|
|
|
return $this->morphToSortedMany( |
|
173
|
|
|
$class, |
|
174
|
|
|
$class::getMorphToManyName(), |
|
175
|
|
|
'position', |
|
176
|
|
|
null, |
|
177
|
|
|
null, |
|
178
|
|
|
$class::getMorphToSortableManyOtherKey() |
|
179
|
|
|
)->wherePivot('collection', snake_case($name)); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
abstract public function getAttribute($key); |
|
183
|
|
|
|
|
184
|
|
|
abstract public function load($relations); |
|
185
|
|
|
} |
|
186
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.