-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Keys must be NameExpressions, values must be string constants.
- Loading branch information
Showing
2 changed files
with
99 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,20 +4,46 @@ | |
|
||
use Twig\Compiler; | ||
use Twig\Node\Expression\ArrayExpression; | ||
use Twig\Node\Expression\ConstantExpression; | ||
use Twig\Node\Expression\NameExpression; | ||
|
||
/** | ||
* Represents a types node. | ||
* | ||
* @author Jeroen Versteeg <[email protected]> | ||
* @see https://github.com/twigphp/Twig/issues/4165 | ||
* @see https://github.com/twigphp/Twig/issues/4165 | ||
*/ | ||
class TypesNode extends Node implements NodeCaptureInterface | ||
{ | ||
public function __construct(ArrayExpression $typesNode, int $lineno, ?string $tag = null) | ||
{ | ||
$this->validateMapping($typesNode); | ||
|
||
parent::__construct(['mapping' => $typesNode], [], $lineno, $tag); | ||
} | ||
|
||
protected function validateMapping(ArrayExpression $typesNode): void | ||
{ | ||
foreach ($typesNode->getKeyValuePairs() as $i => $pair) { | ||
$keyExpression = $pair['key']; | ||
$valueExpression = $pair['value']; | ||
|
||
if (!$keyExpression instanceof NameExpression) { | ||
throw new \InvalidArgumentException("Key at index $i is not a NameExpression"); | ||
} | ||
$name = $keyExpression->getAttribute('name'); | ||
|
||
if (!$valueExpression instanceof ConstantExpression) { | ||
throw new \InvalidArgumentException("Value for key \"$name\" is not a ConstantExpression"); | ||
} | ||
$value = $valueExpression->getAttribute('value'); | ||
|
||
if (!is_string($value)) { | ||
throw new \InvalidArgumentException("Value for key \"$name\" is not a string"); | ||
} | ||
} | ||
} | ||
|
||
public function compile(Compiler $compiler) | ||
{ | ||
// Don't compile anything. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters