Test Setup Failed
Pull Request — master (#7)
by Laurens
01:47
created

AbstractEnum::getInflector()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Werkspot\Enum;
6
7
use BadMethodCallException;
8
use Doctrine\Inflector\Inflector;
9
use Doctrine\Inflector\InflectorFactory;
10
use InvalidArgumentException;
11
use ReflectionClass;
12
use Werkspot\Enum\Util\ClassNameConverter;
13
14
abstract class AbstractEnum
15
{
16
    protected $value;
17 15
18
    protected function __construct($value)
19 15
    {
20 4
        if (!$this->isValid($value)) {
21
            $message = 'Value [%s] is not matching any valid value of class "%s". Valid values are [%s].';
22 4
23 4
            throw new InvalidArgumentException(sprintf(
24 4
                $message,
25 4
                $value,
26 4
                $this->getClassName(),
27
                self::getValidOptionsAsString()
28
            ));
29
        }
30 11
31 11
        $this->value = $value;
32
    }
33
34
    /**
35
     * @return static
36 13
     */
37
    public static function get($value): self
38 13
    {
39
        return new static($value);
40
    }
41 3
42 View Code Duplication
    public static function __callStatic(string $methodName, array $arguments): self
43 3
    {
44 3
        foreach (self::getConstants() as $option => $value) {
45 3
            $expectedMethodName = self::getInflector()->camelize(strtolower($option));
46 3
            if ($expectedMethodName === $methodName) {
47
                return new static($value);
48
            }
49
        }
50 1
51
        throw new BadMethodCallException(sprintf('%s::%s() does not exist.', static::class, $methodName));
52
    }
53 3
54 View Code Duplication
    public function __call(string $methodName, array $arguments)
55 3
    {
56 3
        foreach (self::getConstants() as $option => $value) {
57 3
            $isaMethodName = 'is' . self::getInflector()->classify(strtolower($option));
58 3
            if ($isaMethodName === $methodName) {
59
                return $this->equals(static::get($value));
60
            }
61 1
        }
62
        throw new BadMethodCallException(sprintf('%s::%s() does not exist.', static::class, $methodName));
63
    }
64
65
    /**
66
     * @return mixed
67
     */
68 8
    public function getValue()
69
    {
70 8
        return $this->value;
71
    }
72
73 6
    public function equals(self $other): bool
74
    {
75 6
        return get_class($other) === get_class($this) && $other->getValue() === $this->value;
76
    }
77
78 3
    public function __toString(): string
79
    {
80 3
        return (string) $this->value;
81
    }
82
83 15
    protected function isValid($value): bool
84
    {
85 15
        return in_array($value, self::getValidOptions(), true);
86
    }
87
88 16
    public static function getValidOptions(): array
89
    {
90 16
        return array_values(self::getConstants());
91
    }
92
93 4
    protected function getClassName(): string
94
    {
95 4
        return ClassNameConverter::stripNameSpace(static::class);
96
    }
97
98 17
    private static function getInflector(): Inflector
99
    {
100 17
        return InflectorFactory::create()->build();
101
    }
102
103 4
    private static function getConstants(): array
104
    {
105 4
        return (new ReflectionClass(static::class))->getConstants();
106 4
    }
107 4
108 4
    private static function getValidOptionsAsString(): string
109 4
    {
110
        return implode(
111
            ', ',
112
            array_map(function ($option) {
113
                return var_export($option, true);
114
            }, self::getValidOptions())
115
        );
116
    }
117
}
118