Giter Club home page Giter Club logo

zef-expression-language's Introduction

ZEF Expression Language

A simple extension to the Symfony Expression Language which enables more JUEL like behaviour.

  • it will not break on non existing values - it will evaluate to null
  • it enables you to use JUEL style accessing to array values and object methods (dot notation)

Installation

# With composer
$ composer require zef-dev/zef-expression-language

Usage

Just replace Symfony ExpressionLanguage with the Zef one.

use Zef\Zel\Symfony\ExpressionLanguage;

$expressionLanguage =   new ExpressionLanguage();
$evaluated          =   $expressionLanguage->evaluate( 'true', []);

// will not throw an exception any more
$evaluated          =   $expressionLanguage->evaluate( 'myvar', []);
$evaluated          =   $expressionLanguage->evaluate( 'myvar[\'myfield\']', ['myvar'=>[]]);

JUEL like usage

To gain JUEL like evaluation addon, wrap your values array in ArrayResolver.

use Zef\Zel\Symfony\ExpressionLanguage;
use Zef\Zel\ArrayResolver;

$expressionLanguage =   new ExpressionLanguage();
$resolver           =   new ArrayResolver([]);
$evaluated          =   $expressionLanguage->evaluate( 'true', $resolver->getValues());

// now you can access array fields in dot notation
$resolver           =   new ArrayResolver(['myvar'=>['myfield'=>true]]);
$evaluated          =   $expressionLanguage->evaluate( 'myvar.myfield', $resolver->getValues());

// now you can access getters in a shorter way
$obj    =   new Myclass();
$obj->getName();
$obj->isValid();
$obj->hasErrror();

$resolver           =   new ArrayResolver(['myvar'=>$obj]);
$evaluated          =   $expressionLanguage->evaluate( 'myvar.name', $resolver->getValues());
$evaluated          =   $expressionLanguage->evaluate( 'myvar.valid', $resolver->getValues());
$evaluated          =   $expressionLanguage->evaluate( 'myvar.error', $resolver->getValues());

        

Note about implementation

We wanted to extend Symfony classes and override just what was necessary, but as the original classes were not written in a manner to allow it easily, we ended up copy/pasting some classes completely just to be able implement few minor modifications.


This package is created based on the PHP Boilerplate

zef-expression-language's People

Contributors

mmarcec007 avatar tefached95 avatar tolecar avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

coffeeturbo

zef-expression-language's Issues

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.