-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More modernisation and refinement of the API
Require item class name to be provided via abstract method `getItemClassName()` instead of via the collection constructor. Removed support for `ArrayAccess`, and replicate functionality via more meaningful methods such as `Collection::has()` and `Collection::get()`. Added the factory methods `Collection::from()` and `Collection::empty()`. Added helper methods `Collection::isEmpty()` and `Collection::isNotEmpty()`. Added `Collection::reverse()` helper to reverse the order of the collection.
- Loading branch information
Showing
15 changed files
with
446 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Headsnet\Collections; | ||
|
||
use Headsnet\Collections\Exception\ItemNotFoundException; | ||
|
||
/** | ||
* @template TValue | ||
* @extends AbstractImmutableCollection<TValue> | ||
* @implements MutableCollection<TValue> | ||
*/ | ||
abstract class AbstractMutableCollection extends AbstractImmutableCollection implements MutableCollection | ||
{ | ||
/** | ||
* @param TValue $item | ||
*/ | ||
public function add($item): void | ||
{ | ||
$this->items[] = $item; | ||
} | ||
|
||
/** | ||
* @param TValue $item | ||
*/ | ||
public function remove($item): void | ||
{ | ||
$this->items = array_filter( | ||
$this->items, | ||
fn ($storedItem): bool => $item !== $storedItem | ||
); | ||
} | ||
|
||
public function removeAtPosition(int $index): void | ||
{ | ||
if (!array_key_exists($index, $this->items)) { | ||
throw new ItemNotFoundException(); | ||
} | ||
|
||
unset($this->items[$index]); | ||
$this->items = array_values($this->items); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Headsnet\Collections; | ||
|
||
use Headsnet\Collections\Exception\ItemNotFoundException; | ||
|
||
/** | ||
* @template TValue | ||
*/ | ||
interface Collection | ||
{ | ||
/** | ||
* @param array<TValue> $items | ||
*/ | ||
public static function from(array $items): static; | ||
|
||
public function getItemClassName(): string; | ||
|
||
/** | ||
* @return TValue | ||
*/ | ||
public function get(int $index); | ||
|
||
public function has(int $index): bool; | ||
|
||
public function isEmpty(): bool; | ||
|
||
public function isNotEmpty(): bool; | ||
|
||
/** | ||
* @return TValue|null | ||
*/ | ||
public function first(); | ||
|
||
/** | ||
* @throws ItemNotFoundException | ||
* | ||
* @return TValue | ||
*/ | ||
public function firstOrFail(); | ||
|
||
/** | ||
* @return TValue|null | ||
*/ | ||
public function last(); | ||
|
||
/** | ||
* @throws ItemNotFoundException | ||
* | ||
* @return TValue | ||
*/ | ||
public function lastOrFail(); | ||
|
||
/** | ||
* @param Collection<TValue> $compareWith | ||
*/ | ||
public function equals(Collection $compareWith): bool; | ||
|
||
/** | ||
* @param TValue $element | ||
*/ | ||
public function contains($element): bool; | ||
|
||
/** | ||
* @return array<mixed> | ||
*/ | ||
public function map(callable $func): array; | ||
|
||
/** | ||
* @return static<TValue> | ||
*/ | ||
public function filter(callable $func): Collection|static; | ||
|
||
/** | ||
* @return static<TValue> | ||
*/ | ||
public function reverse(): Collection|static; | ||
|
||
public function walk(callable $func): bool; | ||
|
||
/** | ||
* @return array<int, TValue> | ||
*/ | ||
public function toArray(): array; | ||
|
||
public function count(): int; | ||
} |
Oops, something went wrong.