1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\ModelStates; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
7
|
|
|
use Spatie\ModelStates\Exceptions\InvalidConfig; |
8
|
|
|
use Spatie\ModelStates\Exceptions\CouldNotPerformTransition; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @mixin \Illuminate\Database\Eloquent\Model |
12
|
|
|
*/ |
13
|
|
|
trait HasStates |
14
|
|
|
{ |
15
|
|
|
/** @var \Spatie\ModelStates\StateConfig[]|null */ |
16
|
|
|
protected static $stateFields = null; |
17
|
|
|
|
18
|
|
|
abstract protected function registerStates(): void; |
19
|
|
|
|
20
|
|
|
public static function bootHasStates(): void |
21
|
|
|
{ |
22
|
|
|
$serialiseState = function (StateConfig $stateConfig) { |
23
|
|
|
return function (Model $model) use ($stateConfig) { |
24
|
|
|
$value = $model->getAttribute($stateConfig->field); |
25
|
|
|
|
26
|
|
|
if ($value === null) { |
27
|
|
|
$value = $stateConfig->defaultStateClass; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
if ($value === null) { |
31
|
|
|
return; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$stateClass = $stateConfig->stateClass::resolveStateClass($value); |
35
|
|
|
|
36
|
|
|
if (! is_subclass_of($stateClass, $stateConfig->stateClass)) { |
37
|
|
|
throw InvalidConfig::fieldDoesNotExtendState( |
38
|
|
|
$stateConfig->field, |
39
|
|
|
$stateConfig->stateClass, |
40
|
|
|
$stateClass |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$model->setAttribute( |
45
|
|
|
$stateConfig->field, |
46
|
|
|
State::resolveStateName($value) |
47
|
|
|
); |
48
|
|
|
}; |
49
|
|
|
}; |
50
|
|
|
|
51
|
|
|
$unserialiseState = function (StateConfig $stateConfig) { |
52
|
|
|
return function (Model $model) use ($stateConfig) { |
53
|
|
|
$stateClass = $stateConfig->stateClass::resolveStateClass($model->getAttribute($stateConfig->field)); |
54
|
|
|
|
55
|
|
|
$defaultState = $stateConfig->defaultStateClass |
56
|
|
|
? new $stateConfig->defaultStateClass($model) |
57
|
|
|
: null; |
58
|
|
|
|
59
|
|
|
$model->setAttribute( |
60
|
|
|
$stateConfig->field, |
61
|
|
|
class_exists($stateClass) |
62
|
|
|
? new $stateClass($model) |
63
|
|
|
: $defaultState |
64
|
|
|
); |
65
|
|
|
}; |
66
|
|
|
}; |
67
|
|
|
|
68
|
|
|
foreach (self::getStateConfig() as $stateConfig) { |
69
|
|
|
static::retrieved($unserialiseState($stateConfig)); |
70
|
|
|
static::created($unserialiseState($stateConfig)); |
71
|
|
|
static::saved($unserialiseState($stateConfig)); |
72
|
|
|
|
73
|
|
|
static::updating($serialiseState($stateConfig)); |
74
|
|
|
static::creating($serialiseState($stateConfig)); |
75
|
|
|
static::saving($serialiseState($stateConfig)); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function initializeHasStates(): void |
80
|
|
|
{ |
81
|
|
|
foreach (self::getStateConfig() as $stateConfig) { |
82
|
|
|
if (! $stateConfig->defaultStateClass) { |
83
|
|
|
continue; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$this->{$stateConfig->field} = new $stateConfig->defaultStateClass($this); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function scopeWhereState(Builder $builder, string $field, $states): Builder |
91
|
|
|
{ |
92
|
|
|
self::getStateConfig(); |
93
|
|
|
|
94
|
|
|
/** @var \Spatie\ModelStates\StateConfig|null $stateConfig */ |
95
|
|
|
$stateConfig = self::getStateConfig()[$field] ?? null; |
96
|
|
|
|
97
|
|
|
if (! $stateConfig) { |
98
|
|
|
throw InvalidConfig::unknownState($field, $this); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$abstractStateClass = $stateConfig->stateClass; |
102
|
|
|
|
103
|
|
|
$stateNames = collect((array) $states)->map(function ($state) use ($abstractStateClass) { |
104
|
|
|
return $abstractStateClass::resolveStateName($state); |
105
|
|
|
}); |
106
|
|
|
|
107
|
|
|
return $builder->whereIn($field, $stateNames); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function scopeWhereNotState(Builder $builder, string $field, $states): Builder |
111
|
|
|
{ |
112
|
|
|
/** @var \Spatie\ModelStates\StateConfig|null $stateConfig */ |
113
|
|
|
$stateConfig = self::getStateConfig()[$field] ?? null; |
114
|
|
|
|
115
|
|
|
if (! $stateConfig) { |
116
|
|
|
throw InvalidConfig::unknownState($field, $this); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$stateNames = collect((array) $states)->map(function ($state) use ($stateConfig) { |
120
|
|
|
return $stateConfig->stateClass::resolveStateName($state); |
121
|
|
|
}); |
122
|
|
|
|
123
|
|
|
return $builder->whereNotIn($field, $stateNames); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param \Spatie\ModelStates\State|string $state |
128
|
|
|
* @param string|null $field |
129
|
|
|
*/ |
130
|
|
|
public function transitionTo($state, string $field = null) |
131
|
|
|
{ |
132
|
|
|
$stateConfig = self::getStateConfig(); |
133
|
|
|
|
134
|
|
|
if ($field === null && count($stateConfig) > 1) { |
135
|
|
|
throw CouldNotPerformTransition::couldNotResolveTransitionField($this); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$field = $field ?? reset($stateConfig)->field; |
139
|
|
|
|
140
|
|
|
$this->{$field}->transitionTo($state); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function transitionableStates(string $fromClass, ?string $field = null): array |
144
|
|
|
{ |
145
|
|
|
$stateConfig = self::getStateConfig(); |
146
|
|
|
|
147
|
|
|
if ($field === null && count($stateConfig) > 1) { |
148
|
|
|
throw InvalidConfig::fieldNotFound($fromClass, $this); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$field = $field ?? reset($stateConfig)->field; |
152
|
|
|
|
153
|
|
|
if (! array_key_exists($field, $stateConfig)) { |
154
|
|
|
throw InvalidConfig::unknownState($field, $this); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $stateConfig[$field]->transitionableStates($fromClass); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param string $fromClass |
162
|
|
|
* @param string $toClass |
163
|
|
|
* |
164
|
|
|
* @return \Spatie\ModelStates\Transition|string|null |
165
|
|
|
*/ |
166
|
|
|
public function resolveTransitionClass(string $fromClass, string $toClass) |
167
|
|
|
{ |
168
|
|
|
foreach (static::getStateConfig() as $stateConfig) { |
169
|
|
|
$transitionClass = $stateConfig->resolveTransition($this, $fromClass, $toClass); |
170
|
|
|
|
171
|
|
|
if ($transitionClass) { |
|
|
|
|
172
|
|
|
return $transitionClass; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
throw CouldNotPerformTransition::notFound($fromClass, $toClass, $this); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
protected function addState(string $field, string $stateClass): StateConfig |
180
|
|
|
{ |
181
|
|
|
$stateConfig = new StateConfig($field, $stateClass); |
182
|
|
|
|
183
|
|
|
static::$stateFields[$stateConfig->field] = $stateConfig; |
184
|
|
|
|
185
|
|
|
return $stateConfig; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @return \Spatie\ModelStates\StateConfig[] |
190
|
|
|
*/ |
191
|
|
|
private static function getStateConfig(): array |
192
|
|
|
{ |
193
|
|
|
if (static::$stateFields === null) { |
194
|
|
|
static::$stateFields = []; |
195
|
|
|
|
196
|
|
|
(new static)->registerStates(); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return static::$stateFields ?? []; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: