diff --git a/src/Type/CHANGELOG.md b/src/Type/CHANGELOG.md index 2d5372d4..01d03f4f 100644 --- a/src/Type/CHANGELOG.md +++ b/src/Type/CHANGELOG.md @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.4.4] + +### Added + +- Add `IdentityTypeResolver` — a type resolver that resolves to the passed type. +- Add `TypeResolvers` — a composite for type resolvers. + ## [0.4.3] 2024-08-06 ### Added diff --git a/src/Type/Visitor/IdentityTypeResolver.php b/src/Type/Visitor/IdentityTypeResolver.php new file mode 100644 index 00000000..b3ca19a6 --- /dev/null +++ b/src/Type/Visitor/IdentityTypeResolver.php @@ -0,0 +1,19 @@ + + */ +final class IdentityTypeResolver extends DefaultTypeVisitor +{ + protected function default(Type $type): Type + { + return $type; + } +} diff --git a/src/Type/Visitor/TypeResolvers.php b/src/Type/Visitor/TypeResolvers.php new file mode 100644 index 00000000..1e6ee317 --- /dev/null +++ b/src/Type/Visitor/TypeResolvers.php @@ -0,0 +1,44 @@ + + */ +final class TypeResolvers extends DefaultTypeVisitor +{ + /** + * @param list> $resolvers + * @return TypeVisitor + */ + 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> $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; + } +}