2017 © Pedro Peláez
 

library criteria

image

madlines/criteria

  • Sunday, December 4, 2016
  • by XanderEmu
  • Repository
  • 1 Watchers
  • 0 Stars
  • 10 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 2 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Madlines Criteria

This library lets you prepare a set of search criteria in a nice, object-oriented way without having to use e.g. ORM-specific query builder., (*1)

Keep in mind that this lib isn't trying to replace any query builders (like e.g. the one shipped with Doctrine 2 ORM)., (*2)

A Criteria's structure makes it pretty easy to write a parser which will translate that object into a query you can send to your DB / SearchEngine / API / whatever., (*3)

Example usage

<?php

use Madlines\Criteria\Criteria;
use Madlines\Criteria\Criterion\Equals;
use Madlines\Criteria\Criterion\StartsWith;

$criteria = new Criteria();
$criteria = $criteria // $criteria is immutable so you have to store result of adding
    ->add(new Equals('foo', 'bar'))
    ->add(new StartsWith('lorem', 'ipsum'));

// later in your code
$criteriaArr = $criteria->getAll(); // returns an array containing all criteria
$keys = $criteria->getKeys(); // returns all keys (foo and lorem in this case)
$foo = $criteria->getFor('foo'); // return criterion object for given key

Criteria Builder

You can automate process of building a Criteria object using CriteriaBuilder and one of mapping strategies., (*4)

You can create your own strategies or use a built-in DefaultMappingStrategy which maps input to built-in Criterion types. You can also create a map manually., (*5)

<?php

use Madlines\Criteria\Criteria;
use Madlines\Criteria\Criterion;

$builder = new \Madlines\Criteria\CriteriaBuilder();
$builder->setMappingStrategy(function ($operator) {
    if ('eq' === $operator) {
        return Criterion\Equals::class;
    }

    if ('contains' === $operator) {
        return Criterion\Contains::class;
    }

    return '';
});

// Optionally you can add some mapping manually
$builder->map('starts_with', Criterion\StartsWith::class);
$builder->setDefault('eq');

// e.g. $_GET array
$input = [
    'foo__eq' => 'Cat',
    'bar__contains' => 'Dog',
];

$criteria = $builder->buildFromArray($input, new Criteria());

Allowed keys

If you want to allow creating a criteria only for certain set of fields you have to extend a Criteria class and override getAllowedKeys method, (*6)

<?php

class MyCriteria extends \Madlines\Criteria\Criteria
{
    public function getAllowedKeys()
    {
        return ['lorem', 'ipsum'];
    }
}

The Versions

04/12 2016

dev-master

9999999-dev

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Aleksander Ciesiołkiewicz

18/05 2016

dev-develop

dev-develop

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Aleksander Ciesiołkiewicz