1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SimpleArrayLibrary\Categories; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use UnexpectedValueException; |
7
|
|
|
|
8
|
|
|
trait Changers |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @param array $config |
12
|
|
|
* @param array $keys |
13
|
|
|
* @param mixed $value |
14
|
|
|
* |
15
|
|
|
* @return array |
16
|
|
|
*/ |
17
|
|
|
public static function addConfigRow(array $config, array $keys, $value) |
18
|
|
|
{ |
19
|
|
|
// validation |
20
|
|
|
if (self::isAssociative($keys)) { |
21
|
|
|
throw new UnexpectedValueException('Array of config keys must be numeric'); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
$row = $value; |
25
|
|
|
for ($i = count($keys) - 1; $i >= 0; $i--) { |
26
|
|
|
$row = [$keys[$i] => $row]; |
27
|
|
|
} |
28
|
|
|
$config = self::insertSubArray($config, $row); |
29
|
|
|
|
30
|
|
|
return $config; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param array $matrix |
35
|
|
|
* @param array $castMap |
36
|
|
|
* @param bool $allKeysMustBePresent |
37
|
|
|
* |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
|
|
public static function castColumns(array $matrix, array $castMap, $allKeysMustBePresent = true) |
41
|
|
|
{ |
42
|
|
|
if (!is_bool($allKeysMustBePresent)) { |
43
|
|
|
throw new InvalidArgumentException('Third parameter must be a boolean'); |
44
|
|
|
} |
45
|
|
|
if (empty($matrix)) { |
46
|
|
|
return $matrix; |
47
|
|
|
} |
48
|
|
|
if (self::countMinDepth($matrix) < 2) { |
49
|
|
|
throw new UnexpectedValueException('Can not cast columns on one dimensional array'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
foreach ($matrix as $key => $row) { |
53
|
|
|
foreach ($castMap as $column => $type) { |
54
|
|
|
if (isset($row[$column]) || array_key_exists($column, $row)) { |
55
|
|
|
switch ($type) { |
56
|
|
|
case self::TYPE_INT: |
57
|
|
|
$matrix[$key][$column] = (int)$row[$column]; |
58
|
|
|
break; |
59
|
|
|
case self::TYPE_STRING: |
60
|
|
|
$matrix[$key][$column] = (string)$row[$column]; |
61
|
|
|
break; |
62
|
|
|
case self::TYPE_FLOAT: |
63
|
|
|
$matrix[$key][$column] = (float)$row[$column]; |
64
|
|
|
break; |
65
|
|
|
case self::TYPE_BOOL: |
66
|
|
|
$matrix[$key][$column] = (bool)$row[$column]; |
67
|
|
|
break; |
68
|
|
|
case self::TYPE_ARRAY: |
69
|
|
|
$matrix[$key][$column] = (array)$row[$column]; |
70
|
|
|
break; |
71
|
|
|
case self::TYPE_OBJECT: |
72
|
|
|
$matrix[$key][$column] = (object)$row[$column]; |
73
|
|
|
break; |
74
|
|
|
default: |
75
|
|
|
throw new UnexpectedValueException('Invalid type: ' . $type); |
76
|
|
|
} |
77
|
|
|
} elseif ($allKeysMustBePresent) { |
78
|
|
|
throw new UnexpectedValueException('Column: ' . $column . ' missing in row: ' . $key); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $matrix; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param array $matrix |
88
|
|
|
* @param mixed $columns |
89
|
|
|
* |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
|
|
public static function deleteColumns(array $matrix, array $columns) |
93
|
|
|
{ |
94
|
|
|
// validate input |
95
|
|
|
if (self::countMinDepth($matrix) < 2) { |
96
|
|
|
throw new UnexpectedValueException('Can not delete columns on one dimensional array'); |
97
|
|
|
} |
98
|
|
|
if (self::countMinDepth($columns) != 1) { |
99
|
|
|
throw new InvalidArgumentException('Invalid column'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// remove columns in every row |
103
|
|
|
foreach ($matrix as $key => $row) { |
104
|
|
|
foreach ($columns as $column) { |
105
|
|
|
unset($matrix[$key][$column]); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $matrix; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param mixed $array |
114
|
|
|
* @param mixed $subArray |
115
|
|
|
* @param bool $overwrite |
116
|
|
|
* @param bool $ignoreIfExists |
117
|
|
|
* |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
|
|
public static function insertSubArray($array, $subArray, $overwrite = false, $ignoreIfExists = false) |
121
|
|
|
{ |
122
|
|
|
// validate |
123
|
|
|
if (!is_bool($overwrite)) { |
124
|
|
|
throw new InvalidArgumentException('Overwrite indicator must be a boolean'); |
125
|
|
|
} |
126
|
|
|
if (!is_bool($ignoreIfExists)) { |
127
|
|
|
throw new InvalidArgumentException('Ignore if exists indicator must be a boolean'); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if (!is_array($subArray) || !is_array($array)) { |
131
|
|
|
// $subArray[$key] is leaf of the array |
132
|
|
|
if ($overwrite) { |
133
|
|
|
$array = $subArray; |
134
|
|
|
} elseif (!$ignoreIfExists) { |
135
|
|
|
throw new UnexpectedValueException('Sub-array already exists'); |
136
|
|
|
} |
137
|
|
|
} else { |
138
|
|
|
$key = key($subArray); |
139
|
|
|
if (isset($array[$key]) || array_key_exists($key, $array)) { |
140
|
|
|
$array[$key] = self::insertSubArray($array[$key], $subArray[$key], $overwrite, $ignoreIfExists); |
141
|
|
|
} else { |
142
|
|
|
$array[$key] = $subArray[$key]; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $array; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param array $matrix |
151
|
|
|
* @param mixed $column |
152
|
|
|
* @param mixed $value |
153
|
|
|
* @param bool $insertIfMissing |
154
|
|
|
* @param bool $overwrite |
155
|
|
|
* |
156
|
|
|
* @return array |
157
|
|
|
*/ |
158
|
|
|
public static function setColumn(array $matrix, $column, $value, $insertIfMissing = true, $overwrite = true) |
159
|
|
|
{ |
160
|
|
|
// validate input |
161
|
|
|
if (self::countMinDepth($matrix) < 2) { |
162
|
|
|
throw new UnexpectedValueException('Can not set columns on one dimensional array'); |
163
|
|
|
} |
164
|
|
|
if (!is_scalar($column)) { |
165
|
|
|
throw new InvalidArgumentException('Invalid column'); |
166
|
|
|
} |
167
|
|
|
if (!is_bool($insertIfMissing)) { |
168
|
|
|
throw new InvalidArgumentException('Insert if missing indicator must be a boolean'); |
169
|
|
|
} |
170
|
|
|
if (!is_bool($overwrite)) { |
171
|
|
|
throw new InvalidArgumentException('Overwrite indicator must be a boolean'); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
foreach ($matrix as $key => $row) { |
175
|
|
|
if (isset($row[$column]) || array_key_exists($column, $row)) { |
176
|
|
|
$matrix[$key][$column] = ($overwrite ? $value : $matrix[$key][$column]); |
177
|
|
|
} elseif ($insertIfMissing) { |
178
|
|
|
$matrix[$key][$column] = $value; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return $matrix; |
183
|
|
|
} |
184
|
|
|
} |