1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Smoren\TypeTools; |
6
|
|
|
|
7
|
|
|
use ArrayAccess; |
8
|
|
|
|
9
|
|
|
class MapAccessor |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @template T |
13
|
|
|
* @param array<string, T>|ArrayAccess<string, T>|object|mixed $container |
14
|
|
|
* @param string $key |
15
|
|
|
* @param T|null $defaultValue |
16
|
|
|
* @return T|null |
17
|
|
|
*/ |
18
|
114 |
|
public static function get($container, string $key, $defaultValue = null) |
19
|
|
|
{ |
20
|
|
|
switch(true) { |
21
|
114 |
|
case is_array($container): |
22
|
16 |
|
return static::getFromArray($container, $key, $defaultValue); |
23
|
98 |
|
case $container instanceof ArrayAccess: |
24
|
16 |
|
return static::getFromArrayAccess($container, $key, $defaultValue); |
25
|
82 |
|
case is_object($container): |
26
|
34 |
|
return static::getFromObject($container, $key, $defaultValue); |
27
|
|
|
} |
28
|
|
|
|
29
|
48 |
|
return $defaultValue; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param array<string, mixed>|ArrayAccess<string, mixed>|object|mixed $container |
34
|
|
|
* @param string $key |
35
|
|
|
* @return bool |
36
|
|
|
*/ |
37
|
63 |
|
public static function exists($container, string $key): bool |
38
|
|
|
{ |
39
|
|
|
switch(true) { |
40
|
63 |
|
case is_array($container): |
41
|
10 |
|
return static::existsInArray($container, $key); |
42
|
53 |
|
case $container instanceof ArrayAccess: |
43
|
10 |
|
return static::existsInArrayAccess($container, $key); |
44
|
43 |
|
case is_object($container): |
45
|
19 |
|
return static::existsInObject($container, $key); |
46
|
|
|
} |
47
|
24 |
|
return false; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @template T |
52
|
|
|
* @param array<string, T> $container |
53
|
|
|
* @param string $key |
54
|
|
|
* @param T|null $defaultValue |
55
|
|
|
* @return T|null |
56
|
|
|
*/ |
57
|
16 |
|
protected static function getFromArray(array $container, string $key, $defaultValue) |
58
|
|
|
{ |
59
|
16 |
|
if(static::existsInArray($container, $key)) { |
60
|
2 |
|
return $container[$key]; |
61
|
|
|
} |
62
|
|
|
|
63
|
14 |
|
return $defaultValue ?? null; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @template T |
68
|
|
|
* @param array<string, T> $container |
69
|
|
|
* @param string $key |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
26 |
|
protected static function existsInArray(array $container, string $key): bool |
73
|
|
|
{ |
74
|
26 |
|
return array_key_exists($key, $container); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @template T |
79
|
|
|
* @param ArrayAccess<string, T> $container |
80
|
|
|
* @param string $key |
81
|
|
|
* @param T|null $defaultValue |
82
|
|
|
* @return T|null |
83
|
|
|
*/ |
84
|
16 |
|
protected static function getFromArrayAccess(ArrayAccess $container, string $key, $defaultValue) |
85
|
|
|
{ |
86
|
16 |
|
if(static::existsInArrayAccess($container, $key)) { |
87
|
2 |
|
return $container[$key]; |
88
|
|
|
} |
89
|
|
|
|
90
|
14 |
|
return $defaultValue ?? null; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @template T |
95
|
|
|
* @param ArrayAccess<string, T> $container |
96
|
|
|
* @param string $key |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
26 |
|
protected static function existsInArrayAccess(ArrayAccess $container, string $key): bool |
100
|
|
|
{ |
101
|
26 |
|
return $container->offsetExists($key); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param object $container |
106
|
|
|
* @param string $key |
107
|
|
|
* @param mixed|null $defaultValue |
108
|
|
|
* @return mixed|null |
109
|
|
|
*/ |
110
|
34 |
|
protected static function getFromObject(object $container, string $key, $defaultValue) |
111
|
|
|
{ |
112
|
34 |
|
if(ObjectAccessor::hasAccessibleProperty($container, $key)) { |
113
|
10 |
|
return ObjectAccessor::getPropertyValue($container, $key); |
114
|
|
|
} |
115
|
|
|
|
116
|
24 |
|
return $defaultValue; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param object $container |
121
|
|
|
* @param string $key |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
19 |
|
protected static function existsInObject(object $container, string $key): bool |
125
|
|
|
{ |
126
|
19 |
|
return ObjectAccessor::hasPublicProperty($container, $key) |
127
|
19 |
|
|| ObjectAccessor::hasPropertyAccessibleByGetter($container, $key); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|