carlosv2/funnel

Little testing repositories enhancer

1.1.3 2017-02-10 22:54 UTC

This package is not auto-updated.

Last update: 2024-04-13 17:27:35 UTC


README

Little testing repositories enhancer.

License Build Status SensioLabsInsight

This project aims to provide quick and easy filtering capabilities to everzet/persisted-objects.

Usage

You just need to decorate the testing repository with carlosV2\Funnel\Funnel. For example:

use carlosV2\Funnel\Funnel;
use Everzet\PersistedObjects\Repository;

class MyTestingRepository
{
    /**
     * @var Funnel
     */
    private $repository;
    
    /**
     * @param Repository $repository
     */
    public function __construct(Repository $repository)
    {
        $this->repository = new Funnel($repository);
    }
    
    // ...
}

Funnel also implements Everzet\PersistedObjects\Repository so you don't lose any feature and yet you gain some more instead:

  • findAll(): Returns an array with all the objects. This is an alias for getAll.
  • findBy(callable): Returns an array with all the matching objects. Empty array is returned if none is found.
  • findOneBy(callable): Returns the first matching object or null if none is found.
  • countBy(callable): Returns an integer representing the number of matching objects.

For example:

$objects = $funnel->findBy(function ($object) {
    return $object->getData() === 'foo';
});

In addition, Funnel provides some generic filters that can be used to speed up development:

Those filters can be used by composing a new method applying the following rule (where "Not" is optional):

   find |
findOne > + By + [Not] + Filter
  count |

For example:

  • findByProperty(...): Finds all the objects that have the given property and value.
  • findOneByMethod(...): Finds the first object that matches the given method and value.
  • countByNotType(...): Counts all the objects that don't have the given type.

If none of the provided filters match your requirements, you can also add your own filters to Funnel to make them available when composing new methods.

Feel free to create a pull request with your own generic filters! :)

Alternatively, you can decorate an array in order to provide the Funnel API on top of it. For example:

$myArray = [$obj1, $obj2, $obj3];

// Any Funnel-compatible method can be used here
Funnel($myArray)->countByType(\DateTime::class);

Install

Open a command console, enter your project directory and execute the following command to download the latest stable version of this project:

$ composer require carlosv2/funnel

This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.