Completed
Pull Request — master (#7)
by Evgenii
13:09
created

Enum   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 125
rs 10
c 0
b 0
f 0

8 Methods

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