1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
//---------------------------------------------------------------------- |
4
|
|
|
// |
5
|
|
|
// Copyright (C) 2015 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
|
|
|
/** @var array Shared cache of all dictionaries. */ |
20
|
|
|
private static $cache = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Returns the dictionary as associated array. |
24
|
|
|
* |
25
|
|
|
* @return array |
26
|
|
|
*/ |
27
|
5 |
|
private static function getFromCache() |
28
|
|
|
{ |
29
|
5 |
|
$dictionary = get_called_class(); |
30
|
|
|
|
31
|
5 |
|
if (!array_key_exists($dictionary, self::$cache)) { |
32
|
2 |
|
self::$cache[$dictionary] = static::all(); |
33
|
2 |
|
} |
34
|
|
|
|
35
|
5 |
|
return self::$cache[$dictionary]; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
1 |
|
public static function get($key) |
42
|
|
|
{ |
43
|
1 |
|
$dictionary = static::getFromCache(); |
|
|
|
|
44
|
|
|
|
45
|
1 |
|
if (array_key_exists($key, $dictionary)) { |
46
|
1 |
|
return $dictionary[$key]; |
47
|
|
|
} |
48
|
|
|
else { |
49
|
1 |
|
return static::FALLBACK === null ? null : $dictionary[static::FALLBACK]; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
1 |
|
public static function has($key) |
57
|
|
|
{ |
58
|
1 |
|
$dictionary = static::getFromCache(); |
|
|
|
|
59
|
|
|
|
60
|
1 |
|
return array_key_exists($key, $dictionary); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
1 |
|
public static function find($value) |
67
|
|
|
{ |
68
|
1 |
|
$dictionary = static::getFromCache(); |
|
|
|
|
69
|
|
|
|
70
|
1 |
|
$key = array_search($value, $dictionary); |
71
|
|
|
|
72
|
1 |
|
return $key === false ? static::FALLBACK : $key; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
1 |
|
public static function keys() |
79
|
|
|
{ |
80
|
1 |
|
$dictionary = static::getFromCache(); |
|
|
|
|
81
|
|
|
|
82
|
1 |
|
return array_keys($dictionary); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
1 |
|
public static function values() |
89
|
|
|
{ |
90
|
1 |
|
$dictionary = static::getFromCache(); |
|
|
|
|
91
|
|
|
|
92
|
1 |
|
return array_values($dictionary); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
Let’s assume you have a class which uses late-static binding:
}
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:In the case above, it makes sense to update
SomeClass
to useself
instead: