-
Notifications
You must be signed in to change notification settings - Fork 703
Expand file tree
/
Copy pathArrayHelper.php
More file actions
206 lines (181 loc) · 6.67 KB
/
Copy pathArrayHelper.php
File metadata and controls
206 lines (181 loc) · 6.67 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\helpers;
use Craft;
/**
* Class ArrayHelper
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.0
*/
class ArrayHelper extends \yii\helpers\ArrayHelper
{
// Public Methods
// =========================================================================
/**
* @inheritdoc
*/
public static function toArray($object, $properties = [], $recursive = true): array
{
if ($object === null) {
return [];
}
if (is_string($object) && strpos($object, ',') !== false) {
Craft::$app->getDeprecator()->log('ArrayHelper::toArray(string)', 'Passing a string to ArrayHelper::toArray() has been deprecated. Use StringHelper::split() instead.');
// Split it on the non-escaped commas
$object = preg_split('/(?<!\\\),/', $object);
// Remove any of the backslashes used to escape the commas
foreach ($object as $key => $val) {
// Remove leading/trailing whitespace
$val = trim($val);
// Remove any backslashes used to escape commas
$val = str_replace('\,', ',', $val);
$object[$key] = $val;
}
// Remove any empty elements and reset the keys
$object = array_merge(array_filter($object, function($value) {
return $value !== '';
}));
return $object;
}
return parent::toArray($object, $properties, $recursive);
}
/**
* Prepends or appends a value to an array.
*
* @param array &$array the array to be prepended/appended to
* @param mixed $value the value to prepend/append to the array
* @param bool $prepend `true` will prepend the value; `false` will append it
*/
public static function prependOrAppend(array &$array, $value, bool $prepend)
{
if ($prepend) {
array_unshift($array, $value);
} else {
$array[] = $value;
}
}
/**
* Filters an array to only the values where a given key (the name of a
* sub-array key or sub-object property) is set to a given value.
* Array keys are preserved.
*
* @param array|\Traversable $array the array that needs to be indexed or grouped
* @param string|\Closure $key the column name or anonymous function which result will be used to index the array
* @param mixed $value the value that $key should be compared with
* @param bool $strict whether a strict type comparison should be used when checking array element values against $value
* @return array the filtered array
*/
public static function filterByValue($array, $key, $value = true, bool $strict = false): array
{
$result = [];
foreach ($array as $i => $element) {
$elementValue = static::getValue($element, $key);
/** @noinspection TypeUnsafeComparisonInspection */
if (($strict && $elementValue === $value) || (!$strict && $elementValue == $value)) {
$result[$i] = $element;
}
}
return $result;
}
/**
* Returns the first value in a given array where a given key (the name of a
* sub-array key or sub-object property) is set to a given value.
*
* @param array|\Traversable $array the array that the value will be searched for in
* @param string|\Closure $key the column name or anonymous function which must be set to $value
* @param mixed $value the value that $key should be compared with
* @param bool $strict whether a strict type comparison should be used when checking array element values against $value
* @return mixed the value, or null if it can't be found
*/
public static function firstWhere($array, $key, $value = true, bool $strict = false)
{
foreach ($array as $i => $element) {
$elementValue = static::getValue($element, $key);
/** @noinspection TypeUnsafeComparisonInspection */
if (($strict && $elementValue === $value) || (!$strict && $elementValue == $value)) {
return $element;
}
}
return null;
}
/**
* Filters empty strings from an array.
*
* @param array $arr
* @return array
*/
public static function filterEmptyStringsFromArray(array $arr): array
{
return array_filter($arr, function($value): bool {
return $value !== '';
});
}
/**
* Returns the first key in a given array.
*
* @param array $arr
* @return string|int|null The first key, whether that is a number (if the array is numerically indexed) or a string, or null if $arr isn’t an array, or is empty.
*/
public static function firstKey(array $arr)
{
/** @noinspection LoopWhichDoesNotLoopInspection */
foreach ($arr as $key => $value) {
return $key;
}
return null;
}
/**
* Returns the first value in a given array.
*
* @param array $arr
* @return mixed The first value, or null if $arr isn’t an array, or is empty.
*/
public static function firstValue(array $arr)
{
return !empty($arr) ? reset($arr) : null;
}
/**
* Renames an item in an array. If the new key already exists in the array and the old key doesn’t,
* the array will be left unchanged.
*
* @param array $array the array to extract value from
* @param string $oldKey old key name of the array element
* @param string $newKey new key name of the array element
* @param mixed $default the default value to be set if the specified old key does not exist
*/
public static function rename(array &$array, string $oldKey, string $newKey, $default = null)
{
if (!array_key_exists($newKey, $array) || array_key_exists($oldKey, $array)) {
$array[$newKey] = static::remove($array, $oldKey, $default);
}
}
/**
* Returns a copy of an array without a given key.
*
* @param array $array
* @param string $key
* @return array
*/
public static function without(array $array, string $key): array
{
static::remove($array, $key);
return $array;
}
/**
* Returns a copy of an array without items with matching the given value.
*
* @param array $array
* @param mixed $value
* @return array
*/
public static function withoutValue(array $array, $value): array
{
static::removeValue($array, $value);
return $array;
}
}