Completed
Push — master ( 76d300...01172c )
by Zachary
01:32
created

AbstractEnum   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 110
rs 10
c 0
b 0
f 0
wmc 11

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getConstantName() 0 3 1
A getGenerator() 0 7 2
A __toString() 0 3 1
A getOptions() 0 3 1
A getMeta() 0 3 1
A is() 0 3 1
A __construct() 0 10 2
A getValue() 0 3 1
A getDisplayName() 0 6 1
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
     * @param mixed $value
33
     * @throws Exception\EnumException
34
     * @throws Exception\MissingValueException
35
     * @throws InvalidValueException
36
     */
37
    public function __construct($value)
38
    {
39
        $meta = self::getMeta();
40
41
        if (!$meta->isValid($value)) {
42
            throw new InvalidValueException($value);
43
        }
44
45
        $this->constantName = $meta->getConstantName($value);
46
        $this->value        = $value;
47
    }
48
49
    /**
50
     * @return string
51
     */
52
    public function getConstantName(): string
53
    {
54
        return $this->constantName;
55
    }
56
57
    /**
58
     * @return mixed
59
     */
60
    public function getValue()
61
    {
62
        return $this->value;
63
    }
64
65
    /**
66
     * @return string
67
     */
68
    public function getDisplayName(): string
69
    {
70
        return mb_convert_case(
71
            strtolower(str_replace('_', ' ', $this->constantName)),
72
            MB_CASE_TITLE,
73
            'UTF-8'
74
        );
75
    }
76
77
    /**
78
     * @param mixed $value
79
     * @return bool
80
     */
81
    public function is($value): bool
82
    {
83
        return $value === $this->getValue();
84
    }
85
86
    /**
87
     * @return string
88
     */
89
    public function __toString(): string
90
    {
91
        return strval($this->getValue());
92
    }
93
94
    /**
95
     * @return GeneratorInterface
96
     */
97
    private static function getGenerator(): GeneratorInterface
98
    {
99
        if (null === self::$generator) {
100
            self::$generator = GeneratorFactory::create();
101
        }
102
103
        return self::$generator;
104
    }
105
106
    /**
107
     * @return MetaInterface
108
     */
109
    private static function getMeta(): MetaInterface
110
    {
111
        return self::getGenerator()->get(get_called_class());
112
    }
113
114
    /**
115
     * Makes this compatible with Symfony
116
     * 
117
     * @return array
118
     */
119
    public static function getOptions(): array 
120
    {
121
        return array_flip(self::getMeta()->getConstants()->toArray());
0 ignored issues
show
Bug Best Practice introduced by
The expression return array_flip(self::...Constants()->toArray()) could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
122
    }
123
}
124