New in Symfony 6.1: Improved ExpressionLanguage Syntax

The ExpressionLanguage component provides an engine that can compile and evaluate expressions. The main purpose of this component is to use expressions inside configuration for more complex logic. Its other purpose is to use it to build business rule engines. In Symfony 6.1 we're improving the already rich ExpressionLanguage syntax with more features.

New Syntax Options for Numbers

        Contributed by Thomas Calvet
         in #44073.

In order to better align with the expressions supported in modern PHP code, you can now define numbers using the following syntax:

    # you can remove the leading zero of decimal numbers (in previous versions you had to use 0.99)

$expressionLanguage->evaluate('product.price 1_000_000', ['...' => '...']) $expressionLanguage->evaluate('2 3.14159_26535 radius', ['...' => '...'])

New Operators

        Contributed by Fabien Potencier
        and Sofien Naas
         in #45912, #45795.

First, we've added a new ?. operator which is equivalent to the PHP null-safe operator. It prevents errors when trying to access a property or method of an object that is null:

    // this will throw an exception when `fruit` is `null`

$expressionLanguage->evaluate('fruit.color', ['fruit' => '...'])

// this will return null if fruit is null $expressionLanguage->evaluate('fruit?.color', ['fruit' => '...'])

In addition to the null-safe operator, we've added three new operators (contains, starts with and ends with) that allow to create very readable expressions:

    $expressionLanguage->evaluate('coupon.code stars with "XXX"', ['...' => '...'])

$expressionLanguage->evaluate('imported_file.path ends with ".csv"', ['...' => '...']) $expressionLanguage->evaluate('url contains "example.com"', ['...' => '...'])

These operators were already available in Twig templates, making ExpressionLanguage component more in sync with Twig.

                Sponsor the Symfony project.

https://symfony.com/blog/new-in-symfony-6-1-improved-expressionlanguage-syntax?utm_source=Symfony%20Blog%20Feed&utm_medium=feed

Utworzony 3y | 11 kwi 2022, 08:20:09


Zaloguj się, aby dodać komentarz

Inne posty w tej grupie

New in Symfony 7.3: Mailer Security Improvements

The Symfony Mailer component provides many security-related features like signing and encrypting email messages. In Symfony 7.3 we're pushing those features even further to give you greater control an

9 maj 2025, 09:20:10 | Symfony
SymfonyOnline June 2025: Detect Hidden Defects: Check Your PHP Tests

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:

June 10-11: Workshop days. It is possible to attend 1 two-day training or 2 one-day trainings. June 12-13: Online confe

8 maj 2025, 14:40:19 | Symfony
New in Symfony 7.3: JsonPath Component

Contributed by Alexandre Daubois in

8 maj 2025, 10:10:10 | Symfony
SymfonyOnline June 2025: Combining Turbo, LiveComponent & Stimulus... the Right Way?

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:

June 10-11: Workshop days. It is possible to attend 1 two-day training or 2 one-day trainings. June 12-13: Online confe

7 maj 2025, 13:10:15 | Symfony
New in Symfony 7.3: New and Improved Console Helpers

Symfony 7.3 introduces powerful improvements to the Console component, beyond the much-anticipated invokable commands and input attributes. This version also brings new helpers and features designed t

7 maj 2025, 08:30:04 | Symfony
New in Symfony 7.3: Static Error Pages

Contributed by Loick Piera in

6 maj 2025, 09:20:10 | Symfony
New in Symfony 7.3: Simpler Server Event Streaming

Contributed by Yonel Ceruto in

5 maj 2025, 07:50:03 | Symfony