forked from maciejczyzewski/bottomline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcat.php
74 lines (67 loc) · 1.87 KB
/
concat.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
namespace collections;
/**
* 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 recursive merge, see `__::merge()`.
*
* **Usage**
*
* ```php
* __::concat(
* ['color' => ['favorite' => 'red', 5], 3],
* [10, 'color' => ['favorite' => 'green', 'blue']]
* );
* ```
*
* **Result**
*
* ```
* [
* 'color' => ['favorite' => ['green'], 5, 'blue'],
* 3,
* 10
* ]
* ```
*
* @since 0.2.0 added support for iterables
*
* @param iterable|\stdClass $collection Collection to assign to.
* @param iterable|\stdClass ...$_ N other collections to assign.
*
* @return array|\stdClass If the first argument given to this function is an
* `\stdClass`, an `\stdClass` will be returned. Otherwise, an array will be
* returned.
*/
function concat($collection, $_)
{
$args = func_get_args();
$areArrayish = \__::every($args, function ($arg) {
return \__::isArray($arg) || $arg instanceof \stdClass;
});
if ($areArrayish) {
$argsAsArrays = \__::map($args, function ($arg) {
return (array)$arg;
});
$merged = call_user_func_array('array_merge', $argsAsArrays);
return ($collection instanceof \stdClass) ? (object)$merged : $merged;
}
if ($collection instanceof \Iterator || $collection instanceof \IteratorAggregate) {
$result = iterator_to_array(\__::getIterator($collection));
} else {
$result = (array)$collection;
}
foreach (\__::drop($args, 1) as $iterable) {
foreach ($iterable as $key => $item) {
if (\__::isNumber($key)) {
$result[] = $item;
} else {
$result[$key] = $item;
}
}
}
return $result;
}