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