Skip to content

Commit

Permalink
Support accessing params from the expression as well
Browse files Browse the repository at this point in the history
  • Loading branch information
mpdude committed Jun 26, 2024
1 parent b89a8e7 commit d9a263f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/JMS/ObjectRouting/ObjectRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ public function generate($type, $object, $absolute = false, array $extraParams =

foreach ($route['paramExpressions'] as $k => $expression) {
if (!$expression instanceof ParsedExpression) {
$expression = $this->expressionLanguage->parse($expression, ['this']);
$expression = $this->expressionLanguage->parse($expression, ['this', 'params']);
$metadata->routes[$type]['paramExpressions'][$k] = $expression;
}
$evaluated = $this->expressionLanguage->evaluate($expression, ['this' => $object]);
$evaluated = $this->expressionLanguage->evaluate($expression, ['this' => $object, 'params' => $params]);
if ($k[0] === '?') {
if ($evaluated === null) {
continue;
Expand Down
20 changes: 20 additions & 0 deletions tests/JMS/Tests/ObjectRouting/ObjectRouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,26 @@ public function testGenerateWithParamExpression()
$this->assertEquals('/foobar', $this->router->generate('view', $object));
}

public function testGenerateWithParamExpressionThatRefersToParam()
{
$metadata = new ClassMetadata('stdClass');
$metadata->addRoute('view', 'view_name', ['foo' => 'bar'], ['concat' => 'params["foo"] ~ this.bar']);

$object = new \stdClass;
$object->bar = 'baz';

$this->factory->expects($this->once())
->method('getMetadataForClass')
->will($this->returnValue($metadata));

$this->adapter->expects($this->once())
->method('generate')
->with('view_name', array('foo' => 'baz', 'concat' => 'bazbaz'), false)
->will($this->returnValue('/foobar'));

$this->assertEquals('/foobar', $this->router->generate('view', $object));
}

public function testGenerateWithNullableParamExpression()
{
$metadata = new ClassMetadata('stdClass');
Expand Down

0 comments on commit d9a263f

Please sign in to comment.