Giter Site home page Giter Site logo

spec's Introduction

Spec

Build Status

Spec is a PHP implementation of the Specification pattern which can be used for building simple or complex business rules.

In computer programming, the specification pattern is a particular software design pattern, whereby business rules can be recombined by chaining the business rules together using boolean logic. The pattern is frequently used in the context of domain-driven design.

https://en.wikipedia.org/wiki/Specification_pattern

The library uses the incredible expression-language component from Symfony to provide the DSL for expressions.

Installation

$ composer require danfekete/spec

Usage

1. Simple expression

$d = [2];
$spec = new Specification(new ExpressionSpec('1 > d[0]'));
$spec->isSatisfiedBy(['d' => $d]); // return false

2. Boolean chaining

/*
 * You can use classes in expression code
*/
class Nan {

    public function isNan($value)
    {
        return is_nan($value);
    }

}

$spec = new Specification(new AndSpec(
    new NotSpec(new ExpressionSpec('checker.isNan(d)')),
    new OrSpec(
        new ExpressionSpec('d != 12'),
        new ExpressionSpec('d > 10')
    ),
    new AndSpec(
        new ExpressionSpec('d > 5'),
        new ExpressionSpec('d < 20')
    )
));

$spec->isSatisfiedBy(['d' => 17, 'checker' => new Nan()]); // return true

TODO

  1. Cache parsed code permanently
  2. Array builder, builds specification from arrays
  3. JSON builder

License

MIT, see LICENSE

spec's People

Watchers

 avatar  avatar

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.