Skip to content

Commit e2e1262

Browse files
committed
Collection: Introduce dictionary interface, class and trait
1 parent a51daef commit e2e1262

File tree

6 files changed

+147
-93
lines changed

6 files changed

+147
-93
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: 5 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22

33
namespace Salient\Collection;
44

5-
use Salient\Contract\Collection\CollectionInterface;
5+
use Salient\Contract\Collection\DictionaryInterface;
66

77
/**
88
* @api
99
*
1010
* @template TKey of array-key
1111
* @template TValue
12-
* @template TKeyless
1312
*
14-
* @phpstan-require-implements CollectionInterface
13+
* @phpstan-require-implements DictionaryInterface
1514
*/
16-
trait CollectionTrait
15+
trait DictionaryTrait
1716
{
1817
/** @use ReadOnlyCollectionTrait<TKey,TValue> */
1918
use ReadOnlyCollectionTrait;
@@ -41,17 +40,6 @@ public function unset($key)
4140
return $this->replaceItems($items);
4241
}
4342

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-
5543
/**
5644
* @inheritDoc
5745
*/
@@ -95,7 +83,7 @@ public function reverse()
9583
/**
9684
* @inheritDoc
9785
*/
98-
public function map(callable $callback, int $mode = CollectionInterface::CALLBACK_USE_VALUE)
86+
public function map(callable $callback, int $mode = DictionaryInterface::CALLBACK_USE_VALUE)
9987
{
10088
$prev = null;
10189
$item = null;
@@ -123,7 +111,7 @@ public function map(callable $callback, int $mode = CollectionInterface::CALLBAC
123111
/**
124112
* @inheritDoc
125113
*/
126-
public function filter(callable $callback, int $mode = CollectionInterface::CALLBACK_USE_VALUE)
114+
public function filter(callable $callback, int $mode = DictionaryInterface::CALLBACK_USE_VALUE)
127115
{
128116
$prev = null;
129117
$item = null;
@@ -196,21 +184,6 @@ public function slice(int $offset, ?int $length = null)
196184
return $this->maybeReplaceItems($items);
197185
}
198186

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-
214187
/**
215188
* @inheritDoc
216189
*/
@@ -240,21 +213,6 @@ public function shift(&$first = null)
240213
return $this->replaceItems($items);
241214
}
242215

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-
258216
/**
259217
* @param TKey|null $offset
260218
* @param TValue $value

src/Toolkit/Collection/ReadOnlyCollectionTrait.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Salient\Collection;
44

5-
use Salient\Contract\Collection\CollectionInterface;
5+
use Salient\Contract\Collection\DictionaryInterface;
66
use Salient\Contract\Core\Arrayable;
77
use Salient\Contract\Core\Comparable;
88
use Salient\Contract\Core\Jsonable;
@@ -22,7 +22,7 @@
2222
* @template TKey of array-key
2323
* @template TValue
2424
*
25-
* @phpstan-require-implements CollectionInterface
25+
* @phpstan-require-implements DictionaryInterface
2626
* @phpstan-require-implements IteratorAggregate
2727
*/
2828
trait ReadOnlyCollectionTrait
@@ -68,7 +68,7 @@ public function get($key)
6868
/**
6969
* @inheritDoc
7070
*/
71-
public function forEach(callable $callback, int $mode = CollectionInterface::CALLBACK_USE_VALUE)
71+
public function forEach(callable $callback, int $mode = DictionaryInterface::CALLBACK_USE_VALUE)
7272
{
7373
$prev = null;
7474
$item = null;
@@ -91,7 +91,7 @@ public function forEach(callable $callback, int $mode = CollectionInterface::CAL
9191
/**
9292
* @inheritDoc
9393
*/
94-
public function find(callable $callback, int $mode = CollectionInterface::CALLBACK_USE_VALUE | CollectionInterface::FIND_VALUE)
94+
public function find(callable $callback, int $mode = DictionaryInterface::CALLBACK_USE_VALUE | DictionaryInterface::FIND_VALUE)
9595
{
9696
$prev = null;
9797
$item = null;
@@ -349,31 +349,31 @@ protected function compareItems($a, $b): int
349349
}
350350

351351
/**
352-
* @param int-mask-of<CollectionInterface::*> $mode
352+
* @param int-mask-of<DictionaryInterface::*> $mode
353353
* @param TKey $key
354354
* @param TValue $value
355355
* @return ($mode is 3|11|19 ? array{TKey,TValue} : ($mode is 2|10|18 ? TKey : TValue))
356356
*/
357357
protected function getCallbackValue(int $mode, $key, $value)
358358
{
359-
$mode &= CollectionInterface::CALLBACK_USE_BOTH;
360-
return $mode === CollectionInterface::CALLBACK_USE_KEY
359+
$mode &= DictionaryInterface::CALLBACK_USE_BOTH;
360+
return $mode === DictionaryInterface::CALLBACK_USE_KEY
361361
? $key
362-
: ($mode === CollectionInterface::CALLBACK_USE_BOTH
362+
: ($mode === DictionaryInterface::CALLBACK_USE_BOTH
363363
? [$key, $value]
364364
: $value);
365365
}
366366

367367
/**
368-
* @param int-mask-of<CollectionInterface::*> $mode
368+
* @param int-mask-of<DictionaryInterface::*> $mode
369369
* @param TKey $key
370370
* @param TValue $value
371371
* @return ($mode is 16|17|18|19 ? TKey : TValue)
372372
*/
373373
protected function getReturnValue(int $mode, $key, $value)
374374
{
375-
return $mode & CollectionInterface::FIND_KEY
376-
&& !($mode & CollectionInterface::FIND_VALUE)
375+
return $mode & DictionaryInterface::FIND_KEY
376+
&& !($mode & DictionaryInterface::FIND_VALUE)
377377
? $key
378378
: $value;
379379
}
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+
}

0 commit comments

Comments
 (0)