Skip to content
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

Use native type checking combined with variadic parameters #1

Open
holtkamp opened this issue Apr 10, 2017 · 0 comments
Open

Use native type checking combined with variadic parameters #1

holtkamp opened this issue Apr 10, 2017 · 0 comments

Comments

@holtkamp
Copy link

holtkamp commented Apr 10, 2017

Nice library! Was playing with the same idea.

This concept might be interesting (and better in performance, since it is native?) to implement the TypedCollection:
https://www.sitepoint.com/creating-strictly-typed-arrays-collections-php/

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Collection;

abstract class TypedCollection extends Collection
{

    public function __construct($items = [])
    {
        if (is_array($items)) {
            $this->setItems(...$items);
        }
        if ($items instanceof static) {
            $this->setItems(...$this->items);
        }
        if ($items instanceof Arrayable) {
            $this->setItems(...$items->toArray());
        }

        parent::__construct($items);
    }

    protected abstract function setItems(): void;
}

And then for the specific implementation of the typed Collection:

class Categories extends TypedCollection
{

    protected function setItems(Category ...$items): void
    {
        $this->items = $items;
    }
}

What do you think?

@holtkamp holtkamp changed the title Use native type checking Use native type checking in combination with variadic parameters Apr 10, 2017
@holtkamp holtkamp changed the title Use native type checking in combination with variadic parameters Use native type checking combined with variadic parameters Apr 10, 2017
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

No branches or pull requests

1 participant