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 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions components/expression_language/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Expression Language
===================

.. toctree::
:maxdepth: 2

introduction
141 changes: 141 additions & 0 deletions components/expression_language/introduction.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
.. index::
single: Expressions
Single: Components; Expression Language

The ExpressionLanguage Component
Copy link
Member

Choose a reason for hiding this comment

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

ExpressionLanguage or Expression Language? For example, I used Class Loader and not ClassLoader.

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 prefer ExpressionLanguage (that's also the name used in the README)

=================================

The ExpressionLanguage component provides an engine that can compile and
evaluate expressions. An expression is a one-liner that returns a value
(mostly, but not limited to, Booleans).

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

Choose a reason for hiding this comment

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

component should be lowercased


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

You can install the component in 2 different ways:

* Use the official Git repository (https://github.com/symfony/ExpressionLanguage);
* :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
-----

The ExpressionLanguage component can compile and evaluate expressions.
Expressions are one-liners which most of the time return a boolean, you can
compare them to the expression in an ``if`` statement. A simple example of an
Copy link
Contributor

Choose a reason for hiding this comment

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

you can compare them, for example, to the expression in an if statement

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:
Copy link
Contributor

Choose a reason for hiding this comment

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

It should be "provides", shouldn't 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.

Yes, indeed


* **compile**: the expression is compiled to PHP, so it can be cached and
evaluated;
* **evaluation**: the expression is evaluated without being compiled to PHP.
Copy link
Contributor

Choose a reason for hiding this comment

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

i miss a bit some text saying for what purposes or use cases (at least examples of them) is compile used over evaluation and the other way around. And also how ExpressionLanguage component is plugged into Symfony so that enables further use cases. That is something that will make things like this documentation make a lot more sense.


The main class of the component is
:class:`Symfony\\Component\\ExpressionLanguage\\ExpressionLanguage`::

use Symfony\Component\ExpressionLanguage\ExpressionLanguage;

$language = new ExpressionLanguage();

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

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

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)
* ``=~`` (regex match)
* ``!~`` (regex does not match)
Copy link
Member

Choose a reason for hiding this comment

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

this is wrong. the regex operator has been changed to matches, and the opposite operator has been removed (use not for this)

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed


.. 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)
Copy link
Member

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' : ''``)

.. _Packagist: https://packagist.org/packages/symfony/expression-language
1 change: 1 addition & 0 deletions components/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ The Components
dom_crawler
dependency_injection/index
event_dispatcher/index
expression_language/index
filesystem
finder
form/index
Expand Down
4 changes: 4 additions & 0 deletions components/map.rst.inc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
* :doc:`/components/event_dispatcher/generic_event`
* :doc:`/components/event_dispatcher/immutable_dispatcher`

* :doc:`/components/expression_language/index`

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

* **Filesystem**

* :doc:`/components/filesystem`
Expand Down