Completed
Push — master ( 0cc479...87b452 )
by Christopher
01:29
created

BitMask::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 10
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Cruxinator\BitMask;
4
5
use function array_key_exists;
6
use BadMethodCallException;
7
use MyCLabs\Enum\Enum;
8
9
abstract class BitMask extends Enum
10
{
11
    protected function isFlagSet(int $flag) : bool
12
    {
13
        return (($this->value & $flag) == $flag);
14
    }
15
16
    protected function setFlag(int $flag, bool $value)
17
    {
18
        if ($value) {
19
            $this->value |= $flag;
20
        } else {
21
            $this->value &= ~$flag;
22
        }
23
        return $this;
24
    }
25
26
    /**
27
     * @param $name
28
     * @param $arguments
29
     * @throws \ReflectionException
30
     * @return bool|self
31
     */
32
    public function __call($name, $arguments)
33
    {
34
        $array = static::toArray();
35
        $sub =  array_reduce(['is','set'], function($carry, $item ) use ($name){
36
            return substr($name,0,strlen($item)) === $item ? strlen($item) : $carry;
37
        },false);
38
        
39
        if($sub !== false){
40
            $actualName = substr($name, $sub);
41
            if (isset($array[$actualName]) || array_key_exists($actualName, $array)) {
42
                return $sub === 2 ? $this->isFlagSet($array[$actualName]) : $this->setFlag($array[$actualName], $arguments[0]);
43
            }
44
        }
45
        throw new BadMethodCallException(sprintf('Method %s not found on Class %s', $name, get_class($this)));
46
47
    }
48
49
    /**
50
     * @param $value
51
     * @throws \ReflectionException
52
     * @return bool
53
     */
54
    public static function isValid($value)
55
    {
56
        $min = min(static::toArray());
57
        $max = max(static::toArray()) * 2 - 1;
58
        return $value >= $min && $value <= $max;
59
    }
60
61
    /**
62
     * @throws \ReflectionException
63
     * @return array
64
     */
65
    public static function toArray()
66
    {
67
        $array = parent::toArray();
68
        //TODO: check that the array is defined correctly.
69
        /*$array = array_filter($array, function ($temp) {
70
            $raw = log($temp, 2);
71
            return is_int($temp) && 0.01 > abs($raw - round($raw));
72
        }
73
        );*/
74
        return $array;
75
    }
76
77
    /**
78
     * @throws \ReflectionException
79
     * @return array|mixed
80
     */
81
    public function getKey()
82
    {
83
        $value = $this->value;
84
        $f = array_filter(static::toArray(), function (
85
            /* @noinspection PhpUnusedParameterInspection needed for function def */ $key) use (&$value) {
86
            $isSet = $value & 1;
87
            $value = $value >> 1;
88
            return $isSet;
89
        });
90
91
        return array_keys($f);
92
    }
93
94
    /**
95
     * @throws \ReflectionException
96
     * @return string
97
     */
98
    public function __toString()
99
    {
100
        $name = $this->getName();
101
        $array = static::toArray();
102
        $ret = '';
103
        foreach ($array as $key => $value) {
104
            $ret .= "'" . $key . "' => " . ($this->{'is' . $key}() ? 'TRUE' : 'FALSE') . PHP_EOL;
105
        }
106
        return $name . '[' . PHP_EOL .
107
            $ret .
108
            ']'. PHP_EOL;
109
    }
110
111
    public function getName()
112
    {
113
        $path = explode('\\', __CLASS__);
114
        return array_pop($path);
115
    }
116
}
117