forked from maciejczyzewski/bottomline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcatDeep.php
59 lines (57 loc) · 1.6 KB
/
concatDeep.php
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
<?php
namespace collections;
/**
* Recursively combines and concat collections provided with each others.
*
* If the collections have common keys, then the values are appended in an array.
* If numerical indexes are passed, then values are appended.
*
* For a non-recursive concat, see `__::concat()`.
*
* **Usage**
*
* ```php
* __::concatDeep(
* ['color' => ['favorite' => 'red', 5], 3],
* [10, 'color' => ['favorite' => 'green', 'blue']]
* );
* ```
*
* **Result**
*
* ```
* [
* 'color' => [
* 'favorite' => ['red', 'green'],
* 5,
* 'blue'
* ],
* 3,
* 10
* ]
* ```
*
* @param array|object $collection First collection to concatDeep.
* @param array|object ...$_ N other collections to concatDeep.
*
* @return array|object Concatenated collection.
*/
function concatDeep()
{
return \__::reduceRight(func_get_args(), function ($source, $result) {
\__::doForEach($source, function ($sourceValue, $key) use (&$result) {
if (!\__::has($result, $key)) {
$result = \__::set($result, $key, $sourceValue);
} elseif (is_numeric($key)) {
$result = \__::concat($result, [$sourceValue]);
} else {
$resultValue = \__::get($result, $key);
$result = \__::set($result, $key, concatDeep(
\__::isCollection($resultValue) ? $resultValue : (array) $resultValue,
\__::isCollection($sourceValue) ? $sourceValue : (array) $sourceValue
));
}
});
return $result;
}, []);
}