Skip to content

Commit ce10be4

Browse files
author
pfmmfp
committed
adding fromItems all around
1 parent 70c6a78 commit ce10be4

File tree

11 files changed

+121
-3
lines changed

11 files changed

+121
-3
lines changed

src/Psl/Collection/Map.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@ public static function fromArray(array $elements): Map
6666
return new self($elements);
6767
}
6868

69+
/**
70+
* @template Tsk of array-key
71+
* @template Tsv
72+
*
73+
* @param array<Tsk, Tsv> $items
74+
*
75+
* @return Map<Tsk, Tsv>
76+
*/
77+
public static function fromItems(iterable $items): Map
78+
{
79+
return self::fromArray(iterator_to_array($items));
80+
}
81+
6982
/**
7083
* Returns the first value in the current collection.
7184
*

src/Psl/Collection/MutableMap.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@ public static function fromArray(array $elements): MutableMap
6666
return new self($elements);
6767
}
6868

69+
/**
70+
* @template Tsk of array-key
71+
* @template Tsv
72+
*
73+
* @param array<Tsk, Tsv> $items
74+
*
75+
* @return MutableMap<Tsk, Tsv>
76+
*/
77+
public static function fromItems(iterable $items): MutableMap
78+
{
79+
return self::fromArray(iterator_to_array($items));
80+
}
81+
6982
/**
7083
* Returns the first value in the current collection.
7184
*

src/Psl/Collection/MutableSet.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,20 @@ public static function fromArray(array $elements): MutableSet
7171
return new self($elements);
7272
}
7373

74+
/**
75+
* Create a set from the given iterable, using the values of the iterable as the set values.
76+
*
77+
* @template Ts of array-key
78+
*
79+
* @param iterable<Ts> $items
80+
*
81+
* @return MutableSet<Ts>
82+
*/
83+
public static function fromItems(iterable $items): MutableSet
84+
{
85+
return self::fromArray(iterator_to_array($items));
86+
}
87+
7488
/**
7589
* Create a set from the given $elements array, using the keys of the array as the set values.
7690
*

src/Psl/Collection/Set.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,22 @@ public static function fromArray(array $elements): Set
7272
return new self($elements);
7373
}
7474

75+
/**
76+
* Create a set from the given items, using the keys of the array as the set values.
77+
*
78+
* @template Ts of array-key
79+
*
80+
* @param iterable<Ts, mixed> $items
81+
*
82+
* @return Set<Ts>
83+
*
84+
* @pure
85+
*/
86+
public static function fromItems(iterable $items): Set
87+
{
88+
return self::fromArray(iterator_to_array($items));
89+
}
90+
7591
/**
7692
* Create a set from the given $elements array, using the keys of the array as the set values.
7793
*

src/Psl/Collection/Vector.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,20 @@ public static function fromArray(array $elements): Vector
6969
return new self($elements);
7070
}
7171

72+
/**
73+
* Create a vector from the given $items iterable.
74+
*
75+
* @template Ts
76+
*
77+
* @param iterable<Ts> $items
78+
*
79+
* @return Vector<Ts>
80+
*/
81+
public static function fromItems(iterable $items): Vector
82+
{
83+
return self::fromArray(iterator_to_array($items));
84+
}
85+
7286
/**
7387
* Returns the first value in the current `Vector`.
7488
*

tests/unit/Collection/MapTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ final class MapTest extends AbstractMapTest
1919
*/
2020
protected string $vectorClass = Vector::class;
2121

22+
public function testFromItems(): void
23+
{
24+
$map = Map::fromItems([
25+
'foo' => 'bar',
26+
'bar' => 'baz',
27+
'baz' => 'qux',
28+
]);
29+
30+
static::assertSame('bar', $map->at('foo'));
31+
static::assertSame('baz', $map->at('bar'));
32+
static::assertSame('qux', $map->at('baz'));
33+
}
34+
2235
/**
2336
* @template Tk of array-key
2437
* @template Tv

tests/unit/Collection/MutableMapTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public function testArrayAccess(): void
153153

154154
static::assertTrue(isset($map['foo']));
155155
static::assertSame('1', $map['foo']);
156-
156+
157157
unset($map['foo']);
158158
static::assertFalse(isset($map['foo']));
159159

@@ -235,6 +235,18 @@ public function testOffsetGetThrowsForInvalidOffsetType(): void
235235
$map[false];
236236
}
237237

238+
public function testFromItems(): void {
239+
$map = MutableMap::fromItems([
240+
'foo' => 'bar',
241+
'bar' => 'baz',
242+
'baz' => 'qux',
243+
]);
244+
245+
static::assertSame('bar', $map->at('foo'));
246+
static::assertSame('baz', $map->at('bar'));
247+
static::assertSame('qux', $map->at('baz'));
248+
}
249+
238250
/**
239251
* @template Tk of array-key
240252
* @template Tv

tests/unit/Collection/MutableSetTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public function testArrayAccess(): void
109109

110110
static::assertTrue(isset($set['foo']));
111111
static::assertSame('foo', $set['foo']);
112-
112+
113113
unset($set['foo']);
114114
static::assertFalse(isset($set['foo']));
115115

@@ -191,6 +191,12 @@ public function testOffsetGetThrowsForInvalidOffsetType(): void
191191
$set[false];
192192
}
193193

194+
public function testFromItems(): void
195+
{
196+
$set = MutableSet::fromItems(['a', 'b', 'b', 'c']);
197+
static::assertSame(['a' => 'a', 'b' => 'b', 'c' => 'c'], $set->toArray());
198+
}
199+
194200
/**
195201
* @template T of array-key
196202
*

tests/unit/Collection/MutableVectorTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,6 @@ public function testFromItems(): void
236236
static::assertSame([1, 2, 3], $vector->toArray());
237237
}
238238

239-
240239
/**
241240
* @template T
242241
*

tests/unit/Collection/SetTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ final class SetTest extends AbstractSetTest
1515
*/
1616
protected string $setClass = Set::class;
1717

18+
public function testFromItems(): void {
19+
$set = Set::fromItems([
20+
'foo',
21+
'bar',
22+
'baz',
23+
]);
24+
25+
static::assertTrue($set->contains('foo'));
26+
static::assertTrue($set->contains('bar'));
27+
static::assertTrue($set->contains('baz'));
28+
}
29+
1830
/**
1931
* @template T of array-key
2032
*

tests/unit/Collection/VectorTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ final class VectorTest extends AbstractVectorTest
1515
*/
1616
protected string $vectorClass = Vector::class;
1717

18+
public function testFromItems(): void
19+
{
20+
$vector = Vector::fromItems([1, 2, 3]);
21+
static::assertSame([1, 2, 3], $vector->toArray());
22+
}
23+
1824
/**
1925
* @template T
2026
*

0 commit comments

Comments
 (0)