Giter Site home page Giter Site logo

chequer-php's Introduction

Chequer 0.2

THE SPARKLING NEW LANGUAGE FOR CHECKING THINGS IN A SANE WAY
Match scalars, arrays, objects and grumpy cats against the query of Your choice!

Checkout the chequer.stamina.pl for more information!

Oh wait! There is more!

Part of the package is DynamicObject class, which lets you dynamically create classes in PHP, modify object's methods on the fly, extend objects and moar! Go check it out! It's here to make typecasting easy, but it's pretty awesome on it's own!


Install

Use Composer package stamina/chequer-php to install.

The minimum required PHP version is 5.3. Because 5.4 introduces the shorthand array syntax - this version is recommended and used in this documentation.

php composer.phar require stamina/chequer-php

Build Status


Usage and examples

Usage

There are a couple of usage patterns to choose from.

Simple checks

For simple checks use static function checkValue()

if (Cheque::checkValue($value, $query)) {}

Reusing the query

When you want to reuse your query, or pass it somewhere as a callback, create the object and call check method, or invoke the object.

// build the query object
$chequer = new Chequer($query);

// use it witch check()
if ($chequer->check($value)) {}

// or invoke it as a function
if ($chequer($value)) {}

// or pass it as a callback
array_filter($array, $chequer);

Global configuration

You can store all your queries in configuration files and use them when they are needed. This way you can separate your validation/filtering logic from your code - just like you do with the templates!

// load the rules from the JSON file
Chequer::addGlobalRules(json_decode(file_get_contents('queries.json'), JSON_OBJECT_AS_ARRAY));

// reuse them
if (Chequer::checkValue($value, ['$rule' => 'some_defined_rule'])) {}

As every query is a scalar or an array - they can be easely stored in JSON, YAML, MongoDB - you name it.

Dependency injection

If you rather prefer DI - fret not. You can add rules to Chequer objects directly, which means you can make a factory, or pass the Chequer object around and still populate it with predefined rules.

The above example rewritten as Silex factory:

// load the queries once
$app['chequer.rules'] = $app->share(function() {
        return json_decode(file_get_contents('queries.json'), JSON_OBJECT_AS_ARRAY);
    });
// always have a fresh chequer on hand
$app['chequer'] = function() use ($app) { 
    return (new Chequer())->addRules($app['chequer.rules']);
};

// reuse
if ($app['chequer']->query($value, ['$rule' => 'some_defined_rule'])) {}

Note, that the whole idea is very fresh. I've come up with the concept on January 29th, and made the lib the same day.
And that means - it will change!

©2013 Rafal Lindemann

githalytics.com alpha

chequer-php's People

Contributors

panrafal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

alixaxel

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.