Skip to content

Commit 93f20a7

Browse files
Merge pull request #34 from worksolutions/dev
Some enchasaments for next minor version
2 parents 1afdc5e + e00263f commit 93f20a7

File tree

8 files changed

+131
-9
lines changed

8 files changed

+131
-9
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "Collections library for php language",
44
"minimum-stability": "dev",
55
"license": "MIT",
6-
"version": "1.0.8",
6+
"version": "1.0.9",
77
"authors": [
88
{
99
"name": "Maxim Sokolovsky",

src/WS/Utils/Collections/DummyStreamDecorator.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,23 @@ public function always(): Stream
136136
{
137137
return $this->decoratedStream;
138138
}
139+
140+
/**
141+
* @inheritDoc
142+
*/
143+
public function toArray(): array
144+
{
145+
return $this
146+
->getCollection()
147+
->toArray()
148+
;
149+
}
150+
151+
/**
152+
* @inheritDoc
153+
*/
154+
public function getSet(): Set
155+
{
156+
return new HashSet($this->toArray());
157+
}
139158
}

src/WS/Utils/Collections/Functions/Predicates.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,11 @@ public static function equal($value): Closure
9191
/**
9292
* Returns <Fn($el: mixed): bool> passed unique element at once
9393
*/
94-
public static function lockDuplicated(): Closure
94+
public static function lockDuplicated(?callable $caster = null): Closure
9595
{
9696
$set = new HashSet();
97-
return static function ($el) use ($set): bool {
97+
return static function ($el) use ($set, $caster): bool {
98+
$el = isset($caster) ? $caster($el) : $el;
9899
$res = !$set->contains($el);
99100
$set->add($el);
100101
return $res;

src/WS/Utils/Collections/Functions/Reorganizers.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,25 @@ public static function chunk(int $size): Closure
112112

113113
/**
114114
* Returns Closure <Fn($c: Collection): Collection> that collapses a collection of arrays into a single, flat collection
115+
* @param int $depth Depth of collapses. The 0 value is without
115116
* @return Closure
116117
*/
117-
public static function collapse(): Closure
118+
public static function collapse(int $depth = 0): Closure
118119
{
119-
return static function (Collection $collection): Collection {
120-
$flatIterable = static function (iterable $collection) use (& $flatIterable): array {
120+
if ($depth === 0) {
121+
$depth = null;
122+
}
123+
return static function (Collection $collection) use ($depth): Collection {
124+
$flatIterable = static function (iterable $collection, $depth) use (& $flatIterable): array {
125+
$goToDepth = $depth > 0 || $depth === null;
126+
if ($depth !== null) {
127+
$depth--;
128+
}
129+
121130
$res = [];
122131
foreach ($collection as $item) {
123-
if (is_iterable($item)) {
124-
$toPush = $flatIterable($item);
132+
if (is_iterable($item) && $goToDepth) {
133+
$toPush = $flatIterable($item, $depth);
125134
array_unshift($toPush, $res);
126135
array_push(...$toPush);
127136
$res = array_shift($toPush);
@@ -132,7 +141,7 @@ public static function collapse(): Closure
132141
return $res;
133142
};
134143

135-
return self::collectionConstructor($flatIterable($collection));
144+
return self::collectionConstructor($flatIterable($collection, $depth));
136145
};
137146
}
138147
}

src/WS/Utils/Collections/SerialStream.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,4 +336,23 @@ public function always(): Stream
336336
{
337337
return $this;
338338
}
339+
340+
/**
341+
* @inheritDoc
342+
*/
343+
public function toArray(): array
344+
{
345+
return $this
346+
->getCollection()
347+
->toArray()
348+
;
349+
}
350+
351+
/**
352+
* @inheritDoc
353+
*/
354+
public function getSet(): Set
355+
{
356+
return new HashSet($this->toArray());
357+
}
339358
}

src/WS/Utils/Collections/Stream.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,16 @@ public function getCollection(): Collection;
161161
* @return Stream
162162
*/
163163
public function always(): Stream;
164+
165+
/**
166+
* Returns array as collection represent
167+
* @return array
168+
*/
169+
public function toArray(): array;
170+
171+
/**
172+
* Creates a set structure
173+
* @return Set
174+
*/
175+
public function getSet(): Set;
164176
}

tests/WS/Utils/Collections/ReorganizersFunctionsTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,36 @@ public function collapsing(): void
4848
$this->assertThat($collapsedCollection, CollectionIsEqual::to([1, 2, 3, 4, 5, 6]));
4949
}
5050

51+
/**
52+
* @test
53+
*/
54+
public function singleDepthCollapsing(): void
55+
{
56+
$collection = self::toCollection([1, 2], [3, 4], [5, 6, [7, 8]]);
57+
58+
$collapsedCollection = $collection
59+
->stream()
60+
->reorganize(Reorganizers::collapse(1))
61+
->getCollection()
62+
;
63+
$this->assertThat($collapsedCollection, CollectionIsEqual::to([1, 2, 3, 4, 5, 6, [7, 8]]));
64+
}
65+
66+
/**
67+
* @test
68+
*/
69+
public function numericDepthCollapsing(): void
70+
{
71+
$collection = self::toCollection([1, 2], [3, 4], [5, 6, [7, 8, [9, 10]]]);
72+
73+
$collapsedCollection = $collection
74+
->stream()
75+
->reorganize(Reorganizers::collapse(3))
76+
->getCollection()
77+
;
78+
$this->assertThat($collapsedCollection, CollectionIsEqual::to([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]));
79+
}
80+
5181
/**
5282
* @test
5383
*/
@@ -67,4 +97,22 @@ public function filterDistinctElements(): void
6797

6898
self::assertThat($uniqCollection, CollectionIsEqual::to([$o1, $o2, $o3]));
6999
}
100+
101+
/**
102+
* @test
103+
*/
104+
public function filterDistinctCastedValues(): void
105+
{
106+
$caster = static function (int $number) {
107+
return $number % 2;
108+
};
109+
110+
$result = CollectionFactory::numbers(0, 10)
111+
->stream()
112+
->filter(Predicates::lockDuplicated($caster))
113+
->toArray()
114+
;
115+
116+
$this->assertCount(2, $result);
117+
}
70118
}

tests/WS/Utils/Collections/SerialStreamTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,4 +627,18 @@ public function copyOfCollectionWhenStreaming(): void
627627

628628
$this->assertNotSame($dest, $source);
629629
}
630+
631+
/**
632+
* @test
633+
*/
634+
public function gettingSetStructure(): void
635+
{
636+
$source = CollectionFactory::generate(3);
637+
$this->assertThat($source, $this->isInstanceOf(ListSequence::class));
638+
$set = $source
639+
->stream()
640+
->getSet()
641+
;
642+
$this->assertThat($set, $this->isInstanceOf(Set::class));
643+
}
630644
}

0 commit comments

Comments
 (0)