1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Enum\Laravel\Casts; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
|
7
|
|
|
class EnumCollectionCast extends Cast |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
11
|
|
|
* @param string $key |
12
|
|
|
* @param string|null|mixed $value |
13
|
|
|
* @param array $attributes |
14
|
|
|
* |
15
|
|
|
* @return \Spatie\Enum\Enum[]|null |
16
|
|
|
* |
17
|
|
|
* @throws \BadMethodCallException |
18
|
|
|
* @throws \Spatie\Enum\Laravel\Exceptions\NotNullableEnumField |
19
|
|
|
*/ |
20
|
|
|
public function get($model, string $key, $value, array $attributes) |
21
|
|
|
{ |
22
|
|
|
if (is_null($value)) { |
23
|
|
|
return $this->handleNullValue($model, $key); |
|
|
|
|
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
return $this->asEnums( |
27
|
|
|
Arr::wrap(json_decode($value, true)) |
28
|
|
|
); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
33
|
|
|
* @param string $key |
34
|
|
|
* @param int[]|string[]|\Spatie\Enum\Enum[]|null|mixed $value |
35
|
|
|
* @param array $attributes |
36
|
|
|
* |
37
|
|
|
* @return string|null |
38
|
|
|
*/ |
39
|
|
|
public function set($model, string $key, $value, array $attributes) |
40
|
|
|
{ |
41
|
|
|
if (is_null($value)) { |
42
|
|
|
return $this->handleNullValue($model, $key); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return json_encode($this->asEnums(Arr::wrap($value))); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param int[]|string[]|\Spatie\Enum\Enum[] $values |
50
|
|
|
* |
51
|
|
|
* @return \Spatie\Enum\Enum[] |
52
|
|
|
* |
53
|
|
|
* @throws \TypeError |
54
|
|
|
* @throws \BadMethodCallException |
55
|
|
|
*/ |
56
|
|
|
protected function asEnums(array $values): array |
57
|
|
|
{ |
58
|
|
|
return array_map([$this, 'asEnum'], $values); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.