Skip to content

Commit 0659650

Browse files
committed
big reformat - add strict_types=1
1 parent 33cac79 commit 0659650

File tree

7 files changed

+192
-171
lines changed

7 files changed

+192
-171
lines changed

src/ArrayHelper.php

Lines changed: 67 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
/**
44
* Helper class for processing arrays
55
*
66
* Вспомогательный класс для обработки массивов
77
*
88
* @author Leonid Sheikman (leonid74)
9-
* @copyright 2019-2022 Leonid Sheikman
9+
* @copyright 2019-2024 Leonid Sheikman
10+
*
1011
* @see https://github.com/Leonid74/helpers-php
1112
*
1213
* This file is part of the project.
@@ -42,18 +43,18 @@ public static function arrayKeyExists(
4243
): bool {
4344
$result = false;
4445

45-
if ( !empty( $mNeedle ) && !empty( $aHaystack ) ) {
46-
foreach ( $aHaystack as $key => $mItem ) {
47-
$result = $bCaseInsensitive ? \mb_strtolower( $mNeedle ) == \mb_strtolower( $key ) : (string) $mNeedle == (string) $key;
46+
if (!empty($mNeedle) && !empty($aHaystack)) {
47+
foreach ($aHaystack as $key => $mItem) {
48+
$result = $bCaseInsensitive ? \mb_strtolower($mNeedle) == \mb_strtolower($key) : (string) $mNeedle == (string) $key;
4849

49-
if ( $result ) {
50+
if ($result) {
5051
break;
5152
}
5253

53-
if ( $bProcessSubarrays && \is_array( $mItem ) ) {
54-
$result = self::arrayKeyExists( $mNeedle, $mItem, $bCaseInsensitive, $bProcessSubarrays );
54+
if ($bProcessSubarrays && \is_array($mItem)) {
55+
$result = self::arrayKeyExists($mNeedle, $mItem, $bCaseInsensitive, $bProcessSubarrays);
5556

56-
if ( $result ) {
57+
if ($result) {
5758
break;
5859
}
5960
}
@@ -85,22 +86,22 @@ public static function arrayValueExists(
8586
): bool {
8687
$result = false;
8788

88-
if ( !empty( $mNeedle ) && !empty( $aHaystack ) ) {
89-
foreach ( $aHaystack as $mItem ) {
90-
if ( \is_array( $mItem ) ) {
91-
if ( !$bProcessSubarrays ) {
89+
if (!empty($mNeedle) && !empty($aHaystack)) {
90+
foreach ($aHaystack as $mItem) {
91+
if (\is_array($mItem)) {
92+
if (!$bProcessSubarrays) {
9293
continue;
9394
}
94-
$result = self::arrayValueExists( $mNeedle, $mItem, $bCaseInsensitive, $bProcessSubarrays, $bStrictTypes );
95+
$result = self::arrayValueExists($mNeedle, $mItem, $bCaseInsensitive, $bProcessSubarrays, $bStrictTypes);
9596
} else {
96-
if ( $bCaseInsensitive ) {
97-
$result = $bStrictTypes ? ( \gettype( $mNeedle ) == \gettype( $mItem ) && \mb_strtolower( $mNeedle ) == \mb_strtolower( $mItem ) ) : \mb_strtolower( $mNeedle ) == \mb_strtolower( $mItem );
97+
if ($bCaseInsensitive) {
98+
$result = $bStrictTypes ? (\gettype($mNeedle) == \gettype($mItem) && \mb_strtolower($mNeedle) == \mb_strtolower($mItem)) : \mb_strtolower($mNeedle) == \mb_strtolower($mItem);
9899
} else {
99100
$result = $bStrictTypes ? $mNeedle === $mItem : (string) $mNeedle == (string) $mItem;
100101
}
101102
}
102103

103-
if ( $result ) {
104+
if ($result) {
104105
break;
105106
}
106107
}
@@ -116,7 +117,7 @@ public static function arrayValueExists(
116117
*
117118
* @param string $sHaystack
118119
* @param array $aNeedles
119-
* @param int $iOffset (optional, defaults to 0)
120+
* @param int $iOffset (optional, defaults to 0)
120121
* @param bool $bCaseInSensitive (optional, defaults to false)
121122
*
122123
* @return bool
@@ -127,9 +128,9 @@ public static function arrayNeedlesExists(
127128
?int $iOffset = 0,
128129
?bool $bCaseInSensitive = false
129130
): bool {
130-
if ( !empty( $aNeedles ) && !empty( $sHaystack ) ) {
131-
foreach ( $aNeedles as $needle ) {
132-
if ( ( $bCaseInSensitive ? \stripos( $sHaystack, $needle, $iOffset ) : \strpos( $sHaystack, $needle, $iOffset ) ) !== false ) {
131+
if (!empty($aNeedles) && !empty($sHaystack)) {
132+
foreach ($aNeedles as $needle) {
133+
if (($bCaseInSensitive ? \stripos($sHaystack, $needle, $iOffset) : \strpos($sHaystack, $needle, $iOffset)) !== false) {
133134
return true;
134135
}
135136
}
@@ -162,26 +163,27 @@ public static function arraySearch(
162163
) {
163164
$result = false;
164165

165-
if ( !empty( $mNeedle ) && !empty( $aHaystack ) ) {
166-
foreach ( $aHaystack as $key => $mItem ) {
167-
if ( \is_array( $mItem ) ) {
168-
if ( !$bProcessSubarrays ) {
166+
if (!empty($mNeedle) && !empty($aHaystack)) {
167+
foreach ($aHaystack as $key => $mItem) {
168+
if (\is_array($mItem)) {
169+
if (!$bProcessSubarrays) {
169170
continue;
170171
}
171-
$result = self::arraySearch( $mNeedle, $mItem, $bCaseInsensitive, $bProcessSubarrays, $bStrictTypes );
172+
$result = self::arraySearch($mNeedle, $mItem, $bCaseInsensitive, $bProcessSubarrays, $bStrictTypes);
172173

173-
if ( false !== $result ) {
174+
if (false !== $result) {
174175
break;
175176
}
176177
} else {
177-
if ( $bCaseInsensitive ) {
178-
$result = $bStrictTypes ? ( \gettype( $mNeedle ) == \gettype( $mItem ) && \mb_strtolower( $mNeedle ) == \mb_strtolower( $mItem ) ) : \mb_strtolower( $mNeedle ) == \mb_strtolower( $mItem );
178+
if ($bCaseInsensitive) {
179+
$result = $bStrictTypes ? (\gettype($mNeedle) == \gettype($mItem) && \mb_strtolower($mNeedle) == \mb_strtolower($mItem)) : \mb_strtolower($mNeedle) == \mb_strtolower($mItem);
179180
} else {
180181
$result = $bStrictTypes ? $mNeedle === $mItem : (string) $mNeedle == (string) $mItem;
181182
}
182183

183-
if ( false !== $result ) {
184+
if (false !== $result) {
184185
$result = $key;
186+
185187
break;
186188
}
187189
}
@@ -200,7 +202,7 @@ public static function arraySearch(
200202
*
201203
* @param mixed $mNeedle
202204
* @param array $aHaystack
203-
* @param bool $bCaseInsensitive (optional, defaults to false)
205+
* @param bool $bCaseInsensitive (optional, defaults to false)
204206
* @param bool $bProcessSubarrays (optional, defaults to true)
205207
*
206208
* @return mixed
@@ -213,20 +215,21 @@ public static function arrayStrPos(
213215
) {
214216
$result = false;
215217

216-
if ( !empty( $mNeedle ) && !empty( $aHaystack ) ) {
217-
foreach ( $aHaystack as $key => $mItem ) {
218-
if ( \is_array( $mItem ) ) {
219-
if ( !$bProcessSubarrays ) {
218+
if (!empty($mNeedle) && !empty($aHaystack)) {
219+
foreach ($aHaystack as $key => $mItem) {
220+
if (\is_array($mItem)) {
221+
if (!$bProcessSubarrays) {
220222
continue;
221223
}
222-
$result = self::arrayStrPos( $mNeedle, $mItem, $bCaseInsensitive, $bProcessSubarrays );
224+
$result = self::arrayStrPos($mNeedle, $mItem, $bCaseInsensitive, $bProcessSubarrays);
223225

224-
if ( false !== $result ) {
226+
if (false !== $result) {
225227
break;
226228
}
227229
} else {
228-
if ( false !== ( $bCaseInsensitive ? \mb_stripos( $mItem, $mNeedle ) : \mb_strpos( $mItem, $mNeedle ) ) ) {
230+
if (false !== ($bCaseInsensitive ? \mb_stripos($mItem, $mNeedle) : \mb_strpos($mItem, $mNeedle))) {
229231
$result = $key;
232+
230233
break;
231234
}
232235
}
@@ -243,73 +246,75 @@ public static function arrayStrPos(
243246
* Массив объединённый в строку (к примеру, для сохранения в CSV).
244247
* Вложенные подмассивы будут также объединены в строку с разделителем subseparator.
245248
*
246-
* @param array $aHaystack
249+
* @param array $aHaystack
247250
* @param string $default
248251
* @param string $separator
249252
* @param string $subseparator
250-
* @param bool $isRecursion (service parameter)
253+
* @param bool $isRecursion (service parameter)
251254
*
252255
* @return string converted array to string
253-
*/
256+
*/
254257
public static function arrayToCsvString(
255258
array $aHaystack,
256259
?string $default = '',
257260
?string $separator = ';',
258261
?string $subseparator = '|',
259262
?bool $isRecursion = false
260263
): string {
261-
if ( empty( $aHaystack ) ) {
264+
if (empty($aHaystack)) {
262265
return $default;
263266
}
264267

265268
$separator = $isRecursion ? $separator : '"' . $separator . '"';
266269

267270
$res = [];
268-
foreach ( $aHaystack as $row ) {
269-
$res[] = \is_array( $row ) ? \trim( self::arrayToCsvString( $row, $default, $subseparator, '||', true ), "\"\n\r" ) : \trim( $row );
271+
foreach ($aHaystack as $row) {
272+
$res[] = \is_array($row) ? \trim(self::arrayToCsvString($row, $default, $subseparator, '||', true), "\"\n\r") : \trim($row);
270273
}
271274

272-
return '"' . \implode( $separator, $res ) . '"' . PHP_EOL;
275+
return '"' . \implode($separator, $res) . '"' . PHP_EOL;
273276
}
274277

275278
/**
276279
* Flatten Multidimensional Array.
277280
*
278281
* Превратить Многомерный Массив в одномерный.
279282
*
280-
* @param array $aHaystack
281-
* @param string|bool $prefix
283+
* @param array $aHaystack
284+
* @param bool|string $prefix
282285
*
283286
* @return array
284287
*
285288
* @author https://github.com/php-curl-class/php-curl-class
289+
*
286290
* @edit Leonid Sheikman (leonid74)
287291
*/
288292
public static function arrayFlattenMulti(
289293
?array $aHaystack = [],
290-
bool $prefix = false
294+
$prefix = false
291295
): array {
292296
$aResult = [];
293297

294-
if ( $prefix && $aHaystack === null ) {
298+
if ($prefix && $aHaystack === null) {
295299
$aResult[$prefix] = null;
296300

297301
return $aResult;
298302
}
299303

300-
if ( $prefix && empty( $aHaystack ) ) {
304+
if ($prefix && empty($aHaystack)) {
301305
$aResult[$prefix] = '';
302306

303307
return $aResult;
304308
}
305309

306-
if ( !empty( $aHaystack ) ) {
307-
foreach ( $aHaystack as $key => $val ) {
308-
if ( \is_scalar( $val ) ) {
309-
$aResult[ $prefix ? $prefix . '[' . $key . ']' : $key ] = $val;
310+
if (!empty($aHaystack)) {
311+
foreach ($aHaystack as $key => $val) {
312+
if (\is_scalar($val)) {
313+
$aResult[$prefix ? $prefix . '[' . $key . ']' : $key] = $val;
314+
310315
continue;
311316
}
312-
$aResult = \array_merge( $aResult, self::arrayFlattenMulti( $val, $prefix ? $prefix . '[' . $key . ']' : $key ) );
317+
$aResult = \array_merge($aResult, self::arrayFlattenMulti($val, $prefix ? $prefix . '[' . $key . ']' : $key));
313318
}
314319
}
315320

@@ -333,13 +338,13 @@ public static function arrayMapRecursive(
333338
string $func,
334339
array $aHaystack = []
335340
): array {
336-
if ( empty( $aHaystack ) ) {
341+
if (empty($aHaystack)) {
337342
return [];
338343
}
339344

340345
$aResult = [];
341-
foreach ( $aHaystack as $key => $value ) {
342-
$aResult[$key] = ( \is_array( $value ) ? self::arrayMapRecursive( $func, $value ) : $func( $value ) );
346+
foreach ($aHaystack as $key => $value) {
347+
$aResult[$key] = (\is_array($value) ? self::arrayMapRecursive($func, $value) : $func($value));
343348
}
344349

345350
return $aResult;
@@ -352,7 +357,7 @@ public static function arrayMapRecursive(
352357
*
353358
* @param array|string $search
354359
* @param array|string $replace
355-
* @param array $aHaystack
360+
* @param array $aHaystack
356361
*
357362
* @return array
358363
*
@@ -363,9 +368,10 @@ public static function arrayStrReplaceMulti(
363368
$replace,
364369
array $aHaystack = []
365370
): array {
366-
if ( empty( $aHaystack ) ) {
371+
if (empty($aHaystack)) {
367372
return [];
368373
}
369-
return \json_decode( \str_replace( $search, $replace, \json_encode( $aHaystack, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ) ), true );
374+
375+
return \json_decode(\str_replace($search, $replace, \json_encode($aHaystack, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)), true);
370376
}
371377
}

0 commit comments

Comments
 (0)