Code Monkey home page Code Monkey logo

php-collections's People

Contributors

bryant1410 avatar carusogabriel avatar danfah avatar danielgsims avatar dkraczkowski avatar duvalentini avatar hultberg avatar jakubgiminski avatar jamesthomasonjr avatar jmrieger avatar omnisite avatar peter279k avatar rednblack avatar tsawler avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

php-collections's Issues

Change private $items to protected

Your work looks good.
However, I cannot extend Collections.php and work with $items.
You need to change
private $items;
to
protected $items;
Otherwise the class becomes useless

Setter for collection type

At work we use this collections to improve type hinting. Example:

use \Foo\Bar\UrlCollection;

class SomeClass
{
    /** @var \Foo\Bar\UrlCollection */
    private $urls;

    public function __construct(UrlCollection $urls)
    {
        $this->urls = $urls;
    }

    public function getUrls() : UrlCollection
    {
        return $this->urls;
    }
}
$config = new SomeClass(new UrlCollection(Url::class, $items));

But we always need to declare the collection type, which is strange because we have a class for the collection and it should know his type.

As result we ended up with this abstract class:

declare(strict_types=1);

namespace Foo\Bar;

use Collections\Collection;

abstract class SmartTypeCollection extends Collection
{
    /**
     * Creates a new collection with only $typeOrItems parameter.
     * @param string|mixed[] $typeOrItems you will pass only this parameter that contains you data in an array.
     * @param mixed[]        $items this parameter will be passed only by Collection class.
     * @throws \Collections\Exceptions\InvalidArgumentException
     */
    public function __construct($typeOrItems, ?array $items = [])
    {
        if (\is_string($typeOrItems)) {
            $type = $typeOrItems;
        } else {
            $type = $this->getSmartType();
        }

        if (\is_array($typeOrItems)) {
            $items = $typeOrItems;
        }

        parent::__construct($type, $items);
    }

    /**
     * This method will return a string specifying the type for the collection's items.
     */
    abstract protected function getSmartType() : string;
}

Wich allow us to do this:

namespace \Foo\Bar;

class UrlCollection extends SmartTypeCollection
{
    /** {@inheritdoc} */
    protected function getSmartType() : string
    {
        return \Foo\Bar\Url::class;
    }
}
$config = new SomeClass(new UrlCollection($items));

I'm wondering if it makes sense to you, and if so, there is a way to add this feature into this project.

PHP 8.1 Deprecation

Deprecated: Return type of Collections\CollectionInterface::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /srv/vendor/danielgsims/php-collections/src/CollectionInterface.php on line 121

Cookbook / Examples

Start a cookbook or examples section for how to use this library! Show examples like sorting, filtering, pagination, etc!

Manipulating current collection

What methods are there to manipulate an existing collection? Every method now returns a new collection which is not what the methods describe or what one would expect of them.

wrong return param

in Collection.php you have:

    public function findLastIndex(callable $condition)
    {
        $index = -1;

        for ($i = count($this->items) - 1; $i >= 0; $i--) {
            if ($condition($this->items[$i])) {
                $index = $i;
                break;
            }
        }

        return $i;
    }

method must return $index instead of $i according to your logic or you should get rid of $index

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.