Completed
Push — master ( 9edc9e...97252c )
by Nate
03:14 queued 56s
created

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