Enum   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
lcom 2
cbo 3
dl 0
loc 157
ccs 45
cts 45
cp 1
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
langKey() 0 1 ?
A __construct() 0 5 1
A allValues() 0 10 3
A collect() 0 4 1
A ofType() 0 14 2
A value() 0 4 1
A validateValue() 0 6 2
A isValid() 0 10 2
A getDeclaredConstants() 0 6 1
A toSelect() 0 10 1
A line() 0 10 1
A langKeyPrefix() 0 4 1
1
<?php
2
3
namespace DavidIanBonner\Enumerated;
4
5
use Exception;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Facades\Lang;
8
use ReflectionClass;
9
10
abstract class Enum
11
{
12
    /** @var string */
13
    private $value;
14
15
    /** @var array */
16
    private static $loaded = [];
17
18
    /** @var array */
19
    protected static $values = [];
20
21
    abstract public function langKey(): string;
22
23
    /**
24
     * @param string $value
25
     */
26 10
    public function __construct($value)
27
    {
28 10
        $this->validateValue($value);
29 7
        $this->value = $value;
30 7
    }
31
32
    /**
33
     * Return the enumerators values.
34
     *
35
     * @param bool $keys
36
     * @return array
37
     */
38 28
    public static function allValues($keys = false): array
39
    {
40 28
        $caller = get_called_class();
41
42 28
        if (!isset(static::$values[$caller])) {
43 11
            static::$values[$caller] = static::getDeclaredConstants();
44
        }
45
46 28
        return $keys ? static::$values[$caller] : array_values(static::$values[$caller]);
47
    }
48
49
    /**
50
     * Return a collection of the declared values.
51
     *
52
     * @param  bool $keys
53
     * @return Illuminate\Support\Collection
54
     */
55 7
    public static function collect($keys = false): Collection
56
    {
57 7
        return Collection::make(static::allValues($keys));
58
    }
59
60
    /**
61
     * Return an instance of a desired value.
62
     *
63
     * @param  string $value
64
     * @return DavidIanBonner\Enumerated\Enum
65
     */
66 13
    public static function ofType($value): self
67
    {
68 13
        $key = get_called_class() . ':' . $value;
69
70 13
        if (!isset(self::$loaded[$key])) {
71 10
            self::$loaded[$key] = new static($value);
72
        }
73
74
        // We can safely return the instance from the loaded array as
75
        // validation is carried out in the constructor and the
76
        // $loaded property is private.
77
78 10
        return self::$loaded[$key];
79
    }
80
81
    /**
82
     * Return the value.
83
     *
84
     * @return string
85
     */
86 10
    public function value(): string
87
    {
88 10
        return $this->value;
89
    }
90
91
    /**
92
     * Validate the value.
93
     *
94
     * @param  mixed $value
95
     * @throws DavidIanBonner\Enumerated\EnumNotValidException
96
     * @return void
97
     */
98 16
    public static function validateValue($value)
99
    {
100 16
        if (!in_array($value, static::allValues())) {
101 6
            throw new EnumNotValidException("The value [{$value}] is not a valid type.");
102
        }
103 13
    }
104
105
    /**
106
     * Check the value is valid and return a bool.
107
     *
108
     * @param  mixed  $value
109
     * @return bool
110
     */
111 3
    public static function isValid($value): bool
112
    {
113
        try {
114 3
            static::validateValue($value);
115 3
        } catch (Exception $e) {
116 3
            return false;
117
        }
118
119 3
        return true;
120
    }
121
122
    /**
123
     * Get the declared constants.
124
     *
125
     * @return array
126
     */
127 8
    protected static function getDeclaredConstants(): array
128
    {
129 8
        $reflection = new ReflectionClass(get_called_class());
130
131 8
        return (array) $reflection->getConstants();
132
    }
133
134
    /**
135
     * @return array
136
     */
137 4
    public static function toSelect(): array
138
    {
139 4
        return static::collect()
140 4
            ->mapWithKeys(function ($key) {
141 4
                $outcome = self::ofType($key);
142
143 4
                return [$outcome->value() => $outcome->line()];
144 4
            })
145 4
            ->toArray();
146
    }
147
148
    /**
149
     * @return string
150
     */
151 7
    public function line(): string
152
    {
153 7
        return Lang::get(
154 7
            implode('.', [
155 7
                $this->langKeyPrefix() . 'enum',
156 7
                $this->langKey(),
157 7
                $this->value(),
158
            ])
159
        );
160
    }
161
162 5
    public function langKeyPrefix(): string
163
    {
164 5
        return '';
165
    }
166
}
167