registerProvider example

function DExpressionLanguage $el) {
                    $el->register('fn', function D) {}function D) {});
                },
            ],
            [
                function DExpressionLanguage $el) {
                    $el->addFunction(new ExpressionFunction('fn', function D) {}function D) {}));
                },
            ],
            [
                function DExpressionLanguage $el) {
                    $el->registerProvider(new TestProvider());
                },
            ],
        ];
    }
}
'APP_ENV' => 'test',
                'PHP_VERSION' => '7.2',
            ][$arg] ?? null,
            'sum' => fn () => fn ($a$b) => $a + $b,
            'foo' => fn () => fn () => 'bar',
        ]);

        $this->context = new RequestContext();
        $this->context->setParameter('_functions', $functionProvider);

        $this->expressionLanguage = new ExpressionLanguage();
        $this->expressionLanguage->registerProvider(new ExpressionLanguageProvider($functionProvider));
    }

    /** * @dataProvider compileProvider */
    public function testCompile(string $expression, string $expected)
    {
        $this->assertSame($expected$this->expressionLanguage->compile($expression));
    }

    public static function compileProvider(): iterable
    {
protected array $functions = [];

    /** * @param ExpressionFunctionProviderInterface[] $providers */
    public function __construct(CacheItemPoolInterface $cache = null, array $providers = [])
    {
        $this->cache = $cache ?? new ArrayAdapter();
        $this->registerFunctions();
        foreach ($providers as $provider) {
            $this->registerProvider($provider);
        }
    }

    /** * Compiles an expression source code. */
    public function compile(Expression|string $expression, array $names = []): string
    {
        return $this->getCompiler()->compile($this->parse($expression$names)->getNodes())->getSource();
    }

    
$this->validator->validate($object$constraint);

        $this->assertCount(2, $this->context->getViolations());
    }

    /** * @dataProvider provideCompileIsValid */
    public function testCompileIsValid(string $expression, array $names, string $expected)
    {
        $expressionLanguage = new ExpressionLanguage();
        $expressionLanguage->registerProvider(new ExpressionLanguageProvider());

        $result = $expressionLanguage->compile($expression$names);

        $this->assertSame($expected$result);
    }

    public static function provideCompileIsValid(): array
    {
        return [
            [
                'is_valid("foo", constraints)',
                [
$this->context->buildViolation($constraint->message)
                ->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING))
                ->setCode(Expression::EXPRESSION_FAILED_ERROR)
                ->addViolation();
        }
    }

    private function getExpressionLanguage(): ExpressionLanguage
    {
        if (!isset($this->expressionLanguage)) {
            $this->expressionLanguage = new ExpressionLanguage();
            $this->expressionLanguage->registerProvider(new ExpressionLanguageProvider());
        }

        return $this->expressionLanguage;
    }
}
Home | Imprint | This part of the site doesn't use cookies.