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
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The collection of enum cases. |
18
|
|
|
* |
19
|
|
|
* @template TEnum of \UnitEnum |
20
|
|
|
* |
21
|
|
|
* @extends BaseCasesCollection<TEnum> |
22
|
|
|
* @implements Arrayable<array-key, TEnum> |
23
|
|
|
*/ |
24
|
|
|
class CasesCollection extends BaseCasesCollection implements Arrayable, Castable, Jsonable |
25
|
|
|
{ |
26
|
|
|
use Conditionable; |
27
|
|
|
use Macroable; |
|
|
|
|
28
|
|
|
use Tappable; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Retrieve the caster to cast the collection. |
32
|
|
|
* |
33
|
|
|
* @param list<string> $arguments |
|
|
|
|
34
|
|
|
* @return CasesCollectionCast<TEnum> |
35
|
|
|
*/ |
36
|
19 |
|
public static function castUsing(array $arguments): CastsAttributes |
37
|
|
|
{ |
38
|
|
|
/** @var CasesCollectionCast<TEnum> */ |
39
|
19 |
|
return new CasesCollectionCast($arguments[0] ?? ''); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Retrieve the cast for the given enum. |
44
|
|
|
* |
45
|
|
|
* @param class-string<TEnum> $enum |
|
|
|
|
46
|
|
|
*/ |
47
|
1 |
|
public static function of(string $enum): string |
48
|
|
|
{ |
49
|
1 |
|
return static::class . ':' . $enum; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Turn the collection into a JSON. |
54
|
|
|
* |
55
|
|
|
* @param int $options |
56
|
|
|
*/ |
57
|
3 |
|
public function toJson($options = 0): string|false |
58
|
|
|
{ |
59
|
3 |
|
return json_encode($this->jsonSerialize(), $options); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Dump the cases and end the script. |
64
|
|
|
* |
65
|
|
|
* @codeCoverageIgnore |
66
|
|
|
*/ |
67
|
|
|
public function dd(): never |
68
|
|
|
{ |
69
|
|
|
$this->dump(); |
70
|
|
|
|
71
|
|
|
exit(1); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Dump the cases. |
76
|
|
|
*/ |
77
|
1 |
|
public function dump(): static |
78
|
|
|
{ |
79
|
1 |
|
dump($this->cases); |
80
|
|
|
|
81
|
1 |
|
return $this; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|