Particle\Filter

Travis-CI Packagist Packagist downloads Scrutinizer Scrutinizer

Particle\Filter is a very small filtering library, with the easiest and most usable API we could possibly create.

Quick usage example

$f = new Particle\Filter\Filter;

$f->values(['user.first_name', 'user.last_name'])->trim()->lower()->upperFirst();
$f->value('newsletter')->bool();
$f->value('created_at')->defaults(date('Y-m-d'));
$f->all()->removeNull();

$result = $f->filter([
    'user' => [
        'first_name' => '  JOHN ',
        'middle_name' => null,
        'last_name' => ' DOE  ',
    ],
    'newsletter' => 'yes',
    'referral' => null,
]);

var_dump($result);
/**
 * array(3) {
 *     ["user"]=> array(2) {
 *         ["first_name"]=> string(4) "John"
 *         ["last_name"]=> string(3) "Doe"
 *     }
 *     ["newsletter"]=> bool(true)
 *     ["created_at"]=> string(10) "2015-12-10"
 * } 
 */

Why Particle\Filter?

User input can contain pretty much anything. Filtering your data should always be done to make sure your data is correct. With Particle\Filter you have an easy and quick way of doing that. There is an extremely easy API with full auto-completion support in your IDE.

  • A clear API, so you understand it just by looking at it, and adding a filter is a breeze.
  • IDE-supported code-completion, so you don't have to look at documentation to write a rule.
  • Well-documented, so that if you do go to the documentation, you don't have to search for long.
  • Extensible, so you can simply add your own filter-rules.
  • Well-tested, so that you're absolutely sure that you can rely on the validation rules.
  • Zero dependencies, so that you can use it in any PHP project.

Although there are many filtering libraries out there, they seem to lack in one or more of the above design goals.