Skip to content

Commit

Permalink
feat: introduce method to get date formats supported during mapping
Browse files Browse the repository at this point in the history
The new method `MapperBuilder::supportedDateFormats()` can be used to
get date formats supported during mapping.
  • Loading branch information
romm committed Jul 10, 2023
1 parent 1c70c2d commit 873961e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/MapperBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Psr\SimpleCache\CacheInterface;
use Throwable;

use function array_unique;
use function is_callable;

/** @api */
Expand Down Expand Up @@ -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<non-empty-string>
*/
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.
Expand Down
28 changes: 28 additions & 0 deletions tests/Unit/MapperBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit 873961e

Please sign in to comment.