Skip to content

Commit c4ea842

Browse files
committed
feat(collections): introduce Set, SetInterface, MutableSet, and MutableSetInterface
Signed-off-by: azjezz <azjezz@protonmail.com>
1 parent cb7bd70 commit c4ea842

35 files changed

+3786
-277
lines changed

docs/component/collection.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,22 @@
1616
- [CollectionInterface](./../../src/Psl/Collection/CollectionInterface.php#L23)
1717
- [IndexAccessInterface](./../../src/Psl/Collection/IndexAccessInterface.php#L13)
1818
- [MapInterface](./../../src/Psl/Collection/MapInterface.php#L15)
19-
- [MutableAccessibleCollectionInterface](./../../src/Psl/Collection/MutableAccessibleCollectionInterface.php#L22)
19+
- [MutableAccessibleCollectionInterface](./../../src/Psl/Collection/MutableAccessibleCollectionInterface.php#L23)
2020
- [MutableCollectionInterface](./../../src/Psl/Collection/MutableCollectionInterface.php#L22)
2121
- [MutableIndexAccessInterface](./../../src/Psl/Collection/MutableIndexAccessInterface.php#L16)
2222
- [MutableMapInterface](./../../src/Psl/Collection/MutableMapInterface.php#L16)
23+
- [MutableSetInterface](./../../src/Psl/Collection/MutableSetInterface.php#L15)
2324
- [MutableVectorInterface](./../../src/Psl/Collection/MutableVectorInterface.php#L15)
25+
- [SetInterface](./../../src/Psl/Collection/SetInterface.php#L14)
2426
- [VectorInterface](./../../src/Psl/Collection/VectorInterface.php#L14)
2527

2628
#### `Classes`
2729

2830
- [Map](./../../src/Psl/Collection/Map.php#L25)
2931
- [MutableMap](./../../src/Psl/Collection/MutableMap.php#L25)
32+
- [MutableSet](./../../src/Psl/Collection/MutableSet.php#L23)
3033
- [MutableVector](./../../src/Psl/Collection/MutableVector.php#L23)
34+
- [Set](./../../src/Psl/Collection/Set.php#L23)
3135
- [Vector](./../../src/Psl/Collection/Vector.php#L22)
3236

3337

src/Psl/Collection/AccessibleCollectionInterface.php

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function keys(): AccessibleCollectionInterface;
4343
* that meet a supplied condition.
4444
*
4545
* Only values that meet a certain criteria are affected by a call to
46-
* `filter()`, while all values are affected by a call to `map()`.
46+
* `filter()`.
4747
*
4848
* The keys associated with the current `AccessibleCollectionInterface` remain unchanged in the
4949
* returned `AccessibleCollectionInterface`.
@@ -61,8 +61,7 @@ public function filter(Closure $fn): AccessibleCollectionInterface;
6161
* that meet a supplied condition applied to its keys and values.
6262
*
6363
* Only keys and values that meet a certain criteria are affected by a call
64-
* to `filterWithKey()`, while all values are affected by a call to
65-
* `mapWithKey()`.
64+
* to `filterWithKey()`.
6665
*
6766
* The keys associated with the current `AccessibleCollectionInterface` remain unchanged in the
6867
* returned `AccessibleCollectionInterface`; the keys will be used in the filtering process only.
@@ -76,48 +75,6 @@ public function filter(Closure $fn): AccessibleCollectionInterface;
7675
*/
7776
public function filterWithKey(Closure $fn): AccessibleCollectionInterface;
7877

79-
/**
80-
* Returns a `AccessibleCollectionInterface` after an operation has been applied to each value
81-
* in the current `AccessibleCollectionInterface`.
82-
*
83-
* Every value in the current Map is affected by a call to `map()`, unlike
84-
* `filter()` where only values that meet a certain criteria are affected.
85-
*
86-
* The keys will remain unchanged from the current `AccessibleCollectionInterface` to the
87-
* returned `AccessibleCollectionInterface`.
88-
*
89-
* @template Tu
90-
*
91-
* @param (Closure(Tv): Tu) $fn The callback containing the operation to apply to the current
92-
* `AccessibleCollectionInterface` values.
93-
*
94-
* @return AccessibleCollectionInterface<Tk, Tu> A `AccessibleCollectionInterface` containing key/value
95-
* pairs after a user-specified operation is applied.
96-
*/
97-
public function map(Closure $fn): AccessibleCollectionInterface;
98-
99-
/**
100-
* Returns a `AccessibleCollectionInterface` after an operation has been applied to each key and
101-
* value in the current `AccessibleCollectionInterface`.
102-
*
103-
* Every key and value in the current `AccessibleCollectionInterface` is affected by a call to
104-
* `mapWithKey()`, unlike `filterWithKey()` where only values that meet a
105-
* certain criteria are affected.
106-
*
107-
* The keys will remain unchanged from this `AccessibleCollectionInterface` to the returned
108-
* `AccessibleCollectionInterface`. The keys are only used to help in the mapping operation.
109-
*
110-
* @template Tu
111-
*
112-
* @param (Closure(Tk, Tv): Tu) $fn The callback containing the operation to apply to the current
113-
* `AccessibleCollectionInterface` keys and values.
114-
*
115-
* @return AccessibleCollectionInterface<Tk, Tu> A `AccessibleCollectionInterface` containing the values
116-
* after a user-specified operation on the current
117-
* `AccessibleCollectionInterface`'s keys and values is applied.
118-
*/
119-
public function mapWithKey(Closure $fn): AccessibleCollectionInterface;
120-
12178
/**
12279
* Returns the first value in the current `AccessibleCollectionInterface`.
12380
*

src/Psl/Collection/CollectionInterface.php

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ public function jsonSerialize(): array;
6060
* Returns a `CollectionInterface` containing the values of the current `CollectionInterface`
6161
* that meet a supplied condition.
6262
*
63-
* Only values that meet a certain criteria are affected by a call to
64-
* `filter()`, while all values are affected by a call to `map()`.
63+
* Only values that meet a certain criteria are affected by a call to `filter()`.
6564
*
6665
* The keys associated with the current `CollectionInterface` remain unchanged in the
6766
* returned `CollectionInterface`.
@@ -79,8 +78,7 @@ public function filter(Closure $fn): CollectionInterface;
7978
* that meet a supplied condition applied to its keys and values.
8079
*
8180
* Only keys and values that meet a certain criteria are affected by a call
82-
* to `filterWithKey()`, while all values are affected by a call to
83-
* `mapWithKey()`.
81+
* to `filterWithKey()`.
8482
*
8583
* The keys associated with the current `CollectionInterface` remain unchanged in the
8684
* returned `CollectionInterface`; the keys will be used in the filtering process only.
@@ -94,47 +92,6 @@ public function filter(Closure $fn): CollectionInterface;
9492
*/
9593
public function filterWithKey(Closure $fn): CollectionInterface;
9694

97-
/**
98-
* Returns a `CollectionInterface` after an operation has been applied to each value
99-
* in the current `CollectionInterface`.
100-
*
101-
* Every value in the current Map is affected by a call to `map()`, unlike
102-
* `filter()` where only values that meet a certain criteria are affected.
103-
*
104-
* The keys will remain unchanged from the current `CollectionInterface` to the
105-
* returned `CollectionInterface`.
106-
*
107-
* @template Tu
108-
*
109-
* @param (Closure(Tv): Tu) $fn The callback containing the operation to apply to the current
110-
* `CollectionInterface` values.
111-
*
112-
* @return CollectionInterface<Tk, Tu> A `CollectionInterface` containing key/value pairs after
113-
* a user-specified operation is applied.
114-
*/
115-
public function map(Closure $fn): CollectionInterface;
116-
117-
/**
118-
* Returns a `CollectionInterface` after an operation has been applied to each key and
119-
* value in the current `CollectionInterface`.
120-
*
121-
* Every key and value in the current `CollectionInterface` is affected by a call to
122-
* `mapWithKey()`, unlike `filterWithKey()` where only values that meet a
123-
* certain criteria are affected.
124-
*
125-
* The keys will remain unchanged from this `CollectionInterface` to the returned
126-
* `CollectionInterface`. The keys are only used to help in the mapping operation.
127-
*
128-
* @template Tu
129-
*
130-
* @param (Closure(Tk, Tv): Tu) $fn The callback containing the operation to apply to the current
131-
* `CollectionInterface` keys and values.
132-
*
133-
* @return CollectionInterface<Tk, Tu> A `CollectionInterface` containing the values after a user-specified
134-
* operation on the current `CollectionInterface`'s keys and values is applied.
135-
*/
136-
public function mapWithKey(Closure $fn): CollectionInterface;
137-
13895
/**
13996
* Returns a `CollectionInterface` where each element is a `array{0: Tv, 1: Tu}` that combines the
14097
* element of the current `CollectionInterface` and the provided elements array.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Psl\Collection\Exception;
6+
7+
use Psl\Exception;
8+
9+
final class InvalidOffsetException extends Exception\UnexpectedValueException implements ExceptionInterface
10+
{
11+
}

src/Psl/Collection/Exception/OutOfBoundsException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ final class OutOfBoundsException extends Exception\OutOfBoundsException implemen
1212
/**
1313
* @psalm-mutation-free
1414
*/
15-
public static function for(string|int $offset): OutOfBoundsException
15+
public static function for(int|string $offset): OutOfBoundsException
1616
{
1717
return new self(Str\format('Key (%s) was out-of-bounds.', $offset));
1818
}

src/Psl/Collection/IndexAccessInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ interface IndexAccessInterface
2323
*
2424
* @psalm-mutation-free
2525
*/
26-
public function at(string|int $k): mixed;
26+
public function at(int|string $k): mixed;
2727

2828
/**
2929
* Determines if the specified key is in the current collection.
@@ -43,5 +43,5 @@ public function contains(int|string $k): bool;
4343
*
4444
* @psalm-mutation-free
4545
*/
46-
public function get(string|int $k): mixed;
46+
public function get(int|string $k): mixed;
4747
}

src/Psl/Collection/Map.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ public function jsonSerialize(): array
219219
*
220220
* @psalm-mutation-free
221221
*/
222-
public function at(string|int $k): mixed
222+
public function at(int|string $k): mixed
223223
{
224224
if (!array_key_exists($k, $this->elements)) {
225225
throw Exception\OutOfBoundsException::for($k);
@@ -249,7 +249,7 @@ public function contains(int|string $k): bool
249249
*
250250
* @psalm-mutation-free
251251
*/
252-
public function get(string|int $k): mixed
252+
public function get(int|string $k): mixed
253253
{
254254
return $this->elements[$k] ?? null;
255255
}

0 commit comments

Comments
 (0)