Completed
Pull Request — master (#5)
by Evgenii
03:43
created

Enum::values()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
crap 1
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 18
    public function __construct($value)
31
    {
32 18
        if (!static::constants()->containsStrict($value)) {
33 3
            throw new UnexpectedValueException(sprintf(
34 3
                'Value `%s` is not part of the enum %s',
35 1
                $value,
36 3
                static::class
37 1
            ));
38
        }
39
40 18
        $this->value = $value;
41 18
    }
42
43
    /**
44
     * @return string
45
     */
46 3
    public function __toString()
47
    {
48 3
        return (string)$this->value;
49
    }
50
51
    /**
52
     * Get the enum key.
53
     *
54
     * @return mixed
55
     */
56 3
    public function getKey()
57
    {
58 3
        return static::constants()->search($this->value, true);
59
    }
60
61
    /**
62
     * Get the enum value.
63
     *
64
     * @return mixed
65
     */
66 3
    public function getValue()
67
    {
68 3
        return $this->value;
69
    }
70
71
    /**
72
     * Get the enum keys.
73
     *
74
     * @return array
75
     */
76 3
    public static function keys()
77
    {
78 3
        return static::constants()
79 3
            ->keys()
80 3
            ->all();
81
    }
82
83
    /**
84
     * Get the enum values.
85
     *
86
     * @return array
87
     */
88 3
    public static function values()
89
    {
90 3
        return static::constants()
91 3
            ->map(function ($value) {
92 3
                return new static($value);
93 3
            })
94 3
            ->all();
95
    }
96
97
    /**
98
     * Get the enum constants.
99
     *
100
     * @return \Illuminate\Support\Collection
101
     */
102 27
    public static function constants()
103
    {
104 27
        if (!isset(static::$constants[static::class])) {
105 3
            static::$constants[static::class] = collect(
106 3
                (new ReflectionClass(static::class))->getConstants()
107 1
            );
108 1
        }
109
110 27
        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 6
    public static function __callStatic($name, array $arguments)
122
    {
123 6
        if (static::constants()->has($name)) {
124 3
            return new static(static::constants()->get($name));
125
        }
126
127 3
        throw new BadMethodCallException(sprintf(
128 3
            'No static method or enum constant `%s` in class %s',
129 1
            $name,
130 3
            static::class
131 1
        ));
132
    }
133
}
134