1 | <?php |
||
11 | class SyntaxCheckNodeVisitor extends \Twig_BaseNodeVisitor |
||
12 | { |
||
13 | /** |
||
14 | * @var array |
||
15 | */ |
||
16 | protected $path = []; |
||
17 | |||
18 | /** |
||
19 | * {@inheritdoc} |
||
20 | */ |
||
21 | 49 | public function getPriority() |
|
25 | |||
26 | /** |
||
27 | * {@inheritdoc} |
||
28 | * |
||
29 | * @throws \Twig_Error_Syntax |
||
30 | */ |
||
31 | 49 | protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env) |
|
49 | |||
50 | /** |
||
51 | * {@inheritdoc} |
||
52 | */ |
||
53 | 41 | protected function doLeaveNode(\Twig_Node $node, \ Twig_Environment $env) |
|
59 | |||
60 | /** |
||
61 | * @param \Twig_Node $node |
||
62 | * |
||
63 | * @throws \Twig_Error_Syntax |
||
64 | */ |
||
65 | 49 | private function checkAllowedChildren(\Twig_Node $node) |
|
66 | { |
||
67 | 49 | $hasDocumentNode = false; |
|
68 | 49 | $hasTextNode = false; |
|
69 | |||
70 | /** |
||
71 | * @var \Twig_Node $currentNode |
||
72 | */ |
||
73 | 49 | foreach ($node->getIterator() as $currentNode) { |
|
74 | 49 | if ($currentNode instanceof \Twig_Node_Text) { |
|
75 | 43 | if ($hasDocumentNode) { |
|
76 | 4 | throw new \Twig_Error_Syntax(sprintf('Node "%s" is not allowed after Node "%s".', \Twig_Node_Text::class, DocumentNode::class)); |
|
77 | } |
||
78 | 39 | $hasTextNode = true; |
|
79 | 49 | } elseif ($currentNode instanceof DocumentNode) { |
|
80 | 41 | if ($hasTextNode) { |
|
81 | 4 | throw new \Twig_Error_Syntax(sprintf('Node "%s" is not allowed before Node "%s".', \Twig_Node_Text::class, DocumentNode::class)); |
|
82 | } |
||
83 | 49 | $hasDocumentNode = true; |
|
84 | } |
||
85 | } |
||
86 | 49 | } |
|
87 | |||
88 | /** |
||
89 | * @param BaseNode $node |
||
90 | * |
||
91 | * @throws \Twig_Error_Syntax |
||
92 | */ |
||
93 | 40 | private function checkAllowedParents(BaseNode $node) |
|
119 | } |
||
120 |