-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImplodeTrait.php
More file actions
131 lines (119 loc) · 4.58 KB
/
Copy pathImplodeTrait.php
File metadata and controls
131 lines (119 loc) · 4.58 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
<?php
declare(strict_types=1);
/*
* This file is part of ezkoding
*
* (c) 2025 Oliver Glowa, coding.glowa.com
*
* This source file is subject to the Apache-2.0 license that is bundled
* with this source code in the file LICENSE.
*/
namespace ollily\Tools\String;
use ArrayAccess;
use Stringable;
trait ImplodeTrait
{
/**
* Default glue character.
*
* @see ImplodeTrait::array_flatten()
* @see ImplodeTrait::implode_recursive()
*/
public const string DEFAULT_GLUE = ',';
/**
* Default separate character for csv.
*
* @see ImplodeTrait::array_flatten()
* @see ImplodeTrait::implode_recursive()
*/
public const string DEFAULT_ITEM_SEP = ';';
/**
* Recursively implodes an array with optional key inclusion.
*
* @param string $glue value that glues elements together
* @param mixed $anyData multi-dimensional array to recursively implode
* @param bool $withTextSep add a text seperator (") around each value of a scalar type (default: false)
* @param bool $withKeys include keysForValue before their values (default: false)
*
* @return string imploded array
*
* @see https://www.php.net/manual/en/language.types.type-system.php
*
* @SuppressWarnings("PHPMD.CamelCaseMethodName")
*/
// @phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
final public static function implode_recursive(string $glue, mixed $anyData, bool $withTextSep = false, bool $withKeys = false): string // NOSONAR: php:S100
{
$output = '';
$valueIdx = 0;
$textSep = $withTextSep ? '"' : '';
$objWithArray = (is_object($anyData) && is_subclass_of($anyData, ArrayAccess::class));
if (is_array($anyData) || $objWithArray) {
foreach ($anyData as $key => $value) { // @phpstan-ignore foreach.nonIterable
$currKey = ($withKeys ? (is_int($key) ? $key : "'$key'") . '=>' : ''); // NOSONAR: php:S3358
$output .= ($valueIdx > 0 ? $glue : '') . $currKey;
if (is_array($value)) {
$arrOutput = self::implode_recursive($glue, $value, $withTextSep, $withKeys);
if (!empty($arrOutput)) {
$output .= '[' . $arrOutput . ']';
} else {
$output .= '[]';
}
} else {
if (is_object($value)) {
$objOutput = self::implode_recursive($glue, $value, $withTextSep, $withKeys);
if (!empty($objOutput)) {
$output .= '{' . $objOutput . '}';
} else {
$output .= '{}';
}
} else {
$output .= $textSep . ((string) $value) . $textSep;
}
}
$valueIdx++;
}
} else {
if (is_object($anyData)) {
if ($anyData instanceof Stringable) {
$output = $anyData->__toString();
} else {
$output = $anyData::class;
}
} else {
$output = $anyData;
}
}
return $output;
}
/**
* Flatten a multidimensional anyData to one dimension, optionally preserving keys.
* Original found on {@link https://stackoverflow.com/a/526633}.
*
* @param array<mixed,mixed> $anyData the anyData to flatten
* @param int $preserveKeys 0 to not preserve keys (default),
* 1 to preserve string keys only,
* 2 to preserve all keys
* @param array<mixed,mixed> $output internal use argument for recursion
*
* @return array<mixed,mixed>
*
* @see https://stackoverflow.com/a/526633
*
* @SuppressWarnings("PHPMD.CamelCaseMethodName")
*/
// @phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
final public function array_flatten(array $anyData, int $preserveKeys = 0, array &$output = []): array // NOSONAR: php:S100
{
foreach ($anyData as $anyKey => $anyValue) {
if (is_array($anyValue)) {
$output = $this->array_flatten($anyValue, $preserveKeys, $output);
} elseif ($preserveKeys + (int)is_string($anyKey) > 1) {
$output[$anyKey] = $anyValue;
} else {
$output[] = $anyValue;
}
}
return $output;
}
}