Enum   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 20
c 2
b 1
f 0
dl 0
loc 174
ccs 31
cts 31
cp 1
rs 10
wmc 14

12 Methods

Rating   Name   Duplication   Size   Complexity  
A identifier() 0 3 1
A bootIfNotBooted() 0 7 2
A collection() 0 6 1
A identifiers() 0 3 1
A newCollection() 0 3 1
A get() 0 6 2
A equals() 0 3 1
A exists() 0 3 1
A random() 0 3 1
A __get() 0 3 1
A __toString() 0 3 1
A __callStatic() 0 3 1
1
<?php
2
3
namespace JosKolenberg\Enum;
4
5
use Illuminate\Support\Collection;
6
7
abstract class Enum
8
{
9
    /**
10
     * The array with collections of resolved Enums.
11
     *
12
     * @var array
13
     */
14
    protected static $resolvedInstances = [];
15
16
    /**
17
     * Seed the class with Enum instances.
18
     *
19
     * @return array
20
     */
21
    abstract protected static function seed();
22
23
    /**
24
     * Return the name of the attribute which stores the identifier.
25
     *
26
     * @return string
27
     */
28
    abstract protected function identifierAttribute();
29
30
    /**
31
     * Check if the instances are resolved for the
32
     * current class and if not, resolve them.
33
     *
34
     * @param $class
35
     *
36
     * @return void
37
     */
38 50
    protected static function bootIfNotBooted($class)
39
    {
40 50
        if (isset(static::$resolvedInstances[$class])) {
41 46
            return;
42
        }
43
44 12
        static::$resolvedInstances[$class] = $class::newCollection($class::seed());
45 6
    }
46
47
    /**
48
     * Return a new Collection instance.
49
     *
50
     * @param array $enums
51
     *
52
     * @return EnumCollection
53
     */
54 10
    protected static function newCollection(array $enums = [])
55
    {
56 10
        return new EnumCollection($enums);
57
    }
58
59
    /**
60
     * Get an enum by identifier.
61
     *
62
     * @param $identifier
63
     *
64
     * @throws EnumNotFoundException
65
     *
66
     * @return Enum
67
     */
68 34
    public static function get($identifier)
69
    {
70 34
        if (!static::exists($identifier)) {
71 2
            throw new EnumNotFoundException();
72
        }
73 32
        return static::collection()->byIdentifier($identifier);
74
    }
75
76
    /**
77
     * Return a random Enum.
78
     *
79
     * @return Enum
80
     */
81 2
    public static function random()
82
    {
83 2
        return static::collection()->random();
84
    }
85
86
    /**
87
     * Return a collection with all Enums.
88
     *
89
     * @return Collection
90
     */
91 50
    public static function collection()
92
    {
93 50
        $class = get_called_class();
94 50
        static::bootIfNotBooted($class);
95
96 50
        return static::$resolvedInstances[$class];
97
    }
98
99
    /**
100
     * Make it possible to retrieve an Enum by using it's identifier
101
     * as a function name.
102
     *
103
     * e.g. UserType::get('dev') equals UserType::dev()
104
     *
105
     * @param $name
106
     * @param $arguments
107
     *
108
     * @return Enum
109
     */
110 14
    public static function __callStatic($name, $arguments)
111
    {
112 14
        return static::get($name);
113
    }
114
115
    /**
116
     * Make the attributes readable from the outside.
117
     *
118
     * @param $field
119
     *
120
     * @return mixed
121
     */
122 16
    public function __get($field)
123
    {
124 16
        return $this->$field;
125
    }
126
127
    /**
128
     * Present the instance as the identifier when casted to a string.
129
     *
130
     * @return mixed
131
     */
132 2
    public function __toString()
133
    {
134 2
        return $this->identifier();
135
    }
136
137
    /**
138
     * Get the identifier for the instance.
139
     *
140
     * @throws EnumException
141
     *
142
     * @return mixed
143
     */
144 46
    public function identifier()
145
    {
146 46
        return $this->{$this->identifierAttribute()};
147
    }
148
149
    /**
150
     * Test if the given Enum equals the current.
151
     *
152
     * @param Enum $enum
153
     *
154
     * @return bool
155
     */
156 8
    public function equals(self $enum)
157
    {
158 8
        return $this === $enum;
159
    }
160
161
    /**
162
     * Get a collection with only the identifiers.
163
     *
164
     * @return mixed
165
     */
166 2
    public static function identifiers()
167
    {
168 2
        return static::collection()->identifiers();
169
    }
170
171
    /**
172
     * Check if an Enum with the given identifier exists.
173
     *
174
     * @param $identifier
175
     *
176
     * @return mixed
177
     */
178 36
    public static function exists($identifier)
179
    {
180 36
        return static::collection()->hasIdentifier($identifier);
181
    }
182
}
183