Passed
Push — feature/second-release ( 258ce1...c43059 )
by Andrea Marco
02:43
created

CasesCollection::dd()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 0
cts 0
cp 0
crap 2
rs 10
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;
0 ignored issues
show
Bug introduced by
The trait Illuminate\Support\Traits\Macroable requires the property $name which is not provided by Cerbero\LaravelEnum\CasesCollection.
Loading history...
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
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<\UnitEnum> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<\UnitEnum>.
Loading history...
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);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
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