picea/src/ControlStructure/ForeachToken.php
Dave Mc Nicoll ce4ef6351a - Small refractoring of code, merge tokens
- Completed block token
2020-05-04 22:50:02 -04:00

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;
}
}
}