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

Enum   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 14
dl 0
loc 47
rs 10
c 3
b 1
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isFlag() 0 3 1
A isAnyOf() 0 8 3
A __call() 0 10 3
A getKey() 0 3 1
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