forked from maciejczyzewski/bottomline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcat.php
52 lines (50 loc) · 1.31 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
<?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
* ]
* ```
*
* @param array|object $collection Collection to assign to.
* @param array|object ...$_ N other collections to assign.
*
* @return array|object Assigned collection.
*
*/
function concat($collection, $_)
{
// TODO Alternative over casting to array: implement directly assign using
// foreach (func_get_args() as $collectionN). (with object handling).
// First collection determine output type (array vs. object).
$isObject = \__::isObject($collection);
// Cast args to array.
$args = \__::map(func_get_args(), function ($arg) {
return (array) $arg;
});
// PHP 5.6+ array_merge_recursive(...$args);
$merged = call_user_func_array('array_merge', $args);
;
return $isObject ? (object) $merged : $merged;
}