Completed
Push — master ( 954f77...6792a2 )
by Thomas
03:03
created

CollectionUtils::toMap()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
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 9
	private static function toCollection($data) {
32
		// prepare normal array
33 9
		if (!($data instanceof Iterator)) {
34 9
			$data = json_decode(json_encode($data));
35 9
		}
36
37
		// check if we can transform it into a collection or just return as is
38 9
		if (!(is_array($data) || $data instanceof Iterator || $data instanceof \stdClass)) {
39 9
			return $data;
40
		}
41
42
		// check we have a list
43 9
		if (is_array($data) || $data instanceof AbstractList) {
44 7
			return self::toList($data);
45
		}
46
47
		// everything else must be a map
48 7
		return self::toMap($data);
1 ignored issue
show
Bug introduced by
It seems like $data defined by json_decode(json_encode($data)) on line 34 can also be of type object<stdClass>; however, phootwork\collection\CollectionUtils::toMap() does only seem to accept array|object<Iterator>, maybe add an additional type check?

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:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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 7
	public static function toMap($collection) {
59 7
		$map = new Map();
60 7
		foreach ($collection as $k => $v) {
61 7
			$map->set($k, self::toCollection($v));
62 7
		}
63
64 7
		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 7
	public static function toList($collection) {
75 7
		$list = new ArrayList();
76 7
		foreach ($collection as $v) {
77 7
			$list->add(self::toCollection($v));
78 7
		}
79 7
		return $list;
80
	}
81
82
	/**
83
	 * Recursively exports a collection to an array
84
	 *
85
	 * @param mixed $collection
86
	 * @return array
87
	 */
88 1
	public static function toArrayRecursive($collection) {
89 1
		$arr = $collection;
90 1
		if (is_object($collection) && method_exists($collection, 'toArray')) {
91 1
			$arr = $collection->toArray();
92 1
		}
93
94 1
		return array_map(function ($v) {
95 1
			if (is_object($v) && method_exists($v, 'toArray')) {
96 1
				return static::toArrayRecursive($v);
97
			}
98 1
			return $v;
99 1
		}, $arr);
100
	}
101
102
}
103