Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(collections): add fromItems and containsKey #484

Merged
merged 8 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Psl/Collection/IndexAccessInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ public function at(int|string $k): mixed;
*/
public function contains(int|string $k): bool;

/**
* Alias of `contains`.
*
* @param Tk $k
*
* @see contains() method
*
* @psalm-mutation-free
*/
public function containsKey(int|string $k): bool;
veewee marked this conversation as resolved.
Show resolved Hide resolved

/**
* Returns the value at the specified key in the current collection.
*
Expand Down
25 changes: 25 additions & 0 deletions src/Psl/Collection/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ public static function fromArray(array $elements): Map
return new self($elements);
}

/**
* @template Tsk of array-key
* @template Tsv
*
* @param array<Tsk, Tsv> $items
*
* @return Map<Tsk, Tsv>
*/
public static function fromItems(iterable $items): Map
{
return self::fromArray(iterator_to_array($items));
}

/**
* Returns the first value in the current collection.
*
Expand Down Expand Up @@ -240,6 +253,18 @@ public function contains(int|string $k): bool
return array_key_exists($k, $this->elements);
}

/**
* Alias of `contains`.
*
* @param Tk $k
*
* @psalm-mutation-free
*/
public function containsKey(int|string $k): bool
{
return $this->contains($k);
}

/**
* Returns the value at the specified key in the current map.
*
Expand Down
25 changes: 25 additions & 0 deletions src/Psl/Collection/MutableMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ public static function fromArray(array $elements): MutableMap
return new self($elements);
}

/**
* @template Tsk of array-key
* @template Tsv
*
* @param array<Tsk, Tsv> $items
*
* @return MutableMap<Tsk, Tsv>
*/
public static function fromItems(iterable $items): MutableMap
{
return self::fromArray(iterator_to_array($items));
}

/**
* Returns the first value in the current collection.
*
Expand Down Expand Up @@ -240,6 +253,18 @@ public function contains(int|string $k): bool
return array_key_exists($k, $this->elements);
}

/**
* Alias of `contains`.
*
* @param Tk $k
*
* @psalm-mutation-free
*/
public function containsKey(int|string $k): bool
{
return $this->contains($k);
}

/**
* Returns the value at the specified key in the current map.
*
Expand Down
28 changes: 28 additions & 0 deletions src/Psl/Collection/MutableSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ public static function fromArray(array $elements): MutableSet
return new self($elements);
}

/**
* Create a set from the given iterable, using the values of the iterable as the set values.
*
* @template Ts of array-key
*
* @param iterable<Ts> $items
*
* @return MutableSet<Ts>
*/
public static function fromItems(iterable $items): MutableSet
{
return self::fromArray(iterator_to_array($items));
}

/**
* Create a set from the given $elements array, using the keys of the array as the set values.
*
Expand Down Expand Up @@ -219,6 +233,20 @@ public function contains(int|string $k): bool
return array_key_exists($k, $this->elements);
}

/**
* Alias of `contains`.
*
* @param T $k
*
* @return bool True if the value is in the set, false otherwise.
*
* @psalm-mutation-free
*/
public function containsKey(int|string $k): bool
{
return $this->contains($k);
}

/**
* Returns the provided value if it is part of the set, or null if it is not.
*
Expand Down
27 changes: 27 additions & 0 deletions src/Psl/Collection/MutableVector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use function array_keys;
use function array_values;
use function count;
use function iterator_to_array;

/**
* @template T
Expand Down Expand Up @@ -69,6 +70,20 @@ public static function fromArray(array $elements): MutableVector
return new self($elements);
}

/**
* Create a vector from the given $items iterable.
*
* @template Ts
*
* @param iterable<Ts> $items
*
* @return MutableVector<Ts>
*/
public static function fromItems(iterable $items): MutableVector
veewee marked this conversation as resolved.
Show resolved Hide resolved
{
return self::fromArray(iterator_to_array($items));
}

/**
* Returns the first value in the current `MutableVector`.
*
Expand Down Expand Up @@ -189,6 +204,18 @@ public function contains(int|string $k): bool
return array_key_exists($k, $this->elements);
}

/**
* Alias of `contains`.
*
* @param int<0, max> $k
*
* @psalm-mutation-free
*/
public function containsKey(int|string $k): bool
{
return $this->contains($k);
}

/**
* Returns the value at the specified key in the current `MutableVector`.
*
Expand Down
30 changes: 30 additions & 0 deletions src/Psl/Collection/Set.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ public static function fromArray(array $elements): Set
return new self($elements);
}

/**
* Create a set from the given items, using the keys of the array as the set values.
*
* @template Ts of array-key
*
* @param iterable<Ts, mixed> $items
*
* @return Set<Ts>
*
* @pure
*/
public static function fromItems(iterable $items): Set
{
return self::fromArray(iterator_to_array($items));
}

/**
* Create a set from the given $elements array, using the keys of the array as the set values.
*
Expand Down Expand Up @@ -220,6 +236,20 @@ public function contains(int|string $k): bool
return array_key_exists($k, $this->elements);
}

/**
* Alias of `contains`.
*
* @param T $k
*
* @return bool True if the value is in the set, false otherwise.
*
* @psalm-mutation-free
*/
public function containsKey(int|string $k): bool
{
return $this->contains($k);
}

/**
* Returns the provided value if it is part of the set, or null if it is not.
*
Expand Down
26 changes: 26 additions & 0 deletions src/Psl/Collection/Vector.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ public static function fromArray(array $elements): Vector
return new self($elements);
}

/**
* Create a vector from the given $items iterable.
*
* @template Ts
*
* @param iterable<Ts> $items
*
* @return Vector<Ts>
*/
public static function fromItems(iterable $items): Vector
{
return self::fromArray(iterator_to_array($items));
}

/**
* Returns the first value in the current `Vector`.
*
Expand Down Expand Up @@ -190,6 +204,18 @@ public function contains(int|string $k): bool
return array_key_exists($k, $this->elements);
}

/**
* Alias of `contains`.
*
* @param int<0, max> $k
*
* @psalm-mutation-free
*/
public function containsKey(int|string $k): bool
{
return $this->contains($k);
}

/**
* Returns the value at the specified key in the current `Vector`.
*
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/Collection/MapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ final class MapTest extends AbstractMapTest
*/
protected string $vectorClass = Vector::class;

public function testFromItems(): void
{
$map = Map::fromItems([
'foo' => 'bar',
'bar' => 'baz',
'baz' => 'qux',
]);

static::assertSame('bar', $map->at('foo'));
static::assertSame('baz', $map->at('bar'));
static::assertSame('qux', $map->at('baz'));
}

/**
* @template Tk of array-key
* @template Tv
Expand Down
15 changes: 14 additions & 1 deletion tests/unit/Collection/MutableMapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function testArrayAccess(): void

static::assertTrue(isset($map['foo']));
static::assertSame('1', $map['foo']);

unset($map['foo']);
static::assertFalse(isset($map['foo']));

Expand Down Expand Up @@ -235,6 +235,19 @@ public function testOffsetGetThrowsForInvalidOffsetType(): void
$map[false];
}

public function testFromItems(): void
{
$map = MutableMap::fromItems([
'foo' => 'bar',
'bar' => 'baz',
'baz' => 'qux',
]);

static::assertSame('bar', $map->at('foo'));
static::assertSame('baz', $map->at('bar'));
static::assertSame('qux', $map->at('baz'));
}

/**
* @template Tk of array-key
* @template Tv
Expand Down
8 changes: 7 additions & 1 deletion tests/unit/Collection/MutableSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function testArrayAccess(): void

static::assertTrue(isset($set['foo']));
static::assertSame('foo', $set['foo']);

unset($set['foo']);
static::assertFalse(isset($set['foo']));

Expand Down Expand Up @@ -191,6 +191,12 @@ public function testOffsetGetThrowsForInvalidOffsetType(): void
$set[false];
}

public function testFromItems(): void
{
$set = MutableSet::fromItems(['a', 'b', 'b', 'c']);
static::assertSame(['a' => 'a', 'b' => 'b', 'c' => 'c'], $set->toArray());
}

/**
* @template T of array-key
*
Expand Down
7 changes: 6 additions & 1 deletion tests/unit/Collection/MutableVectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public function testArrayAccess(): void

static::assertTrue(isset($vector[0]));
static::assertSame('foo', $vector[0]);

unset($vector[0]);
static::assertFalse(isset($vector[2]));

Expand Down Expand Up @@ -230,6 +230,11 @@ public function testOffsetGetThrowsForInvalidOffsetType(): void
$vector[false];
}

public function testFromItems(): void
{
$vector = MutableVector::fromItems([1, 2, 3]);
static::assertSame([1, 2, 3], $vector->toArray());
}

/**
* @template T
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/Collection/SetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ final class SetTest extends AbstractSetTest
*/
protected string $setClass = Set::class;

public function testFromItems(): void
{
$set = Set::fromItems([
'foo',
'bar',
'baz',
]);

static::assertTrue($set->contains('foo'));
static::assertTrue($set->contains('bar'));
static::assertTrue($set->contains('baz'));
}

/**
* @template T of array-key
*
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/Collection/VectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ final class VectorTest extends AbstractVectorTest
*/
protected string $vectorClass = Vector::class;

public function testFromItems(): void
{
$vector = Vector::fromItems([1, 2, 3]);
static::assertSame([1, 2, 3], $vector->toArray());
}

/**
* @template T
*
Expand Down
Loading