|
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
|
7 |
|
public static function fromCollection($collection) { |
|
24
|
7 |
|
if (!(is_array($collection) || $collection instanceof Iterator)) { |
|
25
|
1 |
|
throw new InvalidArgumentException('$collection is neither an array nor an iterator'); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
6 |
|
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
|
8 |
|
private static function toCollection($data) { |
|
41
|
|
|
// prepare normal array |
|
42
|
8 |
|
if (!($data instanceof Iterator)) { |
|
43
|
8 |
|
$data = json_decode(json_encode($data)); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
// check if we can transform it into a collection or just return as is |
|
47
|
8 |
|
if (!(is_array($data) || $data instanceof Iterator || $data instanceof \stdClass)) { |
|
48
|
8 |
|
return $data; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
// check we have a list |
|
52
|
8 |
|
if (is_array($data) || $data instanceof AbstractList) { |
|
53
|
6 |
|
return self::toList($data); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// everything else must be a map |
|
57
|
6 |
|
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
|
6 |
|
public static function toMap($collection) { |
|
68
|
6 |
|
$map = new Map(); |
|
69
|
6 |
|
foreach ($collection as $k => $v) { |
|
70
|
6 |
|
$map->set($k, self::toCollection($v)); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
6 |
|
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
|
6 |
|
public static function toList($collection) { |
|
84
|
6 |
|
$list = new ArrayList(); |
|
85
|
6 |
|
foreach ($collection as $v) { |
|
86
|
6 |
|
$list->add(self::toCollection($v)); |
|
87
|
|
|
} |
|
88
|
6 |
|
return $list; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
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.