Completed
Pull Request — master (#16)
by
unknown
10:57
created

Enum   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 135
ccs 36
cts 36
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 2
A __toString() 0 4 1
A jsonSerialize() 0 4 1
A getKey() 0 4 1
A getValue() 0 4 1
A keys() 0 6 1
A values() 0 8 1
A constants() 0 10 2
A __callStatic() 0 12 2
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 12
     */
31
    public function __construct($value)
32 12
    {
33 2
        if (!static::constants()->contains($value)) {
34 2
            throw new UnexpectedValueException(sprintf(
35 2
                'Value `%s` is not part of the enum %s',
36 2
                $value,
37
                static::class
38
            ));
39
        }
40 12
41 12
        $this->value = $value;
42
    }
43
44
    /**
45
     * @return string
46 2
     */
47
    public function __toString()
48 2
    {
49
        return (string)$this->value;
50
    }
51
52
    /**
53
     * Serialize enum value
54
     *
55
     * @return mixed
56 2
     */
57
    public function jsonSerialize()
58 2
    {
59
        return $this->value;
60
    }
61
62
    /**
63
     * Get the enum key.
64
     *
65
     * @return mixed
66 2
     */
67
    public function getKey()
68 2
    {
69
        return static::constants()->search($this->value, true);
70
    }
71
72
    /**
73
     * Get the enum value.
74
     *
75
     * @return mixed
76 2
     */
77
    public function getValue()
78 2
    {
79 2
        return $this->value;
80 2
    }
81
82
    /**
83
     * Get the enum keys.
84
     *
85
     * @return array
86
     */
87
    public static function keys()
88 2
    {
89
        return static::constants()
90 2
            ->keys()
91 2
            ->all();
92 2
    }
93 2
94 2
    /**
95
     * Get the enum values.
96
     *
97
     * @return array
98
     */
99
    public static function values()
100
    {
101
        return static::constants()
102 18
            ->map(function ($value) {
103
                return new static($value);
104 18
            })
105 2
            ->all();
106 2
    }
107
108
    /**
109
     * Get the enum constants.
110 18
     *
111
     * @return \Illuminate\Support\Collection
112
     */
113
    public static function constants()
114
    {
115
        if (!isset(static::$constants[static::class])) {
116
            static::$constants[static::class] = collect(
117
                (new ReflectionClass(static::class))->getConstants()
118
            );
119
        }
120
121 4
        return static::$constants[static::class];
122
    }
123 4
124 2
    /**
125
     * Returns a new enum instance when called statically.
126
     *
127 2
     * @param string $name
128 2
     * @param array  $arguments
129 2
     *
130 2
     * @return static
131
     */
132
    public static function __callStatic($name, array $arguments)
133
    {
134
        if (static::constants()->has($name)) {
135
            return new static(static::constants()->get($name));
136
        }
137
138
        throw new BadMethodCallException(sprintf(
139
            'No static method or enum constant `%s` in class %s',
140
            $name,
141
            static::class
142
        ));
143
    }
144
}
145