Skip to content

Commit c3e9a1f

Browse files
authored
Merge pull request #18 from gameeapp/add_to_list_method
feat: methods added to unique collection: toList feat: optimized getFirst/Last methods
2 parents 76b3e29 + b67a465 commit c3e9a1f

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

src/Collection/UniqueObjectCollection.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,15 +218,15 @@ public function exists(string|int $key): bool
218218

219219

220220
/**
221-
* Starts with zero (0)
222-
*
223221
* @return IdentifiableObject
224222
*
225223
* @throws ItemDoesNotExistException
226224
*/
227225
public function getFirst()
228226
{
229-
return $this->getByIndex(1);
227+
return $this->get(
228+
\array_key_first($this->data) ?? throw new ItemDoesNotExistException('first', 'index'),
229+
);
230230
}
231231

232232

@@ -237,8 +237,8 @@ public function getFirst()
237237
*/
238238
public function getLast()
239239
{
240-
return $this->getByIndex(
241-
\count($this->data),
240+
return $this->get(
241+
\array_key_last($this->data) ?? throw new ItemDoesNotExistException('last', 'index'),
242242
);
243243
}
244244

@@ -336,12 +336,24 @@ public function contains(object $item): bool
336336
}
337337

338338

339-
public function jsonSerialize(): array
339+
/**
340+
* @return array<int, IdentifiableObject>
341+
*/
342+
public function toList(): array
340343
{
341344
return \array_values($this->getItems());
342345
}
343346

344347

348+
/**
349+
* @return array<int, IdentifiableObject>
350+
*/
351+
public function jsonSerialize(): array
352+
{
353+
return $this->toList();
354+
}
355+
356+
345357
/**
346358
* @param IdentifiableObject $item
347359
*/
@@ -351,6 +363,9 @@ protected function getIdentifier(object $item): string|int
351363
}
352364

353365

366+
/**
367+
* @return array<int|string, IdentifiableObject>
368+
*/
354369
protected function getItems(): array
355370
{
356371
return $this->data;

tests/unit/Collection/UniqueObjectCollectionTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,42 @@ public function testContains(): void
592592
}
593593

594594

595+
public function testToList(): void
596+
{
597+
$list = [
598+
new JsonSerializableClass(3),
599+
new JsonSerializableClass(5),
600+
new JsonSerializableClass(7),
601+
];
602+
603+
$collection = new UniqueObjectCollection(
604+
$list,
605+
);
606+
607+
$listFromCollection = $collection->toList();
608+
609+
Assert::same(
610+
count($listFromCollection),
611+
count($list),
612+
);
613+
614+
Assert::same(
615+
array_keys($listFromCollection),
616+
array_keys($list),
617+
);
618+
619+
Assert::same(
620+
$listFromCollection[0],
621+
$list[0],
622+
);
623+
624+
Assert::same(
625+
$listFromCollection[1],
626+
$list[1],
627+
);
628+
}
629+
630+
595631
public function testJsonSerialize(): void
596632
{
597633
$collection = new UniqueObjectCollection(

0 commit comments

Comments
 (0)