Passed
Push — master ( 57f4bd...7ceb38 )
by Zachary
02:54
created

AbstractEnum::isValid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ZingleCom\Enum;
4
5
use ZingleCom\Enum\Exception\InvalidValueException;
6
use ZingleCom\Enum\Meta\GeneratorFactory;
7
use ZingleCom\Enum\Meta\GeneratorInterface;
8
9
/**
10
 * Class AbstractEnum
11
 */
12
abstract class AbstractEnum implements EnumInterface
13
{
14
    /**
15
     * @var GeneratorInterface
16
     */
17
    private static $generator;
18
19
    /**
20
     * @var string
21
     */
22
    private $constantName;
23
24
    /**
25
     * @var mixed
26
     */
27
    private $value;
28
29
30
    /**
31
     * AbstractEnum constructor.
32
     *
33
     * @param mixed $value
34
     *
35
     * @throws Exception\EnumException
36
     * @throws Exception\MissingValueException
37
     * @throws InvalidValueException
38
     */
39
    public function __construct($value)
40
    {
41
        $meta = self::getMeta();
42
43
        if (!$meta->isValid($value)) {
44
            throw new InvalidValueException($value);
45
        }
46
47
        $this->constantName = $meta->getConstantName($value);
48
        $this->value        = $value;
49
    }
50
51
    /**
52
     * @return string
53
     */
54
    public function getConstantName(): string
55
    {
56
        return $this->constantName;
57
    }
58
59
    /**
60
     * @return mixed
61
     */
62
    public function getValue()
63
    {
64
        return $this->value;
65
    }
66
67
    /**
68
     * @return string
69
     */
70
    public function getDisplayName(): string
71
    {
72
        return mb_convert_case(
73
            strtolower(str_replace('_', ' ', $this->constantName)),
74
            MB_CASE_TITLE,
75
            'UTF-8'
76
        );
77
    }
78
79
    /**
80
     * @param mixed $value
81
     *
82
     * @return bool
83
     */
84
    public function is($value): bool
85
    {
86
        return $value === $this->getValue();
87
    }
88
89
    /**
90
     * @return string
91
     */
92
    public function __toString(): string
93
    {
94
        return strval($this->getValue());
95
    }
96
97
    /**
98
     * @return GeneratorInterface
99
     */
100
    private static function getGenerator(): GeneratorInterface
101
    {
102
        if (null === self::$generator) {
103
            self::$generator = GeneratorFactory::create();
104
        }
105
106
        return self::$generator;
107
    }
108
109
    /**
110
     * @return MetaInterface
111
     */
112
    private static function getMeta(): MetaInterface
113
    {
114
        return self::getGenerator()->get(get_called_class());
115
    }
116
117
    /**
118
     * Makes this compatible with Symfony
119
     * 
120
     * @return array
121
     */
122
    public static function getOptions(): array 
123
    {
124
        return self::getMeta()->getConstants()->toArray();
125
    }
126
127
    /**
128
     * @return array
129
     */
130
    public static function getValues(): array
131
    {
132
        return self::getMeta()->getConstants()->values()->toArray();
133
    }
134
135
    /**
136
     * @param mixed $value
137
     *
138
     * @return bool
139
     */
140
    public static function isValid($value): bool
141
    {
142
        return self::getMeta()->getConstants()->values()->contains($value);
143
    }
144
145
    /**
146
     * @param string $constant
147
     *
148
     * @return bool
149
     */
150
    public static function isValidConstant(string $constant): bool
151
    {
152
        return self::getMeta()->getConstants()->keys()->contains($constant);
153
    }
154
}
155