carlosv2 / funnel
Little testing repositories enhancer
Installs: 2 541
Dependents: 0
Suggesters: 1
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ~5.3 || ~7.0
- carlosv2/communism: ^1.2.2
- everzet/persisted-objects: ^1.0
Requires (Dev)
- phpunit/phpunit: ^4.8
This package is not auto-updated.
Last update: 2024-11-09 20:33:58 UTC
README
Little testing repositories enhancer.
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:
- AllOfFilter
- AnyOfFilter
- MethodFilter
- MethodsFilter
- PropertyFilter
- PropertiesFilter
- TypeFilter
- BeingFilter
- HavingFilter
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.