-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Type\Parser\Exception\Enum; | ||
|
||
use CuyZ\Valinor\Type\Parser\Exception\InvalidType; | ||
use RuntimeException; | ||
|
||
/** @internal */ | ||
final class NotBackedEnum extends RuntimeException implements InvalidType | ||
{ | ||
public function __construct(string $enumName) | ||
{ | ||
parent::__construct( | ||
"`$enumName` is not BackedEnum.", | ||
1653468439 | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Type\Parser\Exception\Magic; | ||
|
||
use CuyZ\Valinor\Type\Parser\Exception\InvalidType; | ||
use RuntimeException; | ||
|
||
/** @internal */ | ||
final class ClosingBracketMissing extends RuntimeException implements InvalidType | ||
{ | ||
public function __construct(string $symbol) | ||
{ | ||
parent::__construct( | ||
"The closing bracket is missing for `$symbol<...>`.", | ||
1618994728 | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Type\Parser\Exception\Magic; | ||
|
||
use CuyZ\Valinor\Type\Parser\Exception\InvalidType; | ||
use CuyZ\Valinor\Type\Type; | ||
use RuntimeException; | ||
|
||
/** @internal */ | ||
final class NonArrayOf extends RuntimeException implements InvalidType | ||
{ | ||
public function __construct(string $symbol, Type $type) | ||
{ | ||
parent::__construct( | ||
"The type inside of `$symbol<{$type->toString()}>` is not an array.", | ||
1618994728 | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Type\Parser\Exception\Magic; | ||
|
||
use CuyZ\Valinor\Type\Parser\Exception\InvalidType; | ||
use RuntimeException; | ||
|
||
/** @internal */ | ||
final class OpeningBracketMissing extends RuntimeException implements InvalidType | ||
{ | ||
public function __construct(string $symbol) | ||
{ | ||
parent::__construct( | ||
"The opening bracket is missing for `$symbol<...>`.", | ||
1618994728 | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Type\Parser\Lexer\Token; | ||
|
||
use BackedEnum; | ||
use CuyZ\Valinor\Type\Parser\Exception\Enum\NotBackedEnum; | ||
use CuyZ\Valinor\Type\Parser\Exception\Magic\ClosingBracketMissing; | ||
use CuyZ\Valinor\Type\Parser\Exception\Magic\OpeningBracketMissing; | ||
use CuyZ\Valinor\Type\Parser\Lexer\TokenStream; | ||
use CuyZ\Valinor\Type\Type; | ||
use CuyZ\Valinor\Type\Types\EnumType; | ||
use CuyZ\Valinor\Type\Types\IntegerValueType; | ||
use CuyZ\Valinor\Type\Types\StringValueType; | ||
use CuyZ\Valinor\Type\Types\UnionType; | ||
use CuyZ\Valinor\Utility\IsSingleton; | ||
|
||
/** @internal */ | ||
final class ValueOfToken implements TraversingToken | ||
{ | ||
use IsSingleton; | ||
|
||
public function traverse(TokenStream $stream): Type | ||
{ | ||
if ($stream->done() || !$stream->forward() instanceof OpeningBracketToken) { | ||
throw new OpeningBracketMissing($this->symbol()); | ||
} | ||
|
||
$subType = $stream->read(); | ||
|
||
if ($stream->done() || !$stream->forward() instanceof ClosingBracketToken) { | ||
Check warning on line 32 in src/Type/Parser/Lexer/Token/ValueOfToken.php GitHub Actions / Mutation tests
Check warning on line 32 in src/Type/Parser/Lexer/Token/ValueOfToken.php GitHub Actions / Mutation tests
|
||
throw new ClosingBracketMissing($this->symbol()); | ||
} | ||
|
||
if ($subType instanceof UnionType && count($subType->types()) === 1) { | ||
Check warning on line 36 in src/Type/Parser/Lexer/Token/ValueOfToken.php GitHub Actions / Mutation tests
Check warning on line 36 in src/Type/Parser/Lexer/Token/ValueOfToken.php GitHub Actions / Mutation tests
Check warning on line 36 in src/Type/Parser/Lexer/Token/ValueOfToken.php GitHub Actions / Mutation tests
|
||
$subType = $subType->types()[0]; | ||
} | ||
|
||
if (! $subType instanceof EnumType) { | ||
Check warning on line 40 in src/Type/Parser/Lexer/Token/ValueOfToken.php GitHub Actions / Mutation tests
|
||
throw new NotBackedEnum($subType->toString()); | ||
} | ||
|
||
$list = []; | ||
foreach ($subType->cases() as $case) { | ||
if (! $case instanceof BackedEnum) { | ||
Check warning on line 46 in src/Type/Parser/Lexer/Token/ValueOfToken.php GitHub Actions / Mutation tests
|
||
throw new NotBackedEnum($this->symbol()); | ||
} | ||
if (is_string($case->value)) { | ||
$list[] = new StringValueType("'$case->value'"); | ||
} else { | ||
$list[] = new IntegerValueType($case->value); | ||
} | ||
} | ||
|
||
return new UnionType(...$list); | ||
} | ||
|
||
public function symbol(): string | ||
{ | ||
return 'value-of'; | ||
} | ||
} |