1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cerbero\LaravelEnum; |
6
|
|
|
|
7
|
|
|
use Cerbero\Enum\CasesCollection as BaseCasesCollection; |
8
|
|
|
use Illuminate\Contracts\Database\Eloquent\Castable; |
9
|
|
|
use Illuminate\Contracts\Database\Eloquent\CastsAttributes; |
10
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
11
|
|
|
use Illuminate\Contracts\Support\Jsonable; |
12
|
|
|
use Illuminate\Support\Traits\Conditionable; |
13
|
|
|
use Illuminate\Support\Traits\Macroable; |
14
|
|
|
use Illuminate\Support\Traits\Tappable; |
15
|
|
|
use JsonSerializable; |
16
|
|
|
use Stringable; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The collection of enum cases. |
20
|
|
|
* |
21
|
|
|
* @template TKey of array-key |
22
|
|
|
* @template TValue |
23
|
|
|
* |
24
|
|
|
* @extends BaseCasesCollection<TKey, TValue> |
25
|
|
|
* @implements Arrayable<TKey, TValue> |
26
|
|
|
*/ |
27
|
|
|
class CasesCollection extends BaseCasesCollection implements Arrayable, Castable, Jsonable, JsonSerializable, Stringable |
28
|
|
|
{ |
29
|
|
|
use Conditionable; |
30
|
|
|
use Macroable; |
|
|
|
|
31
|
|
|
use Tappable; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Retrieve the caster to cast the collection. |
35
|
|
|
* |
36
|
|
|
* @param string[] $arguments |
37
|
|
|
* @return CasesCollectionCast<TKey, TValue> |
38
|
|
|
*/ |
39
|
19 |
|
public static function castUsing(array $arguments): CastsAttributes |
40
|
|
|
{ |
41
|
19 |
|
return new CasesCollectionCast($arguments[0] ?? ''); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Retrieve the cast for the given enum. |
46
|
|
|
* |
47
|
|
|
* @param class-string<\UnitEnum> $enum |
|
|
|
|
48
|
|
|
*/ |
49
|
1 |
|
public static function of(string $enum): string |
50
|
|
|
{ |
51
|
1 |
|
return static::class . ':' . $enum; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Turn the collection into a string. |
56
|
|
|
*/ |
57
|
1 |
|
public function __toString(): string |
58
|
|
|
{ |
59
|
1 |
|
return (string) $this->toJson(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Turn the collection into a JSON. |
64
|
|
|
* |
65
|
|
|
* @param int $options |
66
|
|
|
*/ |
67
|
4 |
|
public function toJson($options = 0): string|false |
68
|
|
|
{ |
69
|
4 |
|
return json_encode($this->jsonSerialize(), $options); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Turn the collection into a JSON serializable array. |
74
|
|
|
* |
75
|
|
|
* @return array<TKey, mixed> |
76
|
|
|
*/ |
77
|
9 |
|
public function jsonSerialize(): array |
78
|
|
|
{ |
79
|
9 |
|
return $this->enumIsBacked ? $this->values() : $this->names(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Dump the cases and end the script. |
84
|
|
|
* |
85
|
|
|
* @codeCoverageIgnore |
86
|
|
|
*/ |
87
|
|
|
public function dd(): never |
88
|
|
|
{ |
89
|
|
|
$this->dump(); |
90
|
|
|
|
91
|
|
|
exit(1); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Dump the cases. |
96
|
|
|
*/ |
97
|
1 |
|
public function dump(): static |
98
|
|
|
{ |
99
|
1 |
|
dump($this->cases); |
100
|
|
|
|
101
|
1 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|