diff --git a/docs/pages/other/dealing-with-dates.md b/docs/pages/other/dealing-with-dates.md index c3a9f1c..c455800 100644 --- a/docs/pages/other/dealing-with-dates.md +++ b/docs/pages/other/dealing-with-dates.md @@ -5,14 +5,12 @@ supported. By default, any valid timestamp or ATOM-formatted value will be accepted. If other formats are to be supported, they need to be registered using the -following constructor: +following method: ```php (new \CuyZ\Valinor\MapperBuilder()) // Both `Cookie` and `ATOM` formats will be accepted - ->registerConstructor( - new \CuyZ\Valinor\Mapper\Object\DateTimeFormatConstructor(DATE_COOKIE, DATE_ATOM) - ) + ->supportDateFormats(DATE_COOKIE, DATE_ATOM) ->mapper() ->map(DateTimeInterface::class, 'Monday, 08-Nov-1971 13:37:42 UTC'); ``` diff --git a/src/Mapper/Object/DateTimeFormatConstructor.php b/src/Mapper/Object/DateTimeFormatConstructor.php index 3d9d1e3..28a2235 100644 --- a/src/Mapper/Object/DateTimeFormatConstructor.php +++ b/src/Mapper/Object/DateTimeFormatConstructor.php @@ -26,7 +26,7 @@ use DateTimeInterface; * ->map(DateTimeInterface::class, 'Monday, 08-Nov-1971 13:37:42 UTC'); * ``` * - * @api + * @internal */ final class DateTimeFormatConstructor { diff --git a/src/MapperBuilder.php b/src/MapperBuilder.php index 2b3474d..a279c1e 100644 --- a/src/MapperBuilder.php +++ b/src/MapperBuilder.php @@ -7,6 +7,7 @@ namespace CuyZ\Valinor; use CuyZ\Valinor\Cache\FileSystemCache; use CuyZ\Valinor\Library\Container; use CuyZ\Valinor\Library\Settings; +use CuyZ\Valinor\Mapper\Object\DateTimeFormatConstructor; use CuyZ\Valinor\Mapper\Tree\Message\ErrorMessage; use CuyZ\Valinor\Mapper\TreeMapper; use Psr\SimpleCache\CacheInterface; @@ -172,6 +173,28 @@ final class MapperBuilder return $clone; } + /** + * Describes which date formats will be supported during mapping. + * + * By default, the dates will accept any valid timestamp or ATOM-formatted + * value. + * + * ```php + * (new \CuyZ\Valinor\MapperBuilder()) + * // Both `Cookie` and `ATOM` formats will be accepted + * ->supportDateFormats(DATE_COOKIE, DATE_ATOM) + * ->mapper() + * ->map(DateTimeInterface::class, 'Monday, 08-Nov-1971 13:37:42 UTC'); + * ``` + * + * @param non-empty-string $format + * @param non-empty-string ...$formats + */ + public function supportDateFormats(string $format, string ...$formats): self + { + return $this->registerConstructor(new DateTimeFormatConstructor($format, ...$formats)); + } + /** * Inject a cache implementation that will be in charge of caching heavy * data used by the mapper. diff --git a/tests/Integration/Mapping/Object/DateTimeMappingTest.php b/tests/Integration/Mapping/Object/DateTimeMappingTest.php index 5206cbe..7cce646 100644 --- a/tests/Integration/Mapping/Object/DateTimeMappingTest.php +++ b/tests/Integration/Mapping/Object/DateTimeMappingTest.php @@ -5,7 +5,6 @@ declare(strict_types=1); namespace CuyZ\Valinor\Tests\Integration\Mapping\Object; use CuyZ\Valinor\Mapper\MappingError; -use CuyZ\Valinor\Mapper\Object\DateTimeFormatConstructor; use CuyZ\Valinor\MapperBuilder; use CuyZ\Valinor\Tests\Integration\IntegrationTest; use DateTimeInterface; @@ -55,7 +54,7 @@ final class DateTimeMappingTest extends IntegrationTest { try { $result = (new MapperBuilder()) - ->registerConstructor(new DateTimeFormatConstructor('d/m/Y', 'Y/m/d')) + ->supportDateFormats('d/m/Y', 'Y/m/d') ->mapper() ->map(DateTimeInterface::class, '2022/08/05'); } catch (MappingError $error) { @@ -83,7 +82,7 @@ final class DateTimeMappingTest extends IntegrationTest { try { (new MapperBuilder()) - ->registerConstructor(new DateTimeFormatConstructor('Y/m/d')) + ->supportDateFormats('Y/m/d') ->mapper() ->map(DateTimeInterface::class, 'invalid datetime'); } catch (MappingError $exception) {