|
1
|
|
|
<?php |
|
2
|
|
|
namespace phootwork\collection; |
|
3
|
|
|
|
|
4
|
|
|
use \Iterator; |
|
5
|
|
|
use \InvalidArgumentException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* CollectionUtils help to transform data recursively into collections. |
|
9
|
|
|
* |
|
10
|
|
|
* It must be mentioned the API is experimental and may change. Please |
|
11
|
|
|
* report to the issue tracker. |
|
12
|
|
|
*/ |
|
13
|
|
|
class CollectionUtils { |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Returns a proper collection for the given array (also transforms nested collections) |
|
17
|
|
|
* (experimental API) |
|
18
|
|
|
* |
|
19
|
|
|
* @param array|Iterator $collection |
|
20
|
|
|
* @return Map|ArrayList the collection |
|
21
|
|
|
* @throws InvalidArgumentException |
|
22
|
|
|
*/ |
|
23
|
|
|
public static function fromCollection($collection) { |
|
24
|
|
|
if (!(is_array($collection) || $collection instanceof Iterator)) { |
|
25
|
|
|
throw new InvalidArgumentException('$collection is neither an array nor an iterator'); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
return self::toCollection($collection); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
private static function toCollection($data) { |
|
32
|
|
|
// prepare normal array |
|
33
|
|
|
if (!($data instanceof Iterator)) { |
|
34
|
|
|
$data = json_decode(json_encode($data)); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
// check if we can transform it into a collection or just return as is |
|
38
|
|
|
if (!(is_array($data) || $data instanceof Iterator || $data instanceof \stdClass)) { |
|
39
|
|
|
return $data; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
// check we have a list |
|
43
|
|
|
if (is_array($data) || $data instanceof AbstractList) { |
|
44
|
|
|
return self::toList($data); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
// everything else must be a map |
|
48
|
|
|
return self::toMap($data); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Recursively transforms data into a map (on the first level, deeper levels |
|
53
|
|
|
* transformed to an appropriate collection) (experimental API) |
|
54
|
|
|
* |
|
55
|
|
|
* @param array|Iterator $collection |
|
56
|
|
|
* @return Map |
|
57
|
|
|
*/ |
|
58
|
|
|
public static function toMap($collection) { |
|
59
|
|
|
$map = new Map(); |
|
60
|
|
|
foreach ($collection as $k => $v) { |
|
61
|
|
|
$map->set($k, self::toCollection($v)); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $map; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Recursively transforms data into a list (on the first level, deeper levels |
|
69
|
|
|
* transformed to an appropriate collection) (experimental API) |
|
70
|
|
|
* |
|
71
|
|
|
* @param array|Iterator $collection |
|
72
|
|
|
* @return ArrayList |
|
73
|
|
|
*/ |
|
74
|
|
|
public static function toList($collection) { |
|
75
|
|
|
$list = new ArrayList(); |
|
76
|
|
|
foreach ($collection as $v) { |
|
77
|
|
|
$list->add(self::toCollection($v)); |
|
78
|
|
|
} |
|
79
|
|
|
return $list; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Recursively exports a collection to an array |
|
84
|
|
|
* |
|
85
|
|
|
* @param mixed $collection |
|
86
|
|
|
* @return array |
|
87
|
|
|
*/ |
|
88
|
|
|
public static function toArrayRecursive($collection) { |
|
89
|
|
|
$arr = $collection; |
|
90
|
|
|
if (is_object($collection) && method_exists($collection, 'toArray')) { |
|
91
|
|
|
$arr = $collection->toArray(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return array_map(function ($v) { |
|
95
|
|
|
if (is_object($v) && method_exists($v, 'toArray')) { |
|
96
|
|
|
return static::toArrayRecursive($v); |
|
97
|
|
|
} |
|
98
|
|
|
return $v; |
|
99
|
|
|
}, $arr); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|