Enum::jsonSerialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Nasyrov\Laravel\Enums;
4
5
use BadMethodCallException;
6
use ReflectionClass;
7
use UnexpectedValueException;
8
use JsonSerializable;
9
10
abstract class Enum implements JsonSerializable
11
{
12
    /**
13
     * The enum value.
14
     *
15
     * @var mixed
16
     */
17
    protected $value;
18
19
    /**
20
     * The enum constants.
21
     *
22
     * @var array
23
     */
24
    protected static $constants = [];
25
26
    /**
27
     * Create a new enum instance.
28
     *
29
     * @param mixed $value
30
     */
31 28
    public function __construct($value)
32
    {
33 28
        if (!static::constants()->contains($value)) {
34 4
            throw new UnexpectedValueException(sprintf(
35 4
                'Value `%s` is not part of the enum %s',
36 3
                $value,
37 4
                static::class
38
            ));
39
        }
40
41 28
        $this->value = $value;
42 28
    }
43
44
    /**
45
     * @return string
46
     */
47 4
    public function __toString()
48
    {
49 4
        return (string)$this->value;
50
    }
51
52
    /**
53
     * Get the serialized value.
54
     *
55
     * @return mixed
56
     */
57 4
    public function jsonSerialize()
58
    {
59 4
        return $this->value;
60
    }
61
62
    /**
63
     * Get the enum key.
64
     *
65
     * @return mixed
66
     */
67 4
    public function getKey()
68
    {
69 4
        return static::constants()->search($this->value, true);
70
    }
71
72
    /**
73
     * Get the enum value.
74
     *
75
     * @return mixed
76
     */
77 4
    public function getValue()
78
    {
79 4
        return $this->value;
80
    }
81
82
    /**
83
     * Get the enum keys.
84
     *
85
     * @return array
86
     */
87 4
    public static function keys()
88
    {
89 4
        return static::constants()
90 4
            ->keys()
91 4
            ->all();
92
    }
93
94
    /**
95
     * Get the enum values.
96
     *
97
     * @return array
98
     */
99 4
    public static function values()
100
    {
101 4
        return static::constants()
102 1
            ->map(function ($value) {
103 4
                return new static($value);
104 4
            })
105 4
            ->all();
106
    }
107
108
    /**
109
     * Get the enum constants.
110
     *
111
     * @return \Illuminate\Support\Collection
112
     */
113 40
    public static function constants()
114
    {
115 40
        if (!isset(static::$constants[static::class])) {
116 4
            static::$constants[static::class] = collect(
117 4
                (new ReflectionClass(static::class))->getConstants()
118
            );
119
        }
120
121 40
        return static::$constants[static::class];
122
    }
123
124
    /**
125
     * Returns a new enum instance when called statically.
126
     *
127
     * @param string $name
128
     * @param array  $arguments
129
     *
130
     * @return static
131
     */
132 8
    public static function __callStatic($name, array $arguments)
133
    {
134 8
        if (static::constants()->has($name)) {
135 4
            return new static(static::constants()->get($name));
136
        }
137
138 4
        throw new BadMethodCallException(sprintf(
139 4
            'No static method or enum constant `%s` in class %s',
140 3
            $name,
141 4
            static::class
142
        ));
143
    }
144
}
145