Enumerable::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace yiicod\base\models\enumerables\base;
4
5
/**
6
 * Enumerable is the base class for all enumerable types.
7
 *
8
 * To define an enumerable type, extend Enumberable and define string constants.
9
 * Each constant represents an enumerable value.
10
 * The constant name must be the same as the constant value.
11
 * For example,
12
 * <pre>
13
 * class TextAlign extends Enumerable
14
 * {
15
 *     const Left='Left';
16
 *     const Right='Right';
17
 * }
18
 * </pre>
19
 * Then, one can use the enumerable values such as TextAlign::Left and
20
 * TextAlign::Right.
21
 *
22
 * @author Alexey Orlov <[email protected]>
23
 */
24
abstract class Enumerable
25
{
26
    /**
27
     * Get label of the enumerable
28
     *
29
     * @static
30
     *
31
     * @param $value
32
     *
33
     * @return mixed
34
     */
35
    public static function get($value)
36
    {
37
        $list = static::data();
38
39
        return isset($list[$value]) ? $list[$value] : $value;
40
    }
41
42
    /**
43
     * Get list of the enumerable
44
     *
45
     * @static
46
     *
47
     * @param array $exclude
48
     *
49
     * @return array
50
     */
51
    public static function listData(array $exclude = []): array
52
    {
53
        $list = static::data();
54
        foreach ($exclude as $item) {
55
            unset($list[$item]);
56
        }
57
58
        return $list;
59
    }
60
61
    /**
62
     * Check if value in into keys
63
     *
64
     * @param $value
65
     *
66
     * @return bool
67
     */
68
    public static function inKeys($value): bool
69
    {
70
        return in_array($value, array_keys(static::listData([])));
71
    }
72
73
    /**
74
     * Check if value in into values
75
     *
76
     * @param $value
77
     *
78
     * @return bool
79
     */
80
    public static function inValues($value): bool
81
    {
82
        return in_array($value, array_keys(static::listData([])));
83
    }
84
85
    /**
86
     * Get list of the enumerable
87
     *
88
     * @static
89
     *
90
     * @return array
91
     */
92
    abstract protected static function data(): array;
93
}
94