-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce unsealed shaped array syntax
This syntax enables an extension of the shaped array type by allowing additional values that must respect a certain type. ```php $mapper = (new \CuyZ\Valinor\MapperBuilder())->mapper(); // Default syntax can be used like this: $mapper->map( 'array{foo: string, ...array<string>}', [ 'foo' => 'foo', 'bar' => 'bar', // ✅ valid additional value ] ); $mapper->map( 'array{foo: string, ...array<string>}', [ 'foo' => 'foo', 'bar' => 1337, // ❌ invalid value 1337 ] ); // Key type can be added as well: $mapper->map( 'array{foo: string, ...array<int, string>}', [ 'foo' => 'foo', 42 => 'bar', // ✅ valid additional key ] ); $mapper->map( 'array{foo: string, ...array<int, string>}', [ 'foo' => 'foo', 'bar' => 'bar' // ❌ invalid key ] ); // Advanced types can be used: $mapper->map( "array{ 'en_US': non-empty-string, ...array<non-empty-string, non-empty-string> }", [ 'en_US' => 'Hello', 'fr_FR' => 'Salut', // ✅ valid additional value ] ); $mapper->map( "array{ 'en_US': non-empty-string, ...array<non-empty-string, non-empty-string> }", [ 'en_US' => 'Hello', 'fr_FR' => '', // ❌ invalid value ] ); // If the permissive type is enabled, the following will work: (new \CuyZ\Valinor\MapperBuilder()) ->allowPermissiveTypes() ->mapper() ->map( 'array{foo: string, ...}', ['foo' => 'foo', 'bar' => 'bar', 42 => 1337] ); // ✅ ```
- Loading branch information
Showing
19 changed files
with
560 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/Type/Parser/Exception/Iterable/ShapedArrayInvalidUnsealedType.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Type\Parser\Exception\Iterable; | ||
|
||
use CuyZ\Valinor\Type\Parser\Exception\InvalidType; | ||
use CuyZ\Valinor\Type\Type; | ||
use CuyZ\Valinor\Type\Types\ShapedArrayElement; | ||
use RuntimeException; | ||
|
||
use function implode; | ||
|
||
/** @internal */ | ||
final class ShapedArrayInvalidUnsealedType extends RuntimeException implements InvalidType | ||
{ | ||
/** | ||
* @param ShapedArrayElement[] $elements | ||
*/ | ||
public function __construct(array $elements, Type $unsealedType) | ||
{ | ||
$signature = 'array{'; | ||
$signature .= implode(', ', array_map(fn (ShapedArrayElement $element) => $element->toString(), $elements)); | ||
$signature .= ', ...' . $unsealedType->toString(); | ||
$signature .= '}'; | ||
|
||
parent::__construct( | ||
"Invalid unsealed type `{$unsealedType->toString()}` in shaped array signature `$signature`, it should be a valid array.", | ||
1711618899, | ||
); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/Type/Parser/Exception/Iterable/ShapedArrayUnexpectedTokenAfterSealedType.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Type\Parser\Exception\Iterable; | ||
|
||
use CuyZ\Valinor\Type\Parser\Exception\InvalidType; | ||
use CuyZ\Valinor\Type\Parser\Lexer\Token\Token; | ||
use CuyZ\Valinor\Type\Type; | ||
use CuyZ\Valinor\Type\Types\ShapedArrayElement; | ||
use RuntimeException; | ||
|
||
use function implode; | ||
|
||
/** @internal */ | ||
final class ShapedArrayUnexpectedTokenAfterSealedType extends RuntimeException implements InvalidType | ||
{ | ||
/** | ||
* @param array<ShapedArrayElement> $elements | ||
* @param list<Token> $unexpectedTokens | ||
*/ | ||
public function __construct(array $elements, Type $unsealedType, array $unexpectedTokens) | ||
{ | ||
$unexpected = implode('', array_map(fn (Token $token) => $token->symbol(), $unexpectedTokens)); | ||
|
||
$signature = 'array{'; | ||
$signature .= implode(', ', array_map(fn (ShapedArrayElement $element) => $element->toString(), $elements)); | ||
$signature .= ', ...' . $unsealedType->toString(); | ||
$signature .= $unexpected; | ||
|
||
parent::__construct( | ||
"Unexpected `$unexpected` after sealed type in shaped array signature `$signature`, expected a `}`.", | ||
1711618958, | ||
); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Type/Parser/Exception/Iterable/ShapedArrayWithoutElementsWithSealedType.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Type\Parser\Exception\Iterable; | ||
|
||
use CuyZ\Valinor\Type\Parser\Exception\InvalidType; | ||
use CuyZ\Valinor\Type\Type; | ||
use RuntimeException; | ||
|
||
/** @internal */ | ||
final class ShapedArrayWithoutElementsWithSealedType extends RuntimeException implements InvalidType | ||
{ | ||
public function __construct(Type $unsealedType) | ||
{ | ||
$signature = "array{...{$unsealedType->toString()}}"; | ||
|
||
parent::__construct( | ||
"Missing elements in shaped array signature `$signature`.", | ||
1711629845, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Type\Parser\Lexer\Token; | ||
|
||
use CuyZ\Valinor\Utility\IsSingleton; | ||
|
||
/** @internal */ | ||
final class TripleDotsToken implements Token | ||
{ | ||
use IsSingleton; | ||
|
||
public function symbol(): string | ||
{ | ||
return '...'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.