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
|
8 |
|
public static function fromCollection($collection) { |
24
|
8 |
|
if (!(is_array($collection) || $collection instanceof Iterator)) { |
25
|
1 |
|
throw new InvalidArgumentException('$collection is neither an array nor an iterator'); |
26
|
|
|
} |
27
|
|
|
|
28
|
7 |
|
return self::toCollection($collection); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @deprecated use fromCollection instead (will be removed in version 1.3) |
33
|
|
|
* @param Iterator $array |
34
|
|
|
* @return Map|ArrayList the collection |
35
|
|
|
*/ |
36
|
|
|
public static function fromArray($array) { |
37
|
|
|
return self::fromCollection($array); |
38
|
|
|
} |
39
|
|
|
|
40
|
9 |
|
private static function toCollection($data) { |
41
|
|
|
// prepare normal array |
42
|
9 |
|
if (!($data instanceof Iterator)) { |
43
|
9 |
|
$data = json_decode(json_encode($data)); |
44
|
9 |
|
} |
45
|
|
|
|
46
|
|
|
// check if we can transform it into a collection or just return as is |
47
|
9 |
|
if (!(is_array($data) || $data instanceof Iterator || $data instanceof \stdClass)) { |
48
|
9 |
|
return $data; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// check we have a list |
52
|
9 |
|
if (is_array($data) || $data instanceof AbstractList) { |
53
|
7 |
|
return self::toList($data); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// everything else must be a map |
57
|
7 |
|
return self::toMap($data); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Recursively transforms data into a map (on the first level, deeper levels |
62
|
|
|
* transformed to an appropriate collection) (experimental API) |
63
|
|
|
* |
64
|
|
|
* @param array|Iterator $collection |
65
|
|
|
* @return Map |
66
|
|
|
*/ |
67
|
7 |
|
public static function toMap($collection) { |
68
|
7 |
|
$map = new Map(); |
69
|
7 |
|
foreach ($collection as $k => $v) { |
70
|
7 |
|
$map->set($k, self::toCollection($v)); |
71
|
7 |
|
} |
72
|
|
|
|
73
|
7 |
|
return $map; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Recursively transforms data into a list (on the first level, deeper levels |
78
|
|
|
* transformed to an appropriate collection) (experimental API) |
79
|
|
|
* |
80
|
|
|
* @param array|Iterator $collection |
81
|
|
|
* @return ArrayList |
82
|
|
|
*/ |
83
|
7 |
|
public static function toList($collection) { |
84
|
7 |
|
$list = new ArrayList(); |
85
|
7 |
|
foreach ($collection as $v) { |
86
|
7 |
|
$list->add(self::toCollection($v)); |
87
|
7 |
|
} |
88
|
7 |
|
return $list; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Recursively exports a collection to an array |
93
|
|
|
* |
94
|
|
|
* @param mixed $collection |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
1 |
|
public static function toArrayRecursive($collection) { |
98
|
1 |
|
$arr = $collection; |
99
|
1 |
|
if (is_object($collection) && method_exists($collection, 'toArray')) { |
100
|
1 |
|
$arr = $collection->toArray(); |
101
|
1 |
|
} |
102
|
|
|
|
103
|
1 |
|
return array_map(function ($v) { |
104
|
1 |
|
if (is_object($v) && method_exists($v, 'toArray')) { |
105
|
1 |
|
return static::toArrayRecursive($v); |
106
|
|
|
} |
107
|
1 |
|
return $v; |
108
|
1 |
|
}, $arr); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.