Skip to content

Commit 69b62ce

Browse files
committed
Collection: Introduce dictionary interface, class and trait
- Remove unnecessary templates from interface methods
1 parent a51daef commit 69b62ce

File tree

6 files changed

+141
-92
lines changed

6 files changed

+141
-92
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Salient\Collection;
4+
5+
use Salient\Contract\Collection\CollectionInterface;
6+
7+
/**
8+
* @api
9+
*
10+
* @template TKey of array-key
11+
* @template TValue
12+
* @template TKeyless
13+
*
14+
* @phpstan-require-implements CollectionInterface
15+
*/
16+
trait CollectionTrait
17+
{
18+
/** @use DictionaryTrait<TKey,TValue> */
19+
use DictionaryTrait;
20+
21+
/**
22+
* @inheritDoc
23+
*/
24+
public function add($value)
25+
{
26+
$items = $this->Items;
27+
$items[] = $value;
28+
/** @var TKeyless */
29+
return $this->replaceItems($items, true);
30+
}
31+
32+
/**
33+
* @inheritDoc
34+
*/
35+
public function push(...$items)
36+
{
37+
if (!$items) {
38+
/** @var TKeyless */
39+
return $this;
40+
}
41+
$_items = $this->Items;
42+
array_push($_items, ...$items);
43+
/** @var TKeyless */
44+
return $this->replaceItems($_items, true);
45+
}
46+
47+
/**
48+
* @inheritDoc
49+
*/
50+
public function unshift(...$items)
51+
{
52+
if (!$items) {
53+
/** @var TKeyless */
54+
return $this;
55+
}
56+
$_items = $this->Items;
57+
array_unshift($_items, ...$items);
58+
/** @var TKeyless */
59+
return $this->replaceItems($_items, true);
60+
}
61+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Salient\Collection;
4+
5+
use Salient\Contract\Collection\DictionaryInterface;
6+
use IteratorAggregate;
7+
8+
/**
9+
* @api
10+
*
11+
* @template TKey of array-key
12+
* @template TValue
13+
*
14+
* @implements DictionaryInterface<TKey,TValue>
15+
* @implements IteratorAggregate<TKey,TValue>
16+
*/
17+
class Dictionary implements DictionaryInterface, IteratorAggregate
18+
{
19+
/** @use DictionaryTrait<TKey,TValue> */
20+
use DictionaryTrait;
21+
}

src/Toolkit/Collection/DictionaryTrait.php

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
namespace Salient\Collection;
44

55
use Salient\Contract\Collection\CollectionInterface;
6+
use Salient\Contract\Collection\DictionaryInterface;
67

78
/**
89
* @api
910
*
1011
* @template TKey of array-key
1112
* @template TValue
12-
* @template TKeyless
1313
*
14-
* @phpstan-require-implements CollectionInterface
14+
* @phpstan-require-implements DictionaryInterface
1515
*/
16-
trait CollectionTrait
16+
trait DictionaryTrait
1717
{
1818
/** @use ReadOnlyCollectionTrait<TKey,TValue> */
1919
use ReadOnlyCollectionTrait;
@@ -41,17 +41,6 @@ public function unset($key)
4141
return $this->replaceItems($items);
4242
}
4343

44-
/**
45-
* @inheritDoc
46-
*/
47-
public function add($value)
48-
{
49-
$items = $this->Items;
50-
$items[] = $value;
51-
/** @var TKeyless */
52-
return $this->replaceItems($items, true);
53-
}
54-
5544
/**
5645
* @inheritDoc
5746
*/
@@ -196,21 +185,6 @@ public function slice(int $offset, ?int $length = null)
196185
return $this->maybeReplaceItems($items);
197186
}
198187

199-
/**
200-
* @inheritDoc
201-
*/
202-
public function push(...$items)
203-
{
204-
if (!$items) {
205-
/** @var TKeyless */
206-
return $this;
207-
}
208-
$_items = $this->Items;
209-
array_push($_items, ...$items);
210-
/** @var TKeyless */
211-
return $this->replaceItems($_items, true);
212-
}
213-
214188
/**
215189
* @inheritDoc
216190
*/
@@ -240,21 +214,6 @@ public function shift(&$first = null)
240214
return $this->replaceItems($items);
241215
}
242216

243-
/**
244-
* @inheritDoc
245-
*/
246-
public function unshift(...$items)
247-
{
248-
if (!$items) {
249-
/** @var TKeyless */
250-
return $this;
251-
}
252-
$_items = $this->Items;
253-
array_unshift($_items, ...$items);
254-
/** @var TKeyless */
255-
return $this->replaceItems($_items, true);
256-
}
257-
258217
/**
259218
* @param TKey|null $offset
260219
* @param TValue $value

src/Toolkit/Collection/ReadOnlyCollectionTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Salient\Collection;
44

55
use Salient\Contract\Collection\CollectionInterface;
6+
use Salient\Contract\Collection\DictionaryInterface;
67
use Salient\Contract\Core\Arrayable;
78
use Salient\Contract\Core\Comparable;
89
use Salient\Contract\Core\Jsonable;
@@ -22,7 +23,7 @@
2223
* @template TKey of array-key
2324
* @template TValue
2425
*
25-
* @phpstan-require-implements CollectionInterface
26+
* @phpstan-require-implements DictionaryInterface
2627
* @phpstan-require-implements IteratorAggregate
2728
*/
2829
trait ReadOnlyCollectionTrait
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Salient\Contract\Collection;
4+
5+
/**
6+
* @api
7+
*
8+
* @template TKey of array-key
9+
* @template TValue
10+
*
11+
* @extends DictionaryInterface<TKey,TValue>
12+
*/
13+
interface CollectionInterface extends DictionaryInterface
14+
{
15+
/**
16+
* Add an item
17+
*
18+
* @param TValue $value
19+
* @return static<TKey|int,TValue>
20+
*/
21+
public function add($value);
22+
23+
/**
24+
* Push items onto the end of the collection
25+
*
26+
* @param TValue ...$items
27+
* @return static<TKey|int,TValue>
28+
*/
29+
public function push(...$items);
30+
31+
/**
32+
* Add items to the beginning of the collection
33+
*
34+
* Items are added in one operation and stay in the given order.
35+
*
36+
* @param TValue ...$items
37+
* @return static<TKey|int,TValue>
38+
*/
39+
public function unshift(...$items);
40+
}

src/Toolkit/Contract/Collection/DictionaryInterface.php

Lines changed: 14 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* @extends Traversable<TKey,TValue>
2121
* @extends Arrayable<TKey,TValue|mixed[]>
2222
*/
23-
interface CollectionInterface extends
23+
interface DictionaryInterface extends
2424
ArrayAccess,
2525
Countable,
2626
JsonSerializable,
@@ -103,14 +103,6 @@ public function get($key);
103103
*/
104104
public function unset($key);
105105

106-
/**
107-
* Add an item
108-
*
109-
* @param TValue $value
110-
* @return static<TKey|int,TValue>
111-
*/
112-
public function add($value);
113-
114106
/**
115107
* Merge the collection with the given items
116108
*
@@ -138,18 +130,16 @@ public function reverse();
138130
*
139131
* The callback's return values are discarded.
140132
*
141-
* @template TMode of int-mask-of<CollectionInterface::*>
142-
*
143133
* @param (callable(TValue, TValue|null $next, TValue|null $prev): mixed)|(callable(TKey, TKey|null $next, TKey|null $prev): mixed)|(callable(array{TKey,TValue}, array{TKey,TValue}|null $next, array{TKey,TValue}|null $prev): mixed) $callback
144134
* @phpstan-param (
145-
* TMode is 3|11|19
135+
* $mode is 3|11|19
146136
* ? (callable(array{TKey,TValue}, array{TKey,TValue}|null $next, array{TKey,TValue}|null $prev): mixed)
147-
* : (TMode is 2|10|18
137+
* : ($mode is 2|10|18
148138
* ? (callable(TKey, TKey|null $next, TKey|null $prev): mixed)
149139
* : (callable(TValue, TValue|null $next, TValue|null $prev): mixed)
150140
* )
151141
* ) $callback
152-
* @param TMode $mode
142+
* @param int-mask-of<CollectionInterface::*> $mode
153143
* @return $this
154144
*/
155145
public function forEach(callable $callback, int $mode = CollectionInterface::CALLBACK_USE_VALUE);
@@ -158,38 +148,35 @@ public function forEach(callable $callback, int $mode = CollectionInterface::CAL
158148
* Pass each item in the collection to a callback and populate a new
159149
* collection with its return values
160150
*
161-
* @template TMode of int-mask-of<CollectionInterface::*>
162151
* @template TReturn of TValue
163152
*
164153
* @param (callable(TValue, TValue|null $next, TValue|null $prev): TReturn)|(callable(TKey, TKey|null $next, TKey|null $prev): TReturn)|(callable(array{TKey,TValue}, array{TKey,TValue}|null $next, array{TKey,TValue}|null $prev): TReturn) $callback
165154
* @phpstan-param (
166-
* TMode is 3|11|19
155+
* $mode is 3|11|19
167156
* ? (callable(array{TKey,TValue}, array{TKey,TValue}|null $next, array{TKey,TValue}|null $prev): TReturn)
168-
* : (TMode is 2|10|18
157+
* : ($mode is 2|10|18
169158
* ? (callable(TKey, TKey|null $next, TKey|null $prev): TReturn)
170159
* : (callable(TValue, TValue|null $next, TValue|null $prev): TReturn)
171160
* )
172161
* ) $callback
173-
* @param TMode $mode
162+
* @param int-mask-of<CollectionInterface::*> $mode
174163
* @return static<TKey,TReturn>
175164
*/
176165
public function map(callable $callback, int $mode = CollectionInterface::CALLBACK_USE_VALUE);
177166

178167
/**
179168
* Reduce the collection to items that satisfy a callback
180169
*
181-
* @template TMode of int-mask-of<CollectionInterface::*>
182-
*
183170
* @param (callable(TValue, TValue|null $next, TValue|null $prev): bool)|(callable(TKey, TKey|null $next, TKey|null $prev): bool)|(callable(array{TKey,TValue}, array{TKey,TValue}|null $next, array{TKey,TValue}|null $prev): bool) $callback
184171
* @phpstan-param (
185-
* TMode is 3|11|19
172+
* $mode is 3|11|19
186173
* ? (callable(array{TKey,TValue}, array{TKey,TValue}|null $next, array{TKey,TValue}|null $prev): bool)
187-
* : (TMode is 2|10|18
174+
* : ($mode is 2|10|18
188175
* ? (callable(TKey, TKey|null $next, TKey|null $prev): bool)
189176
* : (callable(TValue, TValue|null $next, TValue|null $prev): bool)
190177
* )
191178
* ) $callback
192-
* @param TMode $mode
179+
* @param int-mask-of<CollectionInterface::*> $mode
193180
* @return static
194181
*/
195182
public function filter(callable $callback, int $mode = CollectionInterface::CALLBACK_USE_VALUE);
@@ -198,19 +185,17 @@ public function filter(callable $callback, int $mode = CollectionInterface::CALL
198185
* Get the first item that satisfies a callback, or null if there is no such
199186
* item in the collection
200187
*
201-
* @template TMode of int-mask-of<CollectionInterface::*>
202-
*
203188
* @param (callable(TValue, TValue|null $next, TValue|null $prev): bool)|(callable(TKey, TKey|null $next, TKey|null $prev): bool)|(callable(array{TKey,TValue}, array{TKey,TValue}|null $next, array{TKey,TValue}|null $prev): bool) $callback
204189
* @phpstan-param (
205-
* TMode is 3|11|19
190+
* $mode is 3|11|19
206191
* ? (callable(array{TKey,TValue}, array{TKey,TValue}|null $next, array{TKey,TValue}|null $prev): bool)
207-
* : (TMode is 2|10|18
192+
* : ($mode is 2|10|18
208193
* ? (callable(TKey, TKey|null $next, TKey|null $prev): bool)
209194
* : (callable(TValue, TValue|null $next, TValue|null $prev): bool)
210195
* )
211196
* ) $callback
212-
* @param TMode $mode
213-
* @return (TMode is 16|17|18|19 ? TKey : TValue)|null
197+
* @param int-mask-of<CollectionInterface::*> $mode
198+
* @return ($mode is 16|17|18|19 ? TKey : TValue)|null
214199
*/
215200
public function find(callable $callback, int $mode = CollectionInterface::CALLBACK_USE_VALUE | CollectionInterface::FIND_VALUE);
216201

@@ -313,14 +298,6 @@ public function last();
313298
*/
314299
public function nth(int $n);
315300

316-
/**
317-
* Push items onto the end of the collection
318-
*
319-
* @param TValue ...$items
320-
* @return static<TKey|int,TValue>
321-
*/
322-
public function push(...$items);
323-
324301
/**
325302
* Pop an item off the end of the collection
326303
*
@@ -338,14 +315,4 @@ public function pop(&$last = null);
338315
* @return static
339316
*/
340317
public function shift(&$first = null);
341-
342-
/**
343-
* Add items to the beginning of the collection
344-
*
345-
* Items are added in one operation and stay in the given order.
346-
*
347-
* @param TValue ...$items
348-
* @return static<TKey|int,TValue>
349-
*/
350-
public function unshift(...$items);
351318
}

0 commit comments

Comments
 (0)