8000 [WIP] Add docs for ExpressionLanguage component by wouterj · Pull Request #3044 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

[WIP] Add docs for ExpressionLanguage component #3044

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 16, 2013
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

10000
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Fixed comments and splitted in intro and syntax
  • Loading branch information
wouterj committed Oct 26, 2013
commit 8fe9fd606fdf4b0b20bf55606b9b7472b3965c83
1 change: 1 addition & 0 deletions components/expression_language/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ Expression Language
:maxdepth: 2

introduction
syntax
100 changes: 7 additions & 93 deletions components/expression_language/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ The ExpressionLanguage Component
(mostly, but not limited to, Booleans).

.. versionadded:: 2.4
The ExpressionLanguage Component was new in Symfony 2.4.
The ExpressionLanguage component was new in Symfony 2.4.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was introduced in


Installation
------------

You can install the component in 2 different ways:

* Use the official Git repository (https://github.com/symfony/ExpressionLanguage);
* Use the official Git repository (https://github.com/symfony/expression-language);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

preach it and practice 👶

* :doc:`Install it via Composer </components/using_components>` (``symfony/expression-language`` on `Packagist`_).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should document Composer first as it is the recommended way

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it, but this means we should change it in all component docs. I'll open a ticket for it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We changed the order of these recently


Usage
Expand All @@ -29,7 +29,7 @@ compare them to the expression in an ``if`` statement. A simple example of an
expression is ``1 + 2``. You can also use more complicated expressions, such
as ``someArray[3].someMethod('bar')``.

The component provide 2 ways to work with expressions:
The component provides 2 ways to work with expressions:

* **compile**: the expression is compiled to PHP, so it can be cached and
evaluated;
Expand All @@ -46,96 +46,10 @@ The main class of the component is

echo $language->compile('1 + 2'); // displays (1 + 2)

Supported Literals
~~~~~~~~~~~~~~~~~~
Expression Syntax
-----------------

The component supports:

* **strings** - single and double quotes (e.g. ``'hello'``)
* **numbers** - e.g. ``103``
* **arrays** - using twig notation (e.g. ``[1, 2]``)
* **hashes** - using twig notation (e.g. ``{ foo: 'bar' }``)
* **booleans** - ``true`` and ``false``
* **null** - ``null``

Supported Operators
~~~~~~~~~~~~~~~~~~~

The component comes with a lot of operators:

Arithmetic Operators
....................

* ``+`` (addition)
* ``-`` (subtraction)
* ``*`` (multiplication)
* ``/`` (division)
* ``%`` (modulus)
* ``**`` (pow)

Assignment Operators
....................

* ``=``

Bitwise Operators
.................

* ``&`` (and)
* ``|`` (or)
* ``^`` (xor)

Comparison Operators
....................

* ``==`` (e EDBE qual)
* ``===`` (identical)
* ``!=`` (not equal)
* ``!==`` (not identical)
* ``<`` (less than)
* ``>`` (greater than)
* ``<=`` (less than or equal to)
* ``>=`` (greater than or equal to)
* ``=~`` (regex match)
* ``!~`` (regex does not match)

.. sidebar:: Regex Operator

The Regex Operators (``=~`` and ``!~``) are coming from Perl. This
operator matches if the regular expression on the right side of the
operator matches the string on the left. For instance,
``'foobar' =~ '/foo/'`` evaluates to true.
``!~`` is the opposite and matches if the regular expression does *not*
match the string.

Logical Operators
.................

* ``not`` or ``!``
* ``and`` or ``&&``
* ``or`` or ``||``

String Operators
................

* ``~`` (concatenation)

Array Operators
...............

* ``in`` (contain)
* ``not in`` (does not contain)

Numeric Operators
.................

* ``..`` (range)

Ternary Operators
.................

* ``foo ? 'yes' : 'no'``
* ``foo ?: 'no'`` (equal to ``foo ? foo : 'no'``)
* ``foo ? 'yes'`` (equal to ``foo ? 'yes' : ''``)
See ":doc:`/components/expression_language/syntax`" to learn the syntax of the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax used with

ExpressionLanguage component.

.. _Packagist: https://packagist.org/packages/symfony/expression-language
98 changes: 98 additions & 0 deletions components/expression_language/syntax.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
.. index::
single: Syntax; ExpressionLanguage

The Expression Syntax
=====================

The ExpressionLanguage component uses a specific syntax which is based on the
expression syntax of Twig. In this document, you can find all supported
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra comma can be removed

syntaxes.

Supported Literals
~~~~~~~~~~~~~~~~~~

The component supports:

* **strings** - single and double quotes (e.g. ``'hello'``)
* **numbers** - e.g. ``103``
* **arrays** - using twig notation (e.g. ``[1, 2]``)
* **hashes** - using twig notation (e.g. ``{ foo: 'bar' }``)
* **booleans** - ``true`` and ``false``
* **null** - ``null``

Supported Operators
~~~~~~~~~~~~~~~~~~~

The component comes with a lot of operators:

Arithmetic Operators
....................

* ``+`` (addition)
* ``-`` (subtraction)
* ``*`` (multiplication)
* ``/`` (division)
* ``%`` (modulus)
* ``**`` (pow)

Assignment Operators
....................

* ``=``

Bitwise Operators
.................

* ``&`` (and)
* ``|`` (or)
* ``^`` (xor)

Comparison Operators
....................

* ``==`` (equal)
* ``===`` (identical)
* ``!=`` (not equal)
* ``!==`` (not identical)
* ``<`` (less than)
* ``>`` (greater than)
* ``<=`` (less than or equal to)
* ``>=`` (greater than or equal to)
* ``matches`` (regex match)

.. tip::

To test if a string does *not* match a regex, use the logical ``not``
operator in combination with the ``matches`` operator::

$language->evaluate('not "foo" matches "/bar/"'); // returns true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we allowed to place also () to group expressions? just curious


Logical Operators
.................

* ``not`` or ``!``
* ``and`` or ``&&``
* ``or`` or ``||``

String Operators
................

* ``~`` (concatenation)

Array Operators
...............

* ``in`` (contain)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

contains

* ``not in`` (does not contain)

Numeric Operators
.................

* ``..`` (range)

Ternary Operators
.................

* ``foo ? 'yes' : 'no'``
* ``foo ?: 'no'`` (equal to ``foo ? foo : 'no'``)
* ``foo ? 'yes'`` (equal to ``foo ? 'yes' : ''``)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this also true in twig, interesting, never knew elvis operator had another version

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as of Twig 1.12, it is supported

However, in the ExpressionLanguage component, the implicit else value is null, not the empty string

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't it be null on evaluation and '' on compilation maybe? just saying

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope. The compilation and the evaluation are producing the same result (after evaluating the compiled code).
And these shortcuts are handled by the parser, not by the evaluator or the compiler.

Using an empty string i the doc is because it was written before changing it to null (the initial code was copied from Twig which uses an empty string)

1 change: 1 addition & 0 deletions components/map.rst.inc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
* :doc:`/components/expression_language/index`

* :doc:`/components/expression_language/introduction`
* :doc:`/components/expression_language/syntax`

* **Filesystem**

Expand Down
0