mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-04 22:47:21 +00:00
Add StringCastUnary
This commit is contained in:
@@ -12,6 +12,7 @@
|
|||||||
namespace Twig\Node\Expression;
|
namespace Twig\Node\Expression;
|
||||||
|
|
||||||
use Twig\Compiler;
|
use Twig\Compiler;
|
||||||
|
use Twig\Node\Expression\Unary\StringCastUnary;
|
||||||
|
|
||||||
class ArrayExpression extends AbstractExpression
|
class ArrayExpression extends AbstractExpression
|
||||||
{
|
{
|
||||||
@@ -99,7 +100,7 @@ class ArrayExpression extends AbstractExpression
|
|||||||
} else {
|
} else {
|
||||||
$key = $pair['key'] instanceof ConstantExpression ? $pair['key']->getAttribute('value') : null;
|
$key = $pair['key'] instanceof ConstantExpression ? $pair['key']->getAttribute('value') : null;
|
||||||
if ($pair['key'] instanceof NameExpression) {
|
if ($pair['key'] instanceof NameExpression) {
|
||||||
$pair['key']->setAttribute('stringify', true);
|
$pair['key'] = new StringCastUnary($pair['key'], $pair['key']->getTemplateLine());
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($nextIndex !== $key) {
|
if ($nextIndex !== $key) {
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ class NameExpression extends AbstractExpression
|
|||||||
|
|
||||||
public function __construct(string $name, int $lineno)
|
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
|
public function compile(Compiler $compiler): void
|
||||||
@@ -54,9 +54,6 @@ class NameExpression extends AbstractExpression
|
|||||||
} elseif (isset($this->specialVars[$name])) {
|
} elseif (isset($this->specialVars[$name])) {
|
||||||
$compiler->raw($this->specialVars[$name]);
|
$compiler->raw($this->specialVars[$name]);
|
||||||
} elseif ($this->getAttribute('always_defined')) {
|
} elseif ($this->getAttribute('always_defined')) {
|
||||||
if ($this->getAttribute('stringify')) {
|
|
||||||
$compiler->raw(' (string)');
|
|
||||||
}
|
|
||||||
$compiler
|
$compiler
|
||||||
->raw('$context[')
|
->raw('$context[')
|
||||||
->string($name)
|
->string($name)
|
||||||
@@ -64,9 +61,6 @@ class NameExpression extends AbstractExpression
|
|||||||
;
|
;
|
||||||
} else {
|
} else {
|
||||||
if ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables()) {
|
if ($this->getAttribute('ignore_strict_check') || !$compiler->getEnvironment()->isStrictVariables()) {
|
||||||
if ($this->getAttribute('stringify')) {
|
|
||||||
$compiler->raw(' (string)');
|
|
||||||
}
|
|
||||||
$compiler
|
$compiler
|
||||||
->raw('($context[')
|
->raw('($context[')
|
||||||
->string($name)
|
->string($name)
|
||||||
@@ -80,9 +74,6 @@ class NameExpression extends AbstractExpression
|
|||||||
->string($name)
|
->string($name)
|
||||||
->raw(', $context) ?')
|
->raw(', $context) ?')
|
||||||
;
|
;
|
||||||
if ($this->getAttribute('stringify')) {
|
|
||||||
$compiler->raw(' (string)');
|
|
||||||
}
|
|
||||||
$compiler
|
$compiler
|
||||||
->raw(' $context[')
|
->raw(' $context[')
|
||||||
->string($name)
|
->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