Completed
Push — master ( 0e13aa...d87a8f )
by Basenko
20s queued 10s
created

Enum::isAny()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
namespace MadWeb\Enum;
4
5
use MadWeb\Enum\Rules\EnumRule;
6
use MyCLabs\Enum\Enum as MyCLabsEnum;
7
8
abstract class Enum extends MyCLabsEnum
9
{
10
    /**
11
     * Default enum value.
12
     */
13
    const __default = null;
14
15
    /**
16
     * {@inheritdoc}
17
     */
18 42
    public function __construct($value = null)
19
    {
20 42
        if (is_null($value)) {
21 18
            $value = static::__default;
22
        }
23
24 42
        parent::__construct($value);
25 42
    }
26
27 3
    public static function randomKey(): string
28
    {
29 3
        $keys = self::keys();
30
31 3
        return $keys[array_rand($keys)];
32
    }
33
34 3
    public static function randomValue(): string
35
    {
36 3
        $values = self::values();
37
38 3
        return $values[array_rand($values)];
39
    }
40
41 6
    public function label(): string
42
    {
43 6
        return self::getLabel($this->getValue());
44
    }
45
46 3
    public static function labels(): array
47
    {
48 3
        $result = [];
49
50 3
        foreach (static::toArray() as $value) {
51 3
            $result[$value] = static::getLabel($value);
0 ignored issues
show
Bug introduced by
Since getLabel() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of getLabel() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
52
        }
53
54 3
        return $result;
55
    }
56
57 9
    private static function getLabel(string $value): string
58
    {
59 9
        $lang_key = sprintf(
60 9
            '%s.%s.%s',
61 9
            config('enum.lang_file_path'),
62 9
            static::class,
63 6
            $value
64
        );
65
66 9
        return trans()->has($lang_key) ? __($lang_key) : $value;
67
    }
68
69 9
    public function is($value)
70
    {
71 9
        if (is_iterable($value)) {
72 3
            return $this->isAny($value);
73
        }
74
75 9
        return $this->getValue() === ($value instanceof self ? $value->getValue() : $value);
76
    }
77
78 3
    public function isAny(iterable $values)
79
    {
80 3
        foreach ($values as $value) {
81 3
            if ($this->is($value)) {
82 3
                return true;
83
            }
84
        }
85
86 3
        return false;
87
    }
88
89 87
    public static function toArray(bool $include_default = false): array
90
    {
91 87
        $result = parent::toArray();
92
93 87
        if (! $include_default) {
94 87
            unset($result['__default']);
95
        }
96
97 87
        return $result;
98
    }
99
100
    /**
101
     * Returns all consts (possible values) as an array according to `SplEnum` class.
102
     */
103 6
    public function getConstList(bool $include_default = false): array
104
    {
105 6
        return static::toArray($include_default);
106
    }
107
108
    /**
109
     * Returns the validation rule (to validate by value).
110
     *
111
     * @return EnumRule
112
     */
113 6
    public static function rule(): EnumRule
114
    {
115 6
        return new EnumRule(static::class, false);
116
    }
117
118
    /**
119
     * Returns the validation rule (to validate by key).
120
     *
121
     * @return EnumRule
122
     */
123 6
    public static function ruleByKey(): EnumRule
124
    {
125 6
        return new EnumRule(static::class, true);
126
    }
127
}
128