Failed Conditions
Pull Request — master (#5)
by Laurens
02:35
created

AbstractEnum::equals()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

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