diff --git a/src/MapperBuilder.php b/src/MapperBuilder.php index 6ff7e1fc..30c191fa 100644 --- a/src/MapperBuilder.php +++ b/src/MapperBuilder.php @@ -12,6 +12,7 @@ use Psr\SimpleCache\CacheInterface; use Throwable; +use function array_unique; use function is_callable; /** @api */ @@ -235,11 +236,24 @@ public function registerConstructor(callable|string ...$constructors): self public function supportDateFormats(string $format, string ...$formats): self { $clone = clone $this; - $clone->settings->supportedDateFormats = [$format, ...$formats]; + $clone->settings->supportedDateFormats = array_unique([$format, ...$formats]); return $clone; } + /** + * Returns the date formats supported during mapping. + * + * By default, any valid timestamp or ATOM-formatted value are accepted. + * Custom formats can be set using method `supportDateFormats()`. + * + * @return non-empty-array + */ + public function supportedDateFormats(): array + { + return $this->settings->supportedDateFormats; + } + /** * Inject a cache implementation that will be in charge of caching heavy * data used by the mapper. diff --git a/tests/Unit/MapperBuilderTest.php b/tests/Unit/MapperBuilderTest.php index 85744784..bd15c959 100644 --- a/tests/Unit/MapperBuilderTest.php +++ b/tests/Unit/MapperBuilderTest.php @@ -51,4 +51,32 @@ public function test_mapper_instance_is_the_same(): void { self::assertSame($this->mapperBuilder->mapper(), $this->mapperBuilder->mapper()); } + + public function test_get_supported_date_formats_returns_defaults_formats_when_not_overridden(): void + { + self::assertSame([DATE_ATOM, 'U'], $this->mapperBuilder->supportedDateFormats()); + } + + public function test_get_supported_date_formats_returns_configured_values(): void + { + $mapperBuilder = $this->mapperBuilder->supportDateFormats('Y-m-d', 'd/m/Y'); + + self::assertSame(['Y-m-d', 'd/m/Y'], $mapperBuilder->supportedDateFormats()); + } + + public function test_get_supported_date_formats_returns_last_values(): void + { + $mapperBuilder = $this->mapperBuilder + ->supportDateFormats('Y-m-d') + ->supportDateFormats('d/m/Y'); + + self::assertSame(['d/m/Y'], $mapperBuilder->supportedDateFormats()); + } + + public function test_supported_date_formats_are_unique(): void + { + $mapperBuilder = $this->mapperBuilder->supportDateFormats('Y-m-d', 'd/m/Y', 'Y-m-d'); + + self::assertSame(['Y-m-d', 'd/m/Y'], $mapperBuilder->supportedDateFormats()); + } }