Expressions
Some mapping rules may involve complex imperative expressions. For example, when using an attribute reader, you could define a rule like this:
use TypeLang\Mapper\Mapping\SkipWhen;
final class UserInfoResponse
{
public ?string $firstName;
public ?string $lastName;
// Skip normalization of this field if
// one of the [firstName, lastName] is not specified
#[SkipWhen('this.firstName == null or this.lastName == null')]
public string $fullName {
get => $this->firstName . ' ' . $this->lastName;
}
}
To enable support for such rules, an additional dependency must be defined.
composer require symfony/expression-language
By default, you don't need to do anything else to support the expression language, but in some cases you may need to extend it. To do this, you must explicitly pass the expression language instance to the MetadataBuilder metadata provider.
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use TypeLang\Mapper\Mapping\Provider\MetadataBuilder;
$expression = new ExpressionLanguage();
// ...
$provider = [[[new MetadataBuilder(|meta-provider.html]]]
expression: $expression,
);
06 November 2025