GithubHelp home page GithubHelp logo

dbrekelmans / collection Goto Github PK

View Code? Open in Web Editor NEW

This project forked from aeviiq/collection

0.0 1.0 0.0 120 KB

A library that provides strict typed collections in PHP.

License: MIT License

PHP 100.00%

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

/**
 * @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]);
$integerCollection->append(3);

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

collection's People

Contributors

aeviiq avatar dbrekelmans avatar nusje2000 avatar

Watchers

James Cloos avatar

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.