mirror of
https://github.com/danog/Valinor.git
synced 2024-11-26 20:24:40 +01:00
feat: introduce helper method to describe supported date formats
```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'); ```
This commit is contained in:
parent
f232cc0636
commit
11a7ea7252
@ -5,14 +5,12 @@ supported. By default, any valid timestamp or ATOM-formatted value will be
|
|||||||
accepted.
|
accepted.
|
||||||
|
|
||||||
If other formats are to be supported, they need to be registered using the
|
If other formats are to be supported, they need to be registered using the
|
||||||
following constructor:
|
following method:
|
||||||
|
|
||||||
```php
|
```php
|
||||||
(new \CuyZ\Valinor\MapperBuilder())
|
(new \CuyZ\Valinor\MapperBuilder())
|
||||||
// Both `Cookie` and `ATOM` formats will be accepted
|
// Both `Cookie` and `ATOM` formats will be accepted
|
||||||
->registerConstructor(
|
->supportDateFormats(DATE_COOKIE, DATE_ATOM)
|
||||||
new \CuyZ\Valinor\Mapper\Object\DateTimeFormatConstructor(DATE_COOKIE, DATE_ATOM)
|
|
||||||
)
|
|
||||||
->mapper()
|
->mapper()
|
||||||
->map(DateTimeInterface::class, 'Monday, 08-Nov-1971 13:37:42 UTC');
|
->map(DateTimeInterface::class, 'Monday, 08-Nov-1971 13:37:42 UTC');
|
||||||
```
|
```
|
||||||
|
@ -26,7 +26,7 @@ use DateTimeInterface;
|
|||||||
* ->map(DateTimeInterface::class, 'Monday, 08-Nov-1971 13:37:42 UTC');
|
* ->map(DateTimeInterface::class, 'Monday, 08-Nov-1971 13:37:42 UTC');
|
||||||
* ```
|
* ```
|
||||||
*
|
*
|
||||||
* @api
|
* @internal
|
||||||
*/
|
*/
|
||||||
final class DateTimeFormatConstructor
|
final class DateTimeFormatConstructor
|
||||||
{
|
{
|
||||||
|
@ -7,6 +7,7 @@ namespace CuyZ\Valinor;
|
|||||||
use CuyZ\Valinor\Cache\FileSystemCache;
|
use CuyZ\Valinor\Cache\FileSystemCache;
|
||||||
use CuyZ\Valinor\Library\Container;
|
use CuyZ\Valinor\Library\Container;
|
||||||
use CuyZ\Valinor\Library\Settings;
|
use CuyZ\Valinor\Library\Settings;
|
||||||
|
use CuyZ\Valinor\Mapper\Object\DateTimeFormatConstructor;
|
||||||
use CuyZ\Valinor\Mapper\Tree\Message\ErrorMessage;
|
use CuyZ\Valinor\Mapper\Tree\Message\ErrorMessage;
|
||||||
use CuyZ\Valinor\Mapper\TreeMapper;
|
use CuyZ\Valinor\Mapper\TreeMapper;
|
||||||
use Psr\SimpleCache\CacheInterface;
|
use Psr\SimpleCache\CacheInterface;
|
||||||
@ -172,6 +173,28 @@ final class MapperBuilder
|
|||||||
return $clone;
|
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
|
* Inject a cache implementation that will be in charge of caching heavy
|
||||||
* data used by the mapper.
|
* data used by the mapper.
|
||||||
|
@ -5,7 +5,6 @@ declare(strict_types=1);
|
|||||||
namespace CuyZ\Valinor\Tests\Integration\Mapping\Object;
|
namespace CuyZ\Valinor\Tests\Integration\Mapping\Object;
|
||||||
|
|
||||||
use CuyZ\Valinor\Mapper\MappingError;
|
use CuyZ\Valinor\Mapper\MappingError;
|
||||||
use CuyZ\Valinor\Mapper\Object\DateTimeFormatConstructor;
|
|
||||||
use CuyZ\Valinor\MapperBuilder;
|
use CuyZ\Valinor\MapperBuilder;
|
||||||
use CuyZ\Valinor\Tests\Integration\IntegrationTest;
|
use CuyZ\Valinor\Tests\Integration\IntegrationTest;
|
||||||
use DateTimeInterface;
|
use DateTimeInterface;
|
||||||
@ -55,7 +54,7 @@ final class DateTimeMappingTest extends IntegrationTest
|
|||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$result = (new MapperBuilder())
|
$result = (new MapperBuilder())
|
||||||
->registerConstructor(new DateTimeFormatConstructor('d/m/Y', 'Y/m/d'))
|
->supportDateFormats('d/m/Y', 'Y/m/d')
|
||||||
->mapper()
|
->mapper()
|
||||||
->map(DateTimeInterface::class, '2022/08/05');
|
->map(DateTimeInterface::class, '2022/08/05');
|
||||||
} catch (MappingError $error) {
|
} catch (MappingError $error) {
|
||||||
@ -83,7 +82,7 @@ final class DateTimeMappingTest extends IntegrationTest
|
|||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
(new MapperBuilder())
|
(new MapperBuilder())
|
||||||
->registerConstructor(new DateTimeFormatConstructor('Y/m/d'))
|
->supportDateFormats('Y/m/d')
|
||||||
->mapper()
|
->mapper()
|
||||||
->map(DateTimeInterface::class, 'invalid datetime');
|
->map(DateTimeInterface::class, 'invalid datetime');
|
||||||
} catch (MappingError $exception) {
|
} catch (MappingError $exception) {
|
||||||
|
Loading…
Reference in New Issue
Block a user