mirror of
https://github.com/twigphp/Twig.git
synced 2026-08-31 12:37:15 +00:00
minor #4306 Add StringCastUnary (fabpot)
This PR was merged into the 3.x branch.
Discussion
----------
Add StringCastUnary
Commits
-------
bfafa5b613 Add StringCastUnary
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
namespace Twig\Node\Expression;
|
||||
|
||||
use Twig\Compiler;
|
||||
use Twig\Node\Expression\Unary\StringCastUnary;
|
||||
|
||||
class ArrayExpression extends AbstractExpression
|
||||
{
|
||||
@@ -99,7 +100,7 @@ class ArrayExpression extends AbstractExpression
|
||||
} else {
|
||||
$key = $pair['key'] instanceof ConstantExpression ? $pair['key']->getAttribute('value') : null;
|
||||
if ($pair['key'] instanceof NameExpression) {
|
||||
$pair['key']->setAttribute('stringify', true);
|
||||
$pair['key'] = new StringCastUnary($pair['key'], $pair['key']->getTemplateLine());
|
||||
}
|
||||
|
||||
if ($nextIndex !== $key) {
|
||||
|
||||
@@ -24,7 +24,7 @@ class NameExpression extends AbstractExpression
|
||||
|
||||
public function __construct(string $name, int $lineno)
|
||||
{
|
||||
parent::__construct([], ['name' => $name, 'is_defined_test' => false, 'ignore_strict_check' => false, 'always_defined' => false, 'stringify' => false], $lineno);
|
||||
parent::__construct([], ['name' => $name, 'is_defined_test' => false, 'ignore_strict_check' => false, 'always_defined' => false], $lineno);
|
||||
}
|
||||
|
||||
public function compile(Compiler $compiler): void
|
||||
@@ -54,9 +54,6 @@ class NameExpression extends AbstractExpression
|
||||
} elseif (isset($this->specialVars[$name])) {
|
||||
$compiler->raw($this->specialVars[$name]);
|
||||
} elseif ($this->getAttribute('always_defined')) {
|
||||
if ($this->getAttribute('stringify')) {
|
||||
$compiler->raw(' (string)');
|
||||
}
|
||||
$compiler
|
||||
->raw('$context[')
|
||||
->string($name)
|
||||
@@ -64,9 +61,6 @@ class NameExpression extends AbstractExpression
|
||||
;
|
||||
} else {
|
||||
if ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables()) {
|
||||
if ($this->getAttribute('stringify')) {
|
||||
$compiler->raw(' (string)');
|
||||
}
|
||||
$compiler
|
||||
->raw('($context[')
|
||||
->string($name)
|
||||
@@ -80,9 +74,6 @@ class NameExpression extends AbstractExpression
|
||||
->string($name)
|
||||
->raw(', $context) ?')
|
||||
;
|
||||
if ($this->getAttribute('stringify')) {
|
||||
$compiler->raw(' (string)');
|
||||
}
|
||||
$compiler
|
||||
->raw(' $context[')
|
||||
->string($name)
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
namespace Twig\Node\Expression\Unary;
|
||||
|
||||
use Twig\Compiler;
|
||||
|
||||
final class StringCastUnary extends AbstractUnary
|
||||
{
|
||||
public function operator(Compiler $compiler): Compiler
|
||||
{
|
||||
return $compiler->raw('(string)');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user