JFIF x x C C " } !1AQa "q2#BR$3br %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz w !1AQ aq"2B #3Rbr{
File "MatchNode.php"
Full Path: /home/palsarh/web/palsarh.in/public_html/vendor/cuyz/valinor/src/Compiler/Native/MatchNode.php
File size: 1.55 KB
MIME-type: text/x-php
Charset: utf-8
<?php
declare(strict_types=1);
namespace CuyZ\Valinor\Compiler\Native;
use CuyZ\Valinor\Compiler\Compiler;
use CuyZ\Valinor\Compiler\Node;
/** @internal */
final class MatchNode extends Node
{
/** @var list<array{condition: Node, body: Node}> */
private array $cases = [];
private Node $defaultCase;
public function __construct(private Node $value) {}
public function withCase(Node $condition, Node $body): self
{
$self = clone $this;
$self->cases[] = ['condition' => $condition, 'body' => $body];
return $self;
}
public function withDefaultCase(Node $defaultCase): self
{
$self = clone $this;
$self->defaultCase = $defaultCase;
return $self;
}
public function compile(Compiler $compiler): Compiler
{
$value = $compiler->sub()->compile($this->value)->code();
$body = [];
foreach ($this->cases as $case) {
$body[] = $compiler->sub()->indent()->compile($case['condition'])->code() .
' => ' .
$compiler->sub()->compile($case['body'])->code() .
',';
}
if (isset($this->defaultCase)) {
$body[] = $compiler->sub()->indent()->write('default')->code() .
' => ' .
$compiler->sub()->compile($this->defaultCase)->code() .
',';
}
$body = implode("\n", $body);
return $compiler->write(
<<<PHP
match ($value) {
$body
}
PHP,
);
}
}