-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathArrayReader.php
More file actions
168 lines (130 loc) · 3.53 KB
/
ArrayReader.php
File metadata and controls
168 lines (130 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/*
* This file is part of the codeliner/array-reader.
* (c) Alexander Miertsch <kontakt@codeliner.ws>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Date: 08.03.14 - 21:28
*/
namespace Codeliner\ArrayReader;
/**
* Class ArrayReader
*
* @package Codeliner\ArrayReader
* @author Alexander Miertsch <kontakt@codeliner.ws>
*/
class ArrayReader
{
/**
* @var array
*/
private $originalArray = [];
/**
* @param array $anArray
*/
public function __construct(array $anArray)
{
$this->originalArray = $anArray;
}
public function integerValue(string $aPath, int $default = 0): int
{
$value = $this->getValueFromPath($aPath);
if ($value === null) {
return $default;
}
return (int)$value;
}
public function floatValue(string $aPath, float $default = 0.0): float
{
$value = $this->getValueFromPath($aPath);
if ($value === null) {
return $default;
}
return (float)$value;
}
public function booleanValue(string $aPath, bool $default = false): bool
{
$value = $this->getValueFromPath($aPath);
if ($value === null) {
return $default;
}
return (bool)$value;
}
public function stringValue(string $aPath, string $default = ''): string
{
$value = $this->getValueFromPath($aPath);
if ($value === null) {
return $default;
}
return (string)$value;
}
public function arrayValue(string $aPath, array $default = []): array
{
$value = $this->getValueFromPath($aPath);
if ($value === null) {
return $default;
}
if (is_scalar($value)) {
return [$value];
}
if (is_object($value)) {
$value = json_decode(json_encode($value), true);
}
return $value;
}
/**
* @param string $aPath
* @param mixed $default
* @return mixed
*/
public function mixedValue(string $aPath, $default = null)
{
$value = $this->getValueFromPath($aPath);
if ($value === null) {
return $default;
}
return $value;
}
public function pathExists(string $aPath): bool
{
$pathKeys = $this->toPathKeys($aPath);
$arrayCopyOrValue = $this->originalArray;
foreach ($pathKeys as $pathKey) {
if (!array_key_exists($pathKey, $arrayCopyOrValue)) {
return false;
}
$arrayCopyOrValue = $arrayCopyOrValue[$pathKey];
}
return true;
}
public function toArray(): array
{
return $this->originalArray;
}
protected function toPathKeys(string $aPath): array
{
$aPath = str_replace('\.', '___IamADot___', $aPath);
$parts = explode('.', $aPath);
return array_map(function ($part) {
return str_replace('___IamADot___', '.', $part);
}, $parts);
}
/**
* @param string $aPath
* @return mixed
*/
protected function getValueFromPath(string $aPath)
{
$pathKeys = $this->toPathKeys($aPath);
$arrayCopyOrValue = $this->originalArray;
foreach ($pathKeys as $pathKey) {
if (!isset($arrayCopyOrValue[$pathKey])) {
return null;
}
$arrayCopyOrValue = $arrayCopyOrValue[$pathKey];
}
return $arrayCopyOrValue;
}
}