File tree Expand file tree Collapse file tree 2 files changed +33
-1
lines changed Expand file tree Collapse file tree 2 files changed +33
-1
lines changed Original file line number Diff line number Diff line change 44
55namespace Phant \DataStructure \Abstract ;
66
7+ /**
8+ * @template T
9+ */
710abstract class Collection
811{
12+ /**
13+ * @var array<T>
14+ */
915 protected array $ items = [];
1016
1117 public function __construct (
1218 ) {
1319 }
1420
21+ /**
22+ * @param T $item
23+ * @return static
24+ */
1525 protected function addItem (
1626 mixed $ item
1727 ): static {
@@ -22,6 +32,10 @@ protected function addItem(
2232 return $ this ;
2333 }
2434
35+ /**
36+ * @param T $item
37+ * @return static
38+ */
2539 protected function removeItem (
2640 mixed $ item
2741 ): static {
@@ -33,12 +47,18 @@ protected function removeItem(
3347 return $ this ;
3448 }
3549
50+ /**
51+ * @param T $item
52+ */
3653 final public function contains (
3754 mixed $ item
3855 ): bool {
3956 return array_search ($ item , $ this ->items ) !== false ;
4057 }
4158
59+ /**
60+ * @return \Generator<T>
61+ */
4262 final public function iterate (
4363 ): \Generator {
4464 foreach ($ this ->items as $ item ) {
@@ -56,12 +76,19 @@ final public function getNbItems(
5676 return count ($ this ->items );
5777 }
5878
79+ /**
80+ * @return T|null
81+ */
5982 final public function getByKey (
6083 int $ key
6184 ): mixed {
6285 return $ this ->items [ $ key ] ?? null ;
6386 }
6487
88+ /**
89+ * @param Collection<T> $collection
90+ * @return static
91+ */
6592 final public function merge (
6693 self $ collection
6794 ): static {
@@ -72,6 +99,11 @@ final public function merge(
7299 return $ this ;
73100 }
74101
102+ /**
103+ * @template U
104+ * @param callable(T): U $callback
105+ * @return array<U>
106+ */
75107 final public function map (
76108 callable $ callback
77109 ): array {
Original file line number Diff line number Diff line change @@ -76,7 +76,7 @@ public function testMap(): void
7676 $ collection ->add ($ value1 );
7777 $ collection ->add ($ value2 );
7878
79- $ result = $ collection ->map (fn (Value $ item ) => (string )$ item . '! ' );
79+ $ result = $ collection ->map (fn (Value $ item ) => (string )$ item . '! ' );
8080
8181 $ this ->assertIsArray ($ result );
8282 $ this ->assertCount (2 , $ result );
You canβt perform that action at this time.
0 commit comments