Completed
Push — master ( 8ece48...a8f1f6 )
by Nate
01:47
created

AbstractEnum::getIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/*
3
 * Copyright (c) Nate Brunette.
4
 * Distributed under the MIT License (http://opensource.org/licenses/MIT)
5
 */
6
7
namespace Tebru\Enum;
8
9
use BadMethodCallException;
10
use RuntimeException;
11
12
/**v
13
 * Class AbstractEnum
14
 *
15
 * @author Nate Brunette <[email protected]>
16
 */
17
abstract class AbstractEnum implements EnumInterface
18
{
19
    /**
20
     * @var string
21
     */
22
    private $value;
23
24
    /**
25
     * An array of created instances to ensure singleton
26
     *
27
     * @var array
28
     */
29
    private static $instances = [];
30
31
    /**
32
     * Constructor
33
     *
34
     * @param string $value
35
     * @throws RuntimeException If the value is not valid
36
     */
37
    final private function __construct($value)
38
    {
39
        if (!static::exists($value)) {
40
            throw new RuntimeException(sprintf('%s is not a valid value for this enum.', $value));
41
        }
42
43
        $this->value = $value;
44
    }
45
46
    /**
47
     * Return the value as a string
48
     *
49
     * @return string
50
     */
51
    public function __toString()
52
    {
53
        return (string) $this->value;
54
    }
55
56
    /**
57
     * Do a comparison to check if the enum values are equal
58
     *
59
     * @param EnumInterface $enum
60
     * @return bool
61
     */
62
    public function equals(EnumInterface $enum)
63
    {
64
        return $this->getValue() === $enum->getValue();
65
    }
66
67
    /**
68
     * Create a new instance of the enum
69
     *
70
     * @param string $value
71
     * @return AbstractEnum
72
     * @throws RuntimeException If the value is not valid
73
     */
74
    public static function create($value)
75
    {
76
        if (array_key_exists($value, self::$instances)) {
77
            return self::$instances[$value];
78
        }
79
80
        $instance = new static($value);
81
        self::$instances[$value] = $instance;
82
83
        return $instance;
84
    }
85
86
    /**
87
     * Return true if the value is valid for the enum
88
     *
89
     * @param string $value
90
     * @return bool
91
     */
92
    public static function exists($value)
93
    {
94
        return in_array($value, static::values(), true);
95
    }
96
97
    /**
98
     * Get an array of the enum values
99
     *
100
     * @return array
101
     */
102
    public static function values()
103
    {
104
        return array_values(static::getConstants());
105
    }
106
107
    /**
108
     * Return enum as an array with keys matching values
109
     *
110
     * @return array
111
     */
112
    public static function toArray()
113
    {
114
        return array_combine(self::values(), self::values());
115
    }
116
117
    /**
118
     * Get the current enum value
119
     *
120
     * @return mixed
121
     */
122
    public function getValue()
123
    {
124
        return $this->value;
125
    }
126
127
    /**
128
     * Allow enum instantiation by magic method
129
     *
130
     * @param string $name
131
     * @param $arguments
132
     * @return static
133
     * @throws RuntimeException If the value is not valid
134
     * @throws BadMethodCallException If the constant doesn't exist in the class
135
     */
136
    public static function __callStatic($name, $arguments)
137
    {
138
        $constant = @constant('static::' . $name);
139
140
        if (null === $constant) {
141
            throw new BadMethodCallException(sprintf('Could not find constant "%s" for class "%s"', $name, static::class));
142
        }
143
144
        return self::create($constant);
145
    }
146
}
147