@@ -20,29 +20,29 @@ |
||
20 | 20 | * @author Thomas Gossmann |
21 | 21 | */ |
22 | 22 | class ArrayList extends AbstractList { |
23 | - use AccessorsPart, AddPart, AddAllPart; |
|
23 | + use AccessorsPart, AddPart, AddAllPart; |
|
24 | 24 | |
25 | - /** |
|
26 | - * Creates a new ArrayList |
|
27 | - * |
|
28 | - * @param array|Iterator $collection |
|
29 | - */ |
|
30 | - public function __construct($collection = []) { |
|
31 | - $this->addAll($collection); |
|
32 | - } |
|
25 | + /** |
|
26 | + * Creates a new ArrayList |
|
27 | + * |
|
28 | + * @param array|Iterator $collection |
|
29 | + */ |
|
30 | + public function __construct($collection = []) { |
|
31 | + $this->addAll($collection); |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * Removes an element from the list by its index. |
|
36 | - * |
|
37 | - * @param int $index |
|
38 | - * |
|
39 | - * @return ArrayList |
|
40 | - */ |
|
41 | - public function removeByIndex(int $index): self { |
|
42 | - if (isset($this->array[$index])) { |
|
43 | - unset($this->array[$index]); |
|
44 | - } |
|
34 | + /** |
|
35 | + * Removes an element from the list by its index. |
|
36 | + * |
|
37 | + * @param int $index |
|
38 | + * |
|
39 | + * @return ArrayList |
|
40 | + */ |
|
41 | + public function removeByIndex(int $index): self { |
|
42 | + if (isset($this->array[$index])) { |
|
43 | + unset($this->array[$index]); |
|
44 | + } |
|
45 | 45 | |
46 | - return $this; |
|
47 | - } |
|
46 | + return $this; |
|
47 | + } |
|
48 | 48 | } |
@@ -21,35 +21,35 @@ |
||
21 | 21 | * |
22 | 22 | * @return void |
23 | 23 | */ |
24 | - public function clear(): void; |
|
24 | + public function clear(): void; |
|
25 | 25 | |
26 | - /** |
|
27 | - * Checks whether this collection is empty |
|
28 | - * |
|
29 | - * @return bool |
|
30 | - */ |
|
31 | - public function isEmpty(): bool; |
|
26 | + /** |
|
27 | + * Checks whether this collection is empty |
|
28 | + * |
|
29 | + * @return bool |
|
30 | + */ |
|
31 | + public function isEmpty(): bool; |
|
32 | 32 | |
33 | - /** |
|
34 | - * Checks whether the given element is in this collection |
|
35 | - * |
|
36 | - * @param mixed $element |
|
37 | - * |
|
38 | - * @return bool |
|
39 | - */ |
|
40 | - public function contains($element): bool; |
|
33 | + /** |
|
34 | + * Checks whether the given element is in this collection |
|
35 | + * |
|
36 | + * @param mixed $element |
|
37 | + * |
|
38 | + * @return bool |
|
39 | + */ |
|
40 | + public function contains($element): bool; |
|
41 | 41 | |
42 | - /** |
|
43 | - * Returns the amount of elements in this collection |
|
44 | - * |
|
45 | - * @return int |
|
46 | - */ |
|
47 | - public function size(): int; |
|
42 | + /** |
|
43 | + * Returns the amount of elements in this collection |
|
44 | + * |
|
45 | + * @return int |
|
46 | + */ |
|
47 | + public function size(): int; |
|
48 | 48 | |
49 | - /** |
|
50 | - * Returns the collection as an array |
|
51 | - * |
|
52 | - * @return array |
|
53 | - */ |
|
54 | - public function toArray(): array; |
|
49 | + /** |
|
50 | + * Returns the collection as an array |
|
51 | + * |
|
52 | + * @return array |
|
53 | + */ |
|
54 | + public function toArray(): array; |
|
55 | 55 | } |
@@ -31,102 +31,102 @@ |
||
31 | 31 | * |
32 | 32 | * @return Map|ArrayList the collection |
33 | 33 | */ |
34 | - public static function fromCollection($collection) { |
|
35 | - if (!(is_array($collection) || $collection instanceof Iterator)) { |
|
36 | - throw new InvalidArgumentException('$collection is neither an array nor an iterator'); |
|
37 | - } |
|
38 | - |
|
39 | - return self::toCollection($collection); |
|
40 | - } |
|
41 | - |
|
42 | - /** |
|
43 | - * @param mixed $data |
|
44 | - * |
|
45 | - * @return mixed|ArrayList|Map |
|
46 | - */ |
|
47 | - private static function toCollection($data) { |
|
48 | - // prepare normal array |
|
49 | - if (!($data instanceof Iterator)) { |
|
50 | - $data = json_decode(json_encode($data)); |
|
51 | - } |
|
52 | - |
|
53 | - // check if we can transform it into a collection or just return as is |
|
54 | - if (!(is_array($data) || $data instanceof Iterator || $data instanceof stdClass)) { |
|
55 | - return $data; |
|
56 | - } |
|
57 | - |
|
58 | - // check we have a list |
|
59 | - if (is_array($data) || $data instanceof AbstractList) { |
|
60 | - return self::toList($data); |
|
61 | - } |
|
62 | - |
|
63 | - // everything else must be a map |
|
64 | - return self::toMap($data); |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * Recursively transforms data into a map (on the first level, deeper levels |
|
69 | - * transformed to an appropriate collection) (experimental API) |
|
70 | - * |
|
71 | - * @param array|Iterator|stdClass $collection |
|
72 | - * |
|
73 | - * @return Map |
|
74 | - */ |
|
75 | - public static function toMap($collection): Map { |
|
76 | - if ($collection instanceof stdClass) { |
|
77 | - $collection = json_decode(json_encode($collection), true); |
|
78 | - } |
|
79 | - |
|
80 | - $map = new Map(); |
|
81 | - foreach ($collection as $k => $v) { |
|
82 | - $map->set($k, self::toCollection($v)); |
|
83 | - } |
|
84 | - |
|
85 | - return $map; |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * Recursively transforms data into a list (on the first level, deeper levels |
|
90 | - * transformed to an appropriate collection) (experimental API) |
|
91 | - * |
|
92 | - * @param array|Iterator $collection |
|
93 | - * |
|
94 | - * @return ArrayList |
|
95 | - */ |
|
96 | - public static function toList($collection): ArrayList { |
|
97 | - $list = new ArrayList(); |
|
98 | - foreach ($collection as $v) { |
|
99 | - $list->add(self::toCollection($v)); |
|
100 | - } |
|
101 | - |
|
102 | - return $list; |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * Recursively exports a collection to an array |
|
107 | - * |
|
108 | - * @param mixed $collection |
|
109 | - * |
|
110 | - * @return array |
|
111 | - */ |
|
112 | - public static function toArrayRecursive($collection): array { |
|
113 | - $arr = $collection; |
|
114 | - if (is_object($collection) && method_exists($collection, 'toArray')) { |
|
115 | - $arr = $collection->toArray(); |
|
116 | - } |
|
117 | - |
|
118 | - return array_map( |
|
34 | + public static function fromCollection($collection) { |
|
35 | + if (!(is_array($collection) || $collection instanceof Iterator)) { |
|
36 | + throw new InvalidArgumentException('$collection is neither an array nor an iterator'); |
|
37 | + } |
|
38 | + |
|
39 | + return self::toCollection($collection); |
|
40 | + } |
|
41 | + |
|
42 | + /** |
|
43 | + * @param mixed $data |
|
44 | + * |
|
45 | + * @return mixed|ArrayList|Map |
|
46 | + */ |
|
47 | + private static function toCollection($data) { |
|
48 | + // prepare normal array |
|
49 | + if (!($data instanceof Iterator)) { |
|
50 | + $data = json_decode(json_encode($data)); |
|
51 | + } |
|
52 | + |
|
53 | + // check if we can transform it into a collection or just return as is |
|
54 | + if (!(is_array($data) || $data instanceof Iterator || $data instanceof stdClass)) { |
|
55 | + return $data; |
|
56 | + } |
|
57 | + |
|
58 | + // check we have a list |
|
59 | + if (is_array($data) || $data instanceof AbstractList) { |
|
60 | + return self::toList($data); |
|
61 | + } |
|
62 | + |
|
63 | + // everything else must be a map |
|
64 | + return self::toMap($data); |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * Recursively transforms data into a map (on the first level, deeper levels |
|
69 | + * transformed to an appropriate collection) (experimental API) |
|
70 | + * |
|
71 | + * @param array|Iterator|stdClass $collection |
|
72 | + * |
|
73 | + * @return Map |
|
74 | + */ |
|
75 | + public static function toMap($collection): Map { |
|
76 | + if ($collection instanceof stdClass) { |
|
77 | + $collection = json_decode(json_encode($collection), true); |
|
78 | + } |
|
79 | + |
|
80 | + $map = new Map(); |
|
81 | + foreach ($collection as $k => $v) { |
|
82 | + $map->set($k, self::toCollection($v)); |
|
83 | + } |
|
84 | + |
|
85 | + return $map; |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * Recursively transforms data into a list (on the first level, deeper levels |
|
90 | + * transformed to an appropriate collection) (experimental API) |
|
91 | + * |
|
92 | + * @param array|Iterator $collection |
|
93 | + * |
|
94 | + * @return ArrayList |
|
95 | + */ |
|
96 | + public static function toList($collection): ArrayList { |
|
97 | + $list = new ArrayList(); |
|
98 | + foreach ($collection as $v) { |
|
99 | + $list->add(self::toCollection($v)); |
|
100 | + } |
|
101 | + |
|
102 | + return $list; |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * Recursively exports a collection to an array |
|
107 | + * |
|
108 | + * @param mixed $collection |
|
109 | + * |
|
110 | + * @return array |
|
111 | + */ |
|
112 | + public static function toArrayRecursive($collection): array { |
|
113 | + $arr = $collection; |
|
114 | + if (is_object($collection) && method_exists($collection, 'toArray')) { |
|
115 | + $arr = $collection->toArray(); |
|
116 | + } |
|
117 | + |
|
118 | + return array_map( |
|
119 | 119 | /** |
120 | 120 | * @param mixed $v |
121 | 121 | * |
122 | 122 | * @return mixed |
123 | 123 | */ |
124 | 124 | function ($v) { |
125 | - if (is_object($v) && method_exists($v, 'toArray')) { |
|
126 | - return static::toArrayRecursive($v); |
|
127 | - } |
|
125 | + if (is_object($v) && method_exists($v, 'toArray')) { |
|
126 | + return static::toArrayRecursive($v); |
|
127 | + } |
|
128 | 128 | |
129 | - return $v; |
|
129 | + return $v; |
|
130 | 130 | }, $arr); |
131 | - } |
|
131 | + } |
|
132 | 132 | } |
@@ -121,7 +121,7 @@ |
||
121 | 121 | * |
122 | 122 | * @return mixed |
123 | 123 | */ |
124 | - function ($v) { |
|
124 | + function($v) { |
|
125 | 125 | if (is_object($v) && method_exists($v, 'toArray')) { |
126 | 126 | return static::toArrayRecursive($v); |
127 | 127 | } |
@@ -21,42 +21,42 @@ |
||
21 | 21 | /** |
22 | 22 | * Remove all elements from the collection. |
23 | 23 | */ |
24 | - public function clear(): void { |
|
25 | - $this->array = []; |
|
26 | - } |
|
27 | - |
|
28 | - /** |
|
29 | - * @internal |
|
30 | - */ |
|
31 | - public function rewind() { |
|
32 | - return reset($this->array); |
|
33 | - } |
|
34 | - |
|
35 | - /** |
|
36 | - * @internal |
|
37 | - */ |
|
38 | - public function current() { |
|
39 | - return current($this->array); |
|
40 | - } |
|
41 | - |
|
42 | - /** |
|
43 | - * @internal |
|
44 | - */ |
|
45 | - public function key(): int { |
|
46 | - return key($this->array); |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * @internal |
|
51 | - */ |
|
52 | - public function next() { |
|
53 | - return next($this->array); |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * @internal |
|
58 | - */ |
|
59 | - public function valid(): bool { |
|
60 | - return key($this->array) !== null; |
|
61 | - } |
|
24 | + public function clear(): void { |
|
25 | + $this->array = []; |
|
26 | + } |
|
27 | + |
|
28 | + /** |
|
29 | + * @internal |
|
30 | + */ |
|
31 | + public function rewind() { |
|
32 | + return reset($this->array); |
|
33 | + } |
|
34 | + |
|
35 | + /** |
|
36 | + * @internal |
|
37 | + */ |
|
38 | + public function current() { |
|
39 | + return current($this->array); |
|
40 | + } |
|
41 | + |
|
42 | + /** |
|
43 | + * @internal |
|
44 | + */ |
|
45 | + public function key(): int { |
|
46 | + return key($this->array); |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * @internal |
|
51 | + */ |
|
52 | + public function next() { |
|
53 | + return next($this->array); |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * @internal |
|
58 | + */ |
|
59 | + public function valid(): bool { |
|
60 | + return key($this->array) !== null; |
|
61 | + } |
|
62 | 62 | } |
@@ -20,55 +20,55 @@ |
||
20 | 20 | * @author Thomas Gossmann |
21 | 21 | */ |
22 | 22 | class Stack extends AbstractList { |
23 | - use PopPart; |
|
23 | + use PopPart; |
|
24 | 24 | |
25 | - /** |
|
26 | - * Creates a new ArrayList |
|
27 | - * |
|
28 | - * @param array|Iterator $collection |
|
29 | - */ |
|
30 | - public function __construct($collection = []) { |
|
31 | - $this->pushAll($collection); |
|
32 | - } |
|
25 | + /** |
|
26 | + * Creates a new ArrayList |
|
27 | + * |
|
28 | + * @param array|Iterator $collection |
|
29 | + */ |
|
30 | + public function __construct($collection = []) { |
|
31 | + $this->pushAll($collection); |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * Pushes an element onto the stack |
|
36 | - * |
|
37 | - * @param mixed $element |
|
38 | - * |
|
39 | - * @return $this |
|
40 | - */ |
|
41 | - public function push($element): self { |
|
42 | - array_push($this->array, $element); |
|
34 | + /** |
|
35 | + * Pushes an element onto the stack |
|
36 | + * |
|
37 | + * @param mixed $element |
|
38 | + * |
|
39 | + * @return $this |
|
40 | + */ |
|
41 | + public function push($element): self { |
|
42 | + array_push($this->array, $element); |
|
43 | 43 | |
44 | - return $this; |
|
45 | - } |
|
44 | + return $this; |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * Pushes many elements onto the stack |
|
49 | - * |
|
50 | - * @param array|Iterator $collection |
|
51 | - * |
|
52 | - * @return $this |
|
53 | - */ |
|
54 | - public function pushAll($collection): self { |
|
55 | - foreach ($collection as $element) { |
|
56 | - $this->push($element); |
|
57 | - } |
|
47 | + /** |
|
48 | + * Pushes many elements onto the stack |
|
49 | + * |
|
50 | + * @param array|Iterator $collection |
|
51 | + * |
|
52 | + * @return $this |
|
53 | + */ |
|
54 | + public function pushAll($collection): self { |
|
55 | + foreach ($collection as $element) { |
|
56 | + $this->push($element); |
|
57 | + } |
|
58 | 58 | |
59 | - return $this; |
|
60 | - } |
|
59 | + return $this; |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * Returns the element at the head or null if the stack is empty but doesn't remove that element |
|
64 | - * |
|
65 | - * @return mixed |
|
66 | - */ |
|
67 | - public function peek() { |
|
68 | - if ($this->size() > 0) { |
|
69 | - return $this->array[$this->size() - 1]; |
|
70 | - } |
|
62 | + /** |
|
63 | + * Returns the element at the head or null if the stack is empty but doesn't remove that element |
|
64 | + * |
|
65 | + * @return mixed |
|
66 | + */ |
|
67 | + public function peek() { |
|
68 | + if ($this->size() > 0) { |
|
69 | + return $this->array[$this->size() - 1]; |
|
70 | + } |
|
71 | 71 | |
72 | - return null; |
|
73 | - } |
|
72 | + return null; |
|
73 | + } |
|
74 | 74 | } |
@@ -21,237 +21,237 @@ |
||
21 | 21 | * @author Thomas Gossmann |
22 | 22 | */ |
23 | 23 | class Map extends AbstractCollection implements \ArrayAccess { |
24 | - use SortAssocPart; |
|
24 | + use SortAssocPart; |
|
25 | 25 | |
26 | - /** |
|
27 | - * Creates a new Map |
|
28 | - * |
|
29 | - * @param array|Iterator $collection |
|
30 | - */ |
|
31 | - public function __construct($collection = []) { |
|
32 | - $this->setAll($collection); |
|
33 | - } |
|
26 | + /** |
|
27 | + * Creates a new Map |
|
28 | + * |
|
29 | + * @param array|Iterator $collection |
|
30 | + */ |
|
31 | + public function __construct($collection = []) { |
|
32 | + $this->setAll($collection); |
|
33 | + } |
|
34 | 34 | |
35 | - /** |
|
36 | - * @param string|Text $key |
|
37 | - * |
|
38 | - * @return int|string |
|
39 | - */ |
|
40 | - private function extractKey($key) { |
|
41 | - if ($key instanceof Text) { |
|
42 | - return $key->toString(); |
|
43 | - } |
|
35 | + /** |
|
36 | + * @param string|Text $key |
|
37 | + * |
|
38 | + * @return int|string |
|
39 | + */ |
|
40 | + private function extractKey($key) { |
|
41 | + if ($key instanceof Text) { |
|
42 | + return $key->toString(); |
|
43 | + } |
|
44 | 44 | |
45 | - return $key; |
|
46 | - } |
|
45 | + return $key; |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * Sets an element with the given key on that map |
|
50 | - * |
|
51 | - * @param string|Text $key |
|
52 | - * @param mixed $element |
|
53 | - * |
|
54 | - * @return Map $this |
|
55 | - */ |
|
56 | - public function set($key, $element): self { |
|
57 | - $key = $this->extractKey($key); |
|
58 | - $this->array[$key] = $element; |
|
48 | + /** |
|
49 | + * Sets an element with the given key on that map |
|
50 | + * |
|
51 | + * @param string|Text $key |
|
52 | + * @param mixed $element |
|
53 | + * |
|
54 | + * @return Map $this |
|
55 | + */ |
|
56 | + public function set($key, $element): self { |
|
57 | + $key = $this->extractKey($key); |
|
58 | + $this->array[$key] = $element; |
|
59 | 59 | |
60 | - return $this; |
|
61 | - } |
|
60 | + return $this; |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * Returns the element for the given key or your value, if the key doesn't exist. |
|
65 | - * |
|
66 | - * @param string|Text $key |
|
67 | - * @param mixed $default the return value, if the key doesn't exist |
|
68 | - * |
|
69 | - * @return mixed |
|
70 | - */ |
|
71 | - public function get($key, $default = null) { |
|
72 | - $key = $this->extractKey($key); |
|
73 | - if (isset($this->array[$key])) { |
|
74 | - return $this->array[$key]; |
|
75 | - } else { |
|
76 | - return $default; |
|
77 | - } |
|
78 | - } |
|
63 | + /** |
|
64 | + * Returns the element for the given key or your value, if the key doesn't exist. |
|
65 | + * |
|
66 | + * @param string|Text $key |
|
67 | + * @param mixed $default the return value, if the key doesn't exist |
|
68 | + * |
|
69 | + * @return mixed |
|
70 | + */ |
|
71 | + public function get($key, $default = null) { |
|
72 | + $key = $this->extractKey($key); |
|
73 | + if (isset($this->array[$key])) { |
|
74 | + return $this->array[$key]; |
|
75 | + } else { |
|
76 | + return $default; |
|
77 | + } |
|
78 | + } |
|
79 | 79 | |
80 | - /** |
|
81 | - * Returns the key for the given value |
|
82 | - * |
|
83 | - * @param mixed $value the value |
|
84 | - * |
|
85 | - * @return mixed |
|
86 | - */ |
|
87 | - public function getKey($value) { |
|
88 | - foreach ($this->array as $k => $v) { |
|
89 | - if ($v === $value) { |
|
90 | - return $k; |
|
91 | - } |
|
92 | - } |
|
80 | + /** |
|
81 | + * Returns the key for the given value |
|
82 | + * |
|
83 | + * @param mixed $value the value |
|
84 | + * |
|
85 | + * @return mixed |
|
86 | + */ |
|
87 | + public function getKey($value) { |
|
88 | + foreach ($this->array as $k => $v) { |
|
89 | + if ($v === $value) { |
|
90 | + return $k; |
|
91 | + } |
|
92 | + } |
|
93 | 93 | |
94 | - return null; |
|
95 | - } |
|
94 | + return null; |
|
95 | + } |
|
96 | 96 | |
97 | - /** |
|
98 | - * Sets many elements on that map |
|
99 | - * |
|
100 | - * @param array|Iterator $collection |
|
101 | - * |
|
102 | - * @return Map $this |
|
103 | - */ |
|
104 | - public function setAll($collection): self { |
|
105 | - foreach ($collection as $key => $element) { |
|
106 | - $this->set($key, $element); |
|
107 | - } |
|
97 | + /** |
|
98 | + * Sets many elements on that map |
|
99 | + * |
|
100 | + * @param array|Iterator $collection |
|
101 | + * |
|
102 | + * @return Map $this |
|
103 | + */ |
|
104 | + public function setAll($collection): self { |
|
105 | + foreach ($collection as $key => $element) { |
|
106 | + $this->set($key, $element); |
|
107 | + } |
|
108 | 108 | |
109 | - return $this; |
|
110 | - } |
|
109 | + return $this; |
|
110 | + } |
|
111 | 111 | |
112 | - /** |
|
113 | - * Removes and returns an element from the map by the given key. Returns null if the key |
|
114 | - * does not exist. |
|
115 | - * |
|
116 | - * @param string|Text $key |
|
117 | - * |
|
118 | - * @return $this |
|
119 | - */ |
|
120 | - public function remove($key): self { |
|
121 | - $key = $this->extractKey($key); |
|
122 | - if (isset($this->array[$key])) { |
|
123 | - unset($this->array[$key]); |
|
124 | - } |
|
112 | + /** |
|
113 | + * Removes and returns an element from the map by the given key. Returns null if the key |
|
114 | + * does not exist. |
|
115 | + * |
|
116 | + * @param string|Text $key |
|
117 | + * |
|
118 | + * @return $this |
|
119 | + */ |
|
120 | + public function remove($key): self { |
|
121 | + $key = $this->extractKey($key); |
|
122 | + if (isset($this->array[$key])) { |
|
123 | + unset($this->array[$key]); |
|
124 | + } |
|
125 | 125 | |
126 | - return $this; |
|
127 | - } |
|
126 | + return $this; |
|
127 | + } |
|
128 | 128 | |
129 | - /** |
|
130 | - * Returns all keys as Set |
|
131 | - * |
|
132 | - * @return Set the map's keys |
|
133 | - */ |
|
134 | - public function keys(): Set { |
|
135 | - return new Set(array_keys($this->array)); |
|
136 | - } |
|
129 | + /** |
|
130 | + * Returns all keys as Set |
|
131 | + * |
|
132 | + * @return Set the map's keys |
|
133 | + */ |
|
134 | + public function keys(): Set { |
|
135 | + return new Set(array_keys($this->array)); |
|
136 | + } |
|
137 | 137 | |
138 | - /** |
|
139 | - * Returns all values as ArrayList |
|
140 | - * |
|
141 | - * @return ArrayList the map's values |
|
142 | - */ |
|
143 | - public function values(): ArrayList { |
|
144 | - return new ArrayList(array_values($this->array)); |
|
145 | - } |
|
138 | + /** |
|
139 | + * Returns all values as ArrayList |
|
140 | + * |
|
141 | + * @return ArrayList the map's values |
|
142 | + */ |
|
143 | + public function values(): ArrayList { |
|
144 | + return new ArrayList(array_values($this->array)); |
|
145 | + } |
|
146 | 146 | |
147 | - /** |
|
148 | - * Returns whether the key exist. |
|
149 | - * |
|
150 | - * @param string|Text $key |
|
151 | - * |
|
152 | - * @return bool |
|
153 | - */ |
|
154 | - public function has($key): bool { |
|
155 | - $key = $this->extractKey($key); |
|
147 | + /** |
|
148 | + * Returns whether the key exist. |
|
149 | + * |
|
150 | + * @param string|Text $key |
|
151 | + * |
|
152 | + * @return bool |
|
153 | + */ |
|
154 | + public function has($key): bool { |
|
155 | + $key = $this->extractKey($key); |
|
156 | 156 | |
157 | - return isset($this->array[$key]); |
|
158 | - } |
|
157 | + return isset($this->array[$key]); |
|
158 | + } |
|
159 | 159 | |
160 | - /** |
|
161 | - * Sorts the map |
|
162 | - * |
|
163 | - * @param Comparator|callable $cmp |
|
164 | - * |
|
165 | - * @return $this |
|
166 | - */ |
|
167 | - public function sort($cmp = null): AbstractArray { |
|
168 | - return $this->sortAssoc($cmp); |
|
169 | - } |
|
160 | + /** |
|
161 | + * Sorts the map |
|
162 | + * |
|
163 | + * @param Comparator|callable $cmp |
|
164 | + * |
|
165 | + * @return $this |
|
166 | + */ |
|
167 | + public function sort($cmp = null): AbstractArray { |
|
168 | + return $this->sortAssoc($cmp); |
|
169 | + } |
|
170 | 170 | |
171 | - /** |
|
172 | - * Iterates the map and calls the callback function with the current key and value as parameters |
|
173 | - * |
|
174 | - * @param callable $callback |
|
175 | - */ |
|
176 | - public function each(callable $callback): void { |
|
177 | - foreach ($this->array as $key => $value) { |
|
178 | - $callback($key, $value); |
|
179 | - } |
|
180 | - } |
|
171 | + /** |
|
172 | + * Iterates the map and calls the callback function with the current key and value as parameters |
|
173 | + * |
|
174 | + * @param callable $callback |
|
175 | + */ |
|
176 | + public function each(callable $callback): void { |
|
177 | + foreach ($this->array as $key => $value) { |
|
178 | + $callback($key, $value); |
|
179 | + } |
|
180 | + } |
|
181 | 181 | |
182 | - /** |
|
183 | - * Searches the collection with a given callback and returns the key for the first element if found. |
|
184 | - * |
|
185 | - * The callback function takes one or two parameters: |
|
186 | - * |
|
187 | - * function ($element [, $query]) {} |
|
188 | - * |
|
189 | - * The callback must return a boolean |
|
190 | - * When it's passed, $query must be the first argument: |
|
191 | - * |
|
192 | - * - find($query, callback) |
|
193 | - * - find(callback) |
|
194 | - * |
|
195 | - * @param array $arguments |
|
196 | - * |
|
197 | - * @return mixed|null the key or null if it hasn't been found |
|
198 | - */ |
|
199 | - public function findKey(...$arguments) { |
|
200 | - $index = count($arguments) === 1 ? $this->find($arguments[0]) : $this->find($arguments[0], $arguments[1]); |
|
182 | + /** |
|
183 | + * Searches the collection with a given callback and returns the key for the first element if found. |
|
184 | + * |
|
185 | + * The callback function takes one or two parameters: |
|
186 | + * |
|
187 | + * function ($element [, $query]) {} |
|
188 | + * |
|
189 | + * The callback must return a boolean |
|
190 | + * When it's passed, $query must be the first argument: |
|
191 | + * |
|
192 | + * - find($query, callback) |
|
193 | + * - find(callback) |
|
194 | + * |
|
195 | + * @param array $arguments |
|
196 | + * |
|
197 | + * @return mixed|null the key or null if it hasn't been found |
|
198 | + */ |
|
199 | + public function findKey(...$arguments) { |
|
200 | + $index = count($arguments) === 1 ? $this->find($arguments[0]) : $this->find($arguments[0], $arguments[1]); |
|
201 | 201 | |
202 | - return $this->getKey($index); |
|
203 | - } |
|
202 | + return $this->getKey($index); |
|
203 | + } |
|
204 | 204 | |
205 | - /** |
|
206 | - * Searches the collection with a given callback and returns the key for the last element if found. |
|
207 | - * |
|
208 | - * The callback function takes one or two parameters: |
|
209 | - * |
|
210 | - * function ($element [, $query]) {} |
|
211 | - * |
|
212 | - * The callback must return a boolean |
|
213 | - * When it's passed, $query must be the first argument: |
|
214 | - * |
|
215 | - * - find($query, callback) |
|
216 | - * - find(callback) |
|
217 | - * |
|
218 | - * @param array $arguments |
|
219 | - * |
|
220 | - * @return mixed|null the key or null if it hasn't been found |
|
221 | - */ |
|
222 | - public function findLastKey(...$arguments) { |
|
223 | - $index = count($arguments) === 1 ? $this->findLast($arguments[0]) : $this->findLast($arguments[0], $arguments[1]); |
|
205 | + /** |
|
206 | + * Searches the collection with a given callback and returns the key for the last element if found. |
|
207 | + * |
|
208 | + * The callback function takes one or two parameters: |
|
209 | + * |
|
210 | + * function ($element [, $query]) {} |
|
211 | + * |
|
212 | + * The callback must return a boolean |
|
213 | + * When it's passed, $query must be the first argument: |
|
214 | + * |
|
215 | + * - find($query, callback) |
|
216 | + * - find(callback) |
|
217 | + * |
|
218 | + * @param array $arguments |
|
219 | + * |
|
220 | + * @return mixed|null the key or null if it hasn't been found |
|
221 | + */ |
|
222 | + public function findLastKey(...$arguments) { |
|
223 | + $index = count($arguments) === 1 ? $this->findLast($arguments[0]) : $this->findLast($arguments[0], $arguments[1]); |
|
224 | 224 | |
225 | - return $this->getKey($index); |
|
226 | - } |
|
225 | + return $this->getKey($index); |
|
226 | + } |
|
227 | 227 | |
228 | - /** |
|
229 | - * @internal |
|
230 | - */ |
|
231 | - public function offsetSet($offset, $value) { |
|
232 | - if (!is_null($offset)) { |
|
233 | - $this->array[$offset] = $value; |
|
234 | - } |
|
235 | - } |
|
228 | + /** |
|
229 | + * @internal |
|
230 | + */ |
|
231 | + public function offsetSet($offset, $value) { |
|
232 | + if (!is_null($offset)) { |
|
233 | + $this->array[$offset] = $value; |
|
234 | + } |
|
235 | + } |
|
236 | 236 | |
237 | - /** |
|
238 | - * @internal |
|
239 | - */ |
|
240 | - public function offsetExists($offset) { |
|
241 | - return isset($this->array[$offset]); |
|
242 | - } |
|
237 | + /** |
|
238 | + * @internal |
|
239 | + */ |
|
240 | + public function offsetExists($offset) { |
|
241 | + return isset($this->array[$offset]); |
|
242 | + } |
|
243 | 243 | |
244 | - /** |
|
245 | - * @internal |
|
246 | - */ |
|
247 | - public function offsetUnset($offset) { |
|
248 | - unset($this->array[$offset]); |
|
249 | - } |
|
244 | + /** |
|
245 | + * @internal |
|
246 | + */ |
|
247 | + public function offsetUnset($offset) { |
|
248 | + unset($this->array[$offset]); |
|
249 | + } |
|
250 | 250 | |
251 | - /** |
|
252 | - * @internal |
|
253 | - */ |
|
254 | - public function offsetGet($offset) { |
|
255 | - return isset($this->array[$offset]) ? $this->array[$offset] : null; |
|
256 | - } |
|
251 | + /** |
|
252 | + * @internal |
|
253 | + */ |
|
254 | + public function offsetGet($offset) { |
|
255 | + return isset($this->array[$offset]) ? $this->array[$offset] : null; |
|
256 | + } |
|
257 | 257 | } |
@@ -23,19 +23,19 @@ |
||
23 | 23 | * @author Cristiano Cinotti |
24 | 24 | */ |
25 | 25 | abstract class AbstractList extends AbstractCollection { |
26 | - use EachPart, IndexFindersPart, ReducePart, RemovePart, ReversePart; |
|
26 | + use EachPart, IndexFindersPart, ReducePart, RemovePart, ReversePart; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Sorts the collection in reverse order |
|
30 | - * |
|
31 | - * @see #sort |
|
32 | - * @see #reverse |
|
33 | - * |
|
34 | - * @param Comparator|callable $cmp |
|
35 | - * |
|
36 | - * @return $this |
|
37 | - */ |
|
38 | - public function reverseSort($cmp = null): self { |
|
39 | - return $this->sort($cmp)->reverse(); |
|
40 | - } |
|
28 | + /** |
|
29 | + * Sorts the collection in reverse order |
|
30 | + * |
|
31 | + * @see #sort |
|
32 | + * @see #reverse |
|
33 | + * |
|
34 | + * @param Comparator|callable $cmp |
|
35 | + * |
|
36 | + * @return $this |
|
37 | + */ |
|
38 | + public function reverseSort($cmp = null): self { |
|
39 | + return $this->sort($cmp)->reverse(); |
|
40 | + } |
|
41 | 41 | } |
@@ -18,29 +18,29 @@ |
||
18 | 18 | * @author Thomas Gossmann |
19 | 19 | */ |
20 | 20 | class Set extends AbstractList { |
21 | - use AddAllPart; |
|
21 | + use AddAllPart; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Creates a new Set |
|
25 | - * |
|
26 | - * @param array|Iterator $collection |
|
27 | - */ |
|
28 | - public function __construct($collection = []) { |
|
29 | - $this->addAll($collection); |
|
30 | - } |
|
23 | + /** |
|
24 | + * Creates a new Set |
|
25 | + * |
|
26 | + * @param array|Iterator $collection |
|
27 | + */ |
|
28 | + public function __construct($collection = []) { |
|
29 | + $this->addAll($collection); |
|
30 | + } |
|
31 | 31 | |
32 | - /** |
|
33 | - * Adds an element to that set |
|
34 | - * |
|
35 | - * @param mixed $element |
|
36 | - * |
|
37 | - * @return $this |
|
38 | - */ |
|
39 | - public function add($element): self { |
|
40 | - if (!in_array($element, $this->array, true)) { |
|
41 | - $this->array[$this->size()] = $element; |
|
42 | - } |
|
32 | + /** |
|
33 | + * Adds an element to that set |
|
34 | + * |
|
35 | + * @param mixed $element |
|
36 | + * |
|
37 | + * @return $this |
|
38 | + */ |
|
39 | + public function add($element): self { |
|
40 | + if (!in_array($element, $this->array, true)) { |
|
41 | + $this->array[$this->size()] = $element; |
|
42 | + } |
|
43 | 43 | |
44 | - return $this; |
|
45 | - } |
|
44 | + return $this; |
|
45 | + } |
|
46 | 46 | } |
@@ -25,59 +25,59 @@ |
||
25 | 25 | * |
26 | 26 | * @param array|Iterator $collection |
27 | 27 | */ |
28 | - public function __construct($collection = []) { |
|
29 | - foreach ($collection as $element) { |
|
30 | - $this->array[] = $element; |
|
31 | - } |
|
32 | - } |
|
28 | + public function __construct($collection = []) { |
|
29 | + foreach ($collection as $element) { |
|
30 | + $this->array[] = $element; |
|
31 | + } |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * Enqueues an element |
|
36 | - * |
|
37 | - * @param mixed $element |
|
38 | - * |
|
39 | - * @return $this |
|
40 | - */ |
|
41 | - public function enqueue($element): self { |
|
42 | - array_unshift($this->array, $element); |
|
34 | + /** |
|
35 | + * Enqueues an element |
|
36 | + * |
|
37 | + * @param mixed $element |
|
38 | + * |
|
39 | + * @return $this |
|
40 | + */ |
|
41 | + public function enqueue($element): self { |
|
42 | + array_unshift($this->array, $element); |
|
43 | 43 | |
44 | - return $this; |
|
45 | - } |
|
44 | + return $this; |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * Enqueues many elements |
|
49 | - * |
|
50 | - * @param array|Iterator $collection |
|
51 | - * |
|
52 | - * @return $this |
|
53 | - */ |
|
54 | - public function enqueueAll($collection): self { |
|
55 | - foreach ($collection as $element) { |
|
56 | - $this->enqueue($element); |
|
57 | - } |
|
47 | + /** |
|
48 | + * Enqueues many elements |
|
49 | + * |
|
50 | + * @param array|Iterator $collection |
|
51 | + * |
|
52 | + * @return $this |
|
53 | + */ |
|
54 | + public function enqueueAll($collection): self { |
|
55 | + foreach ($collection as $element) { |
|
56 | + $this->enqueue($element); |
|
57 | + } |
|
58 | 58 | |
59 | - return $this; |
|
60 | - } |
|
59 | + return $this; |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * Returns the element at the head or null if the queue is empty but doesn't remove that element |
|
64 | - * |
|
65 | - * @return mixed |
|
66 | - */ |
|
67 | - public function peek() { |
|
68 | - if ($this->size() > 0) { |
|
69 | - return $this->array[0]; |
|
70 | - } |
|
62 | + /** |
|
63 | + * Returns the element at the head or null if the queue is empty but doesn't remove that element |
|
64 | + * |
|
65 | + * @return mixed |
|
66 | + */ |
|
67 | + public function peek() { |
|
68 | + if ($this->size() > 0) { |
|
69 | + return $this->array[0]; |
|
70 | + } |
|
71 | 71 | |
72 | - return null; |
|
73 | - } |
|
72 | + return null; |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * Removes and returns the element at the head or null if the is empty |
|
77 | - * |
|
78 | - * @return mixed |
|
79 | - */ |
|
80 | - public function poll() { |
|
81 | - return array_shift($this->array); |
|
82 | - } |
|
75 | + /** |
|
76 | + * Removes and returns the element at the head or null if the is empty |
|
77 | + * |
|
78 | + * @return mixed |
|
79 | + */ |
|
80 | + public function poll() { |
|
81 | + return array_shift($this->array); |
|
82 | + } |
|
83 | 83 | } |