42 lines
1.2 KiB
PHP
42 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace Picea\ControlStructure;
|
|
|
|
class ForeachToken implements ControlStructure {
|
|
|
|
public array $token = [ "foreach", "endforeach" ];
|
|
|
|
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
|
switch($token) {
|
|
case "foreach":
|
|
$name = "$".uniqid("foreach_");
|
|
|
|
$count = count($context->iterationStack ?? []);
|
|
|
|
if ( $count > 0 ) {
|
|
$name .= "[" . $context->iterationStack[$count - 1]['uid'] . "]";
|
|
}
|
|
|
|
$context->iterationStack[] = [
|
|
'or' => false,
|
|
'uid' => $name,
|
|
'token' => 'endforeach',
|
|
];
|
|
|
|
return "<?php foreach ($arguments): $name = ( $name ?? 0 ) + 1; ; ?>";
|
|
|
|
case "endforeach":
|
|
if ( end($context->iterationStack)['or'] === false ) {
|
|
$output = "<?php endforeach; ?>";
|
|
}
|
|
else {
|
|
$output = "<?php endif; ?>";
|
|
}
|
|
|
|
array_pop($context->iterationStack);
|
|
|
|
return $output;
|
|
}
|
|
}
|
|
}
|