minor #4162 Simplify usage of RawFilter (fabpot)

This PR was merged into the 3.x branch.

Discussion
----------

Simplify usage of RawFilter

Commits
-------

6e55b5f9 Simplify usage of RawFilter
This commit is contained in:
Fabien Potencier
2024-07-28 11:11:17 +02:00
2 changed files with 52 additions and 0 deletions
+14
View File
@@ -12,13 +12,27 @@
namespace Twig\Node\Expression\Filter;
use Twig\Compiler;
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\FilterExpression;
use Twig\Node\Node;
/**
* @author Fabien Potencier <fabien@symfony.com>
*/
class RawFilter extends FilterExpression
{
public function __construct(Node $node, ?ConstantExpression $filterName = null, ?Node $arguments = null, int $lineno = 0, ?string $tag = null)
{
if (null === $filterName) {
$filterName = new ConstantExpression('raw', $node->getTemplateLine());
}
if (null === $arguments) {
$arguments = new Node();
}
parent::__construct($node, $filterName, $arguments, $lineno ?: $node->getTemplateLine(), $tag ?: $node->getNodeTag());
}
public function compile(Compiler $compiler): void
{
$compiler->subcompile($this->getNode('node'));
+38
View File
@@ -0,0 +1,38 @@
<?php
namespace Twig\Tests\Node\Expression\Filter;
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use Twig\Node\Expression\ConstantExpression;
use Twig\Node\Expression\Filter\RawFilter;
use Twig\Test\NodeTestCase;
class RawTest extends NodeTestCase
{
public function testConstructor()
{
$filter = new RawFilter($node = new ConstantExpression('foo', 12));
$this->assertSame(12, $filter->getTemplateLine());
$this->assertSame('raw', $filter->getNode('filter')->getAttribute('value'));
$this->assertSame($node, $filter->getNode('node'));
$this->assertSame(0, count($filter->getNode('arguments')));
}
public function getTests()
{
$node = new RawFilter(new ConstantExpression('foo', 12));
return [
[$node, '"foo"'],
];
}
}