tandiljuan/collection

PHP library to create custom collections

1.0.0 2019-01-06 19:12 UTC

This package is auto-updated.

Last update: 2024-04-07 06:54:10 UTC


README

PHP library to create custom collections.

Usage

Define a custom collection, extending library's abstract collection and defining the item and offset types

<?php

class MyCustomCollection extends \Tandiljuan\Collection\AbstractCollection
{
    /**
     * {@inheritdoc}
     */
    protected $itemType = '\DateTime';

    /**
     * {@inheritdoc}
     */
    protected $offsetType = 'integer';
}

Create a collection that only will allow the item and offset types you have defined

<?php

$collection = new MyCustomCollection();

try {
    $collection[] = 'error';
} catch (\Tandiljuan\Collection\Exception\InvalidItemType $e) {
    echo "Invalid item type\n";
}

try {
    $collection['a'] = new \DateTime();
} catch (\Tandiljuan\Collection\Exception\InvalidOffsetType $e) {
    echo "Invalid offset type\n";
}

$collection[] = new DateTime('2001-01-01 01:01:01');
$collection[] = new DateTime('2002-02-02 02:02:02');
$collection[] = new DateTime('2003-03-03 03:03:03');

Require parameters to be of MyCustomCollection using type declarations

<?php

function printItems(MyCustomCollection $parameter)
{
    foreach ($parameter as $item) {
        echo $item->format('r')."\n";
    }
}

printItems($collection);

// The output will be
// Mon, 01 Jan 2001 01:01:01 +0000
// Sat, 02 Feb 2002 02:02:02 +0000
// Mon, 03 Mar 2003 03:03:03 +0000

Development

Contributions must follow Symfony Coding Standards.

It's encouraged to read the following documentation:

The run bash script is a wrapper of a php docker container. It will bootstrap the container and run a given command inside of it. The container should have all needed tools for development. Please, let know the project maintainer if something is missing.

To add new packages in the composer.json file, use composer require

./run composer require [--dev] [vendor/package:version]

To install packages defined in the composer.json file, use composer install

./run composer install

To check PHP code against coding standards, use php-cs-fixer

# Check if a file needs to be fixed
./run php-cs-fixer fix --dry-run --diff path/to/file.php
# Fix a file
./run php-cs-fixer fix path/to/file.php

To run PHP REPL interactive shell, use php or psysh

# Using PHP
./run php -a
# Using psysh
./run psysh

Testing

Bootstrap Codeception test environment. This must be done only once.

$ # Bootstrap codeception without the standard suites
$ ./run codecept bootstrap --empty --namespace 'Tandiljuan\Collection\Tests'

Create a codeception test suit. This must be done only once per suit.

$ # Create `unit` suit (for unit testing)
$ ./run codecept generate:suite unit

Create a Cest class under a given suit. Can create as many Cest classes as needed.

$ # Create a new cest `Dummy` under the `unit` suit
$ ./run codecept generate:cest unit Dummy

After writing the tests, can execute them using the run command

$ ./run codecept run