-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add IdentityTypeResolver and TypeResolvers composite
- Loading branch information
Showing
3 changed files
with
70 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Typhoon\Type\Visitor; | ||
|
||
use Typhoon\Type\Type; | ||
|
||
/** | ||
* @api | ||
* @extends DefaultTypeVisitor<Type> | ||
*/ | ||
final class IdentityTypeResolver extends DefaultTypeVisitor | ||
{ | ||
protected function default(Type $type): Type | ||
{ | ||
return $type; | ||
} | ||
} |
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,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Typhoon\Type\Visitor; | ||
|
||
use Typhoon\Type\Type; | ||
use Typhoon\Type\TypeVisitor; | ||
|
||
/** | ||
* @api | ||
* @extends DefaultTypeVisitor<Type> | ||
*/ | ||
final class TypeResolvers extends DefaultTypeVisitor | ||
{ | ||
/** | ||
* @param list<TypeVisitor<Type>> $resolvers | ||
* @return TypeVisitor<Type> | ||
*/ | ||
public static function from(array $resolvers): TypeVisitor | ||
{ | ||
return match (\count($resolvers)) { | ||
0 => new IdentityTypeResolver(), | ||
1 => $resolvers[0], | ||
default => new self($resolvers), | ||
}; | ||
} | ||
|
||
/** | ||
* @param non-empty-list<TypeVisitor<Type>> $resolvers | ||
*/ | ||
private function __construct( | ||
private readonly array $resolvers, | ||
) {} | ||
|
||
protected function default(Type $type): mixed | ||
{ | ||
foreach ($this->resolvers as $resolver) { | ||
$type = $type->accept($resolver); | ||
} | ||
|
||
return $type; | ||
} | ||
} |