From 0675b562edf1dc72bf0bab6752ab972a25e70096 Mon Sep 17 00:00:00 2001 From: Wouter J Date: Fri, 29 Nov 2013 08:46:29 +0100 Subject: [PATCH 1/4] Removed assignment operator --- components/expression_language/syntax.rst | 5 ----- 1 file changed, 5 deletions(-) diff --git a/components/expression_language/syntax.rst b/components/expression_language/syntax.rst index 3552cad5b7d..972f16f65ab 100644 --- a/components/expression_language/syntax.rst +++ b/components/expression_language/syntax.rst @@ -128,11 +128,6 @@ For example:: This will print out ``42``. -Assignment Operators -~~~~~~~~~~~~~~~~~~~~ - -* ``=`` - Bitwise Operators ~~~~~~~~~~~~~~~~~ From e506f01321335bf6707889b2e0daf1633a025432 Mon Sep 17 00:00:00 2001 From: Wouter J Date: Fri, 29 Nov 2013 08:49:07 +0100 Subject: [PATCH 2/4] Added example of range operator --- components/expression_language/syntax.rst | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/components/expression_language/syntax.rst b/components/expression_language/syntax.rst index 972f16f65ab..38e4739e0fa 100644 --- a/components/expression_language/syntax.rst +++ b/components/expression_language/syntax.rst @@ -244,6 +244,26 @@ Numeric Operators * ``..`` (range) +For example:: + + class User + { + public $age; + } + + $user = new User(); + $user->age = 34; + + $language->evaluate( + 'user.age in 18..45', + array( + 'user' => $user, + ) + ); + +This will evaluate to ``true``, because ``user.age`` is in the range from +``18`` till ``45`` + Ternary Operators ~~~~~~~~~~~~~~~~~ From 0725a8b95a5d9aa7ef38b074f5a16e2cfff74fbd Mon Sep 17 00:00:00 2001 From: Wouter J Date: Fri, 29 Nov 2013 14:19:43 +0100 Subject: [PATCH 3/4] Added docs for using functions --- components/expression_language/syntax.rst | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/components/expression_language/syntax.rst b/components/expression_language/syntax.rst index 38e4739e0fa..5c4d756f107 100644 --- a/components/expression_language/syntax.rst +++ b/components/expression_language/syntax.rst @@ -81,6 +81,24 @@ JavaScript:: This will print ``Hi Hi Hi!``. +.. _component-expression-functions: + +Working with Functions +---------------------- + +You can also use registered functions in the expression by using the same +syntax as PHP and JavaScript. The ExpressionLanguage component comes with one +function by default: ``constant()`` Which will return the value of the PHP +constant:: + + define('DB_USER', 'root'); + + echo $language->evaluate( + 'constant("DB_USER")' + ); + +This will print ``root``. + .. _component-expression-arrays: Working with Arrays From 6e2e5836c483240a97b9db3364e4cdeea055d425 Mon Sep 17 00:00:00 2001 From: Wouter J Date: Fri, 29 Nov 2013 14:20:46 +0100 Subject: [PATCH 4/4] Added article about custom functions --- components/expression_language/extending.rst | 87 ++++++++++++++++++++ components/expression_language/index.rst | 1 + components/expression_language/syntax.rst | 5 ++ components/map.rst.inc | 1 + 4 files changed, 94 insertions(+) create mode 100644 components/expression_language/extending.rst diff --git a/components/expression_language/extending.rst b/components/expression_language/extending.rst new file mode 100644 index 00000000000..ddcc44d476f --- /dev/null +++ b/components/expression_language/extending.rst @@ -0,0 +1,87 @@ +.. index:: + single: Extending; ExpressionLanguage + +Extending the ExpressionLanguage +================================ + +The ExpressionLanguage can be extended by adding custom functions. For +instance, in the framework, the security has custom functions to check the +user's role. + +.. note:: + + If you want to learn how to use functions in an expression, read + ":ref:`component-expression-functions`". + +Register Functions +------------------ + +Functions will be registered on the current ``ExpressionLanguage`` instance. +That means the functions can be used in any expression executed by that +instance. + +To register a function, use +:method:`Symfony\\Component\\ExpressionLanguage\\ExpressionLanguage::register``. +This method has 3 arguments: + +* **name** - The name of the function in an expression; +* **compiler** - A function executed when compiling an expression using the + function; +* **evaluator** - A function executed when the expression is evaluated. + +.. code-block:: php + + use Symfony\Component\ExpressionLanguage\ExpressionLanguage; + + $language = new ExpressionLanguage(); + $language->register('lowercase', function ($str) { + if (!is_string($str)) { + return $str; + } + + return sprintf('strtolower(%s)', $str); + }, function ($str) { + if (!is_string($str)) { + return $str; + } + + return strtolower($str); + }); + + echo $language->evaluate('lowercase("HELLO")'); + +This will print ``hello``. + +Creating a new ExpressionLanguage class +--------------------------------------- + +When you use the ``ExpressionLanguage`` class in your library, it's recommend +to create a new ``ExpressionLanguage`` class and register the functions there. +The class will execute ``registerFunctions`` to register the default +functions, you can override this to also add your own functions:: + + namespace Acme\AwesomeLib\ExpressionLanguage; + + use Symfony\Component\ExpressionLanguage\ExpressionLanguage as BaseExpressionLanguage; + + class ExpressionLanguage extends BaseExpressionLanguage + { + protected function registerFunctions() + { + parent::registerFunctions(); // do not forget to also register core functions + + $this->register('lowercase', function ($str) { + if (!is_string($str)) { + return $str; + } + + return sprintf('strtolower(%s)', $str); + }, function ($str) { + if (!is_string($str)) { + return $str; + } + + return strtolower($str); + }); + } + } diff --git a/components/expression_language/index.rst b/components/expression_language/index.rst index e6eb657b4be..5ae40be979f 100644 --- a/components/expression_language/index.rst +++ b/components/expression_language/index.rst @@ -6,3 +6,4 @@ Expression Language introduction syntax + extending diff --git a/components/expression_language/syntax.rst b/components/expression_language/syntax.rst index 5c4d756f107..e105e82bdb8 100644 --- a/components/expression_language/syntax.rst +++ b/components/expression_language/syntax.rst @@ -99,6 +99,11 @@ constant:: This will print ``root``. +.. tip:: + + To read how to register your own function to use in an expression, see + ":doc:`/components/expression_language/extending`". + .. _component-expression-arrays: Working with Arrays diff --git a/components/map.rst.inc b/components/map.rst.inc index 5ec6d81f91e..2703a6057fc 100644 --- a/components/map.rst.inc +++ b/components/map.rst.inc @@ -61,6 +61,7 @@ * :doc:`/components/expression_language/introduction` * :doc:`/components/expression_language/syntax` + * :doc:`/components/expression_language/extending` * **Filesystem**