Skip to content

Enable InMemory repository to accept items in constructor #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 29, 2023

Conversation

Bl00D4NGEL
Copy link
Collaborator

Currently the InMemoryRepository has no way to accept any items making it basically a "NullRepository". This PR adds the items parameeter to the constructor (as a promoted property) to help mitigate this.

NOTE: This should be merged after #18

Previously a class that extends the Repository would write something like this:

class FooRepository extends Repository {
    public function __construct() {
        parent::__construct(Foo::class);
        $this->items = [new Foo()];
    }
}

If this PR gets merged a user could use a more straight forward interface:

class FooRepository extends Repository {
    public function __construct() {
        parent::__construct(Foo::class, [new Foo()]);
    }
}

In addition this enables users of the InMemoryRepository to implement the "filter" option a bit more straight forward.

Instead of this

public function findByEmail(string $email): self
{
    $filtered = [];
    foreach ($this->items as $item) {
        if ($item->email === $email) {
            filtered[] = $item;
        }
    }

    $self = new self($this->itemType);
    $self->items = $filtered;

     return $self;
}

A user can now do this:

public function findByEmail(string $email): self
{
    $filtered = [];
    foreach ($this->items as $item) {
        if ($item->email === $email) {
            filtered[] = $item;
        }
    }

    return new self($this->itemType, $filtered);
}

@Bl00D4NGEL Bl00D4NGEL requested a review from b00gizm as a code owner November 28, 2023 14:32
@janvt janvt merged commit 66f37e6 into geekcell:main Nov 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants