GithubHelp home page GithubHelp logo

collection's Introduction

Aeviiq PHP Typed Collections

Why

To provide an easy way to ensure type safety on collections/arrays and provide useful custom methods for object collections, as seen in the example below.

Installation

composer require aeviiq/collection

Declaration

/**
 * @extends ObjectCollection<int, Foo>
 *
 * @method \ArrayIterator|Foo[] getIterator()
 * @method Foo|null first()
 * @method Foo|null last()
 */
final class FooCollection extends ObjectCollection
{
    public function filterReleasedBefore(\DateTimeInterface $dateTime): FooCollection
    {
        return $this->filter(static function (Foo $foo) use ($dateTime): bool {
            return $foo->getReleaseDate() < $dateTime;
        });
    }

    public function filterActives(): FooCollection
    {
        return $this->filter(static function (Foo $foo): bool {
            return $foo->isActive();
        });
    }
    
    protected function allowedInstance(): string
    {
        return Foo::class;
    }
}

Usage

// Useful custom methods for ObjectCollections:
$fooCollection = new FooCollection([$foo1, $foo2]);
$result = $fooCollection->filterReleasedBefore(new DateTime('now'))->filterActives();

// Basic type collections that are provided
$intCollection = new IntCollection([1, 2]);
$intCollection->append(3);

$intCollection = new IntCollection([1, '2']); // InvalidArgumentException thrown
$intCollection->append('3');  // InvalidArgumentException thrown

collection's People

Contributors

aeviiq avatar nvdbeek avatar nusje2000 avatar dbrekelmans avatar chi-teck avatar

Stargazers

Loum avatar  avatar  avatar  avatar  avatar  avatar

Watchers

James Cloos avatar  avatar  avatar  avatar

collection's Issues

Generics for Collection functions

It would be nice to have a @template annotation for the default functions (first, last, getIterator and filter) and maybe more, so static code analysis tools like php-stan will understand the return type.

Merge method could cause duplicate values

The current merge method, works as the default array_merge for all collection types, except the object collection. In the object collection, this will create a unique key, without checking the values. This could result in duplicate values, which would make no sense.

To solve this, it might make most sense to make it so that a merge will cause all unique values to be the result of the merge. This would also change the current behavior of the int, float and string collections.

Refactor object collection to solve the deep clone bug itself.

Solution to use the prefixed memory hash for both serialization and (deep) cloning: the internal elements can be rebuild using the newly assigned memory hashes in both calls. In case this works, all methods that can append an element to the collection, can use these hashes instead.

ArrayCollection map function in ObjectCollection

The object collection inherits a map function from the ArrayCollection. This function returns a static version of itself with the mapped values. When using this method on an ObjectCollection it expects that it can construct the same collection as which the function is called on with the mapped values. This is most likely not possible since the map function is probably used to map values from one thing to another, i.e: from objects to integers.

Example

class FooCollection extends ObjectCollection {
    protected function allowedInstance(): string
    {
        return Foo::class;
    }
}

class Foo {
    public $id;

    public function __construct(int $fooId) {
        $this->id = $fooId;
    }
}

$collection = new FooCollection(new Foo(1), new Foo(2), new Foo(3));
$collection->map(function (Foo $foo) {
    return $foo->id; // returning the id of foo (integer)
}); // Fails because it cannot construct a FooCollection with an integer array

Removing element within foreach causes unexpected behaviour

$fooCollection = new FooCollection(); // extends ObjectCollection

$fooCollection->count(); // 5

foreach ($fooCollection as $index => $foo) {
  var_dump($index);
  if ($this->someCondition()) {
    $fooCollection->remove($foo);
  }
}

var_dump() is only called 3 times instead of the expected 5 times.
It works as expected when looping over $fooCollection->toArray() instead.

Use array<key, value> doc-comments instead of type[] when expecting an array

In some cases Collection|mixed[] is used to type a parameter to contain either an array or an instance of the Collection class. This however leads to some problems when using PHPStan. When the type[] format is used in combination with a class that implements the IteratorAggregate interface (like the Collection), the type will be parsed to Collection&iterator<type>. For more information see phpstan/phpstan#1988. An example of this type definition can be found on Collection::merge($input).

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.