Issues (1)

src/StaticDictionary.php (1 issue)

Severity
1
<?php
2
3
//----------------------------------------------------------------------
4
//
5
//  Copyright (C) 2015-2021 Artem Rodygin
6
//
7
//  You should have received a copy of the MIT License along with
8
//  this file. If not, see <http://opensource.org/licenses/MIT>.
9
//
10
//----------------------------------------------------------------------
11
12
namespace Dictionary;
13
14
/**
15
 * Abstract static dictionary of key/value pairs.
16
 */
17
abstract class StaticDictionary implements StaticDictionaryInterface
18
{
19
    // Fallback key of the default value to return on non-existing keys.
20
    public const FALLBACK = null;
21
22
    /**
23
     * @var array Dictionary values (to be overloaded).
24
     */
25
    protected static array $dictionary = [];
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 1
    public static function all(): array
31
    {
32 1
        return static::dictionary();
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 1
    public static function get($key)
39
    {
40 1
        $dictionary = static::dictionary();
41
42 1
        if (array_key_exists($key, $dictionary)) {
43 1
            return $dictionary[$key];
44
        }
45
46 1
        return static::FALLBACK === null ? null : $dictionary[static::FALLBACK];
0 ignored issues
show
The condition static::FALLBACK === null is always true.
Loading history...
47
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 1
    public static function has($key): bool
54
    {
55 1
        return array_key_exists($key, static::dictionary());
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 1
    public static function find($value)
62
    {
63 1
        $key = array_search($value, static::dictionary(), true);
64
65 1
        return $key === false ? static::FALLBACK : $key;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 1
    public static function keys(): array
72
    {
73 1
        return array_keys(static::dictionary());
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 1
    public static function values(): array
80
    {
81 1
        return array_values(static::dictionary());
82
    }
83
84
    /**
85
     * Returns dictionary.
86
     *
87
     * @return array
88
     */
89 1
    protected static function dictionary(): array
90
    {
91 1
        return static::$dictionary;
92
    }
93
}
94