Completed
Push — master ( a62642...790996 )
by Max
01:10
created

EnumTrait::checkKey()   A

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
namespace drycart\data;
3
4
/*
5
 *  @copyright (c) 2019 Mendel <[email protected]>
6
 *  @license see license.txt
7
 */
8
9
/**
10
 * Trait for pretty use for list of constants and/or data arrays
11
 *
12
 * @author mendel
13
 */
14
trait EnumTrait
15
{
16
    /**
17
     * Array for titles. Just default dummy for override if need
18
     * 
19
     * @return array
20
     */
21
    static protected function titles() : array
22
    {
23
        return [];
24
    }
25
    
26
    /**
27
     * Main function. Return list of constants i.e. keys and values...
28
     * 
29
     * @return array
30
     */
31
    static public function data() : array
32
    {
33
        // @2DO: add assertion for check if values is uniqal
34
        $reflector = new \ReflectionClass(static::class);
35
        return $reflector->getConstants();
36
    }
37
    
38
    /**
39
     * Get iterator for titles/keys/values
40
     * 
41
     * @return \Iterator
42
     */
43
    static public function titlesIterator() : \Iterator
44
    {
45
        $titles = static::titles();
46
        foreach(static::data() as $key=>$value) {
47
            yield new DataWrapper([
48
                'key' => $key,
49
                'value' => $value,
50
                'title' => $titles[$key] ?? StrHelper::key2Label(strtolower($key))
51
            ]);
52
        }
53
    }
54
    
55
    /**
56
     * Return all keys and his titles (defined at titles() or default generated)
57
     * 
58
     * @return array
59
     */
60
    static public function keyTitles() : array
61
    {
62
        $titles = static::titles();
63
        $result = [];
64
        foreach(array_keys(static::data()) as $key) {
65
            $result[$key] = $titles[$key] ?? StrHelper::key2Label(strtolower($key));
66
        }
67
        return $result;
68
    }
69
    
70
    /**
71
     * Return all values and his titles (defined at titles() or default generated)
72
     * 
73
     * @return array
74
     */
75
    static public function valueTitles() : array
76
    {
77
        $titles = static::titles();
78
        $result = [];
79
        foreach(static::data() as $key=>$value) {
80
            $result[$value] = $titles[$key] ?? StrHelper::key2Label(strtolower($key));
81
        }
82
        return $result;
83
    }
84
        
85
    /**
86
     * Get value by key
87
     * @param string $key
88
     * @return mixed
89
     */
90
    static public function value(string $key)
91
    {
92
        return static::data()[$key] ?? null;
93
    }
94
    
95
    /**
96
     * Get key by value
97
     * @param mixed $value
98
     * @return string|null
99
     */
100
    static public function key($value) : ?string
101
    {
102
        $data = array_flip(static::data());
103
        return $data[$value] ?? null;
104
    }
105
    
106
    static public function checkKey(string $key) : void
107
    {
108
        if(in_null(static::value($key))) {
109
            throw \Exception();
110
        }
111
    }
112
    
113
}
114