Completed
Push — master ( 7f10cd...099423 )
by Ivannis Suárez
02:07
created

Enum::ensure()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
rs 9.2
cc 4
eloc 6
nc 4
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Cubiche\Core\Enum;
13
14
use Cubiche\Core\Equatable\EquatableInterface;
15
use MyCLabs\Enum\Enum as BaseEnum;
16
17
/**
18
 * Enum class.
19
 *
20
 * @method static static __DEFAULT()
21
 *
22
 * @author Karel Osorio Ramírez <[email protected]>
23
 */
24
abstract class Enum extends BaseEnum implements EquatableInterface
25
{
26
    /**
27
     * @var array
28
     */
29
    protected static $defaultCache = array();
30
31
    /**
32
     * @param mixed $value
33
     *
34
     * @return bool
35
     */
36
    public function is($value)
37
    {
38
        return $this->value === $value;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function equals($other)
45
    {
46
        return \get_class($this) === \get_class($other) && $this->getValue() === $other->getValue();
47
    }
48
49
    /**
50
     * @param Enum $enum
51
     * @param Enum $default
52
     *
53
     * @throws \InvalidArgumentException
54
     *
55
     * @return static
56
     */
57
    public static function ensure(Enum $enum = null, Enum $default = null)
58
    {
59
        if ($enum instanceof static) {
60
            return $enum;
61
        }
62
        if ($enum === null) {
63
            return $default === null ? static::__DEFAULT() : static::ensure($default);
64
        }
65
66
        throw new \InvalidArgumentException(\sprintf('The enum parameter must be a %s instance', static::class));
67
    }
68
69
    /**
70
     * @param string $name
71
     * @param array  $arguments
72
     *
73
     * @return static
74
     *
75
     * @throws \BadMethodCallException
76
     */
77
    public static function __callStatic($name, $arguments)
78
    {
79
        $array = static::toArray();
80
        if (\strtoupper($name) === '__DEFAULT' && isset(static::$defaultCache[static::class])) {
81
            return new static(static::$defaultCache[static::class]);
82
        }
83
84
        if (isset($array[$name])) {
85
            return new static($array[$name]);
86
        }
87
88
        throw new \BadMethodCallException("No static method or enum constant '$name' in class ".static::class);
89
    }
90
91
    /**
92
     * @return array
93
     */
94
    public static function toArray()
95
    {
96
        if (!isset(static::$cache[static::class])) {
97
            static::$cache[static::class] = self::constants(static::class);
98
            if (!isset(static::$defaultCache[static::class]) && !empty(static::$cache[static::class])) {
99
                static::$defaultCache[static::class] = \array_values(static::$cache[static::class])[0];
100
            }
101
        }
102
103
        return static::$cache[static::class];
104
    }
105
106
    /**
107
     * @param string $class
108
     *
109
     * @return array
110
     */
111
    private static function constants($class)
112
    {
113
        $reflection = new \ReflectionClass($class);
114
        $constants = $reflection->getConstants();
115
        foreach ($constants as $name => $value) {
116
            if (\strtoupper($name) === '__DEFAULT') {
117
                static::$defaultCache[$class] = $value;
118
                unset($constants[$name]);
119
            }
120
        }
121
122
        return $constants;
123
    }
124
}
125