Completed
Push — master ( 74ba61...67fb5e )
by Alex
15s queued 13s
created

Enum::getKey()   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 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
6
namespace AlgoWeb\ODataMetadata\Enums;
7
8
use BadMethodCallException;
9
10
abstract class Enum extends \MyCLabs\Enum\Enum
11
{
12
    protected function isFlag($flag): bool
13
    {
14
        return $this->value === $flag;
15
    }
16
17
    /**
18
     * @param $name
19
     * @param $arguments
20
     * @throws \ReflectionException
21
     * @return bool
22
     */
23
    public function __call($name, $arguments)
24
    {
25
        $array     = static::toArray();
26
        $regexBase = '/(is)(%s)/m';
27
        $regexFull = sprintf($regexBase, implode('$|', array_keys($array)));
28
        preg_match($regexFull, $name, $match);
29
        if (count($match)>0 && $match[0] === $name) {
30
            return $this->{$match[1] . 'Flag'}($array[$match[2]], $arguments[0] ?? null);
31
        }
32
        throw new BadMethodCallException(sprintf('Enum %s not found on %s', $name, get_class($this)));
33
    }
34
35
    /**
36
     * @param  self ...$candidates
37
     * @return bool
38
     */
39
    public function isAnyOf(self ...$candidates): bool
40
    {
41
        foreach ($candidates as $candidate) {
42
            if ($this->isFlag($candidate->getValue())) {
43
                return true;
44
            }
45
        }
46
        return false;
47
    }
48
49
    /**
50
     * Returns the enum key (i.e. the constant name).
51
     *
52
     * @return string
53
     */
54
    public function getKey()
55
    {
56
        return strval(parent::getKey());
57
    }
58
}
59