1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the sauls/helpers package. |
4
|
|
|
* |
5
|
|
|
* @author Saulius Vaičeliūnas <[email protected]> |
6
|
|
|
* @link http://saulius.vaiceliunas.lt |
7
|
|
|
* @copyright 2018 |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Sauls\Component\Helper\Operation\ArrayOperation; |
14
|
|
|
|
15
|
|
|
use Sauls\Component\Helper\Operation\ObjectOperation; |
16
|
|
|
|
17
|
|
|
class GetValue implements GetValueInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var ObjectOperation\GetPropertyValueInterface |
21
|
|
|
*/ |
22
|
|
|
private $objectGetPropertyValueOperation; |
23
|
|
|
|
24
|
1 |
|
public function __construct( |
25
|
|
|
ObjectOperation\GetPropertyValueInterface $objectGetPropertyValueOperation, |
26
|
|
|
KeyExistsInterface $arrayKeyExistsOperation |
|
|
|
|
27
|
|
|
) { |
28
|
1 |
|
$this->objectGetPropertyValueOperation = $objectGetPropertyValueOperation; |
29
|
1 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return mixed |
33
|
|
|
* @throws \Sauls\Component\Helper\Exception\PropertyNotAccessibleException |
34
|
|
|
*/ |
35
|
32 |
|
public function execute($array, $key, $default = null) |
36
|
|
|
{ |
37
|
32 |
|
if (\is_callable($key)) { |
38
|
1 |
|
return $key($array, $default); |
39
|
|
|
} |
40
|
|
|
|
41
|
31 |
|
return $this->getValueUsingArrayKey($array, $key, $default); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return mixed |
46
|
|
|
* @throws \Sauls\Component\Helper\Exception\PropertyNotAccessibleException |
47
|
|
|
*/ |
48
|
31 |
|
private function getValueUsingArrayKey($array, $key, $default) |
49
|
|
|
{ |
50
|
31 |
|
if (\is_array($key)) { |
51
|
2 |
|
$lastKey = array_pop($key); |
52
|
2 |
|
foreach ($key as $keyPart) { |
53
|
2 |
|
$array = $this->execute($array, $keyPart); |
54
|
|
|
} |
55
|
2 |
|
$key = $lastKey; |
56
|
|
|
} |
57
|
|
|
|
58
|
31 |
|
if ($this->keyExistsAndValueIsArray($key, $array)) { |
59
|
26 |
|
return $array[$key]; |
60
|
|
|
} |
61
|
|
|
|
62
|
9 |
|
return $this->getValueUsingDotNotatedArrayKey($array, $key, $default); |
63
|
|
|
} |
64
|
|
|
|
65
|
31 |
|
private function keyExistsAndValueIsArray($key, $array): bool |
66
|
|
|
{ |
67
|
31 |
|
return \is_array($array) && (isset($array[$key]) || \array_key_exists($key, $array)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return mixed |
72
|
|
|
* @throws \Sauls\Component\Helper\Exception\PropertyNotAccessibleException |
73
|
|
|
*/ |
74
|
9 |
|
private function getValueUsingDotNotatedArrayKey($array, $key, $default) |
75
|
|
|
{ |
76
|
9 |
|
if ((bool)($pos = strrpos($key, '.')) !== false) { |
77
|
4 |
|
$array = $this->execute($array, substr($key, 0, $pos), $default); |
78
|
4 |
|
$key = substr($key, $pos + 1); |
79
|
|
|
} |
80
|
|
|
|
81
|
9 |
|
if (\is_object($array)) { |
82
|
1 |
|
return $this->objectGetPropertyValueOperation->execute($array, $key) ?? $default; |
83
|
|
|
} |
84
|
|
|
|
85
|
8 |
|
if (\is_array($array)) { |
86
|
6 |
|
return $this->getValueOrFallbackToDefault($array, $key, $default); |
87
|
|
|
} |
88
|
|
|
|
89
|
2 |
|
return $default; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return mixed |
94
|
|
|
*/ |
95
|
6 |
|
private function getValueOrFallbackToDefault($array, $key, $default) |
96
|
|
|
{ |
97
|
6 |
|
return (isset($array[$key]) || \array_key_exists($key, $array)) ? $array[$key] : $default; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.