Skip to content

Commit 06e85f0

Browse files
committed
Handle DateTimeFormat in MapTo/MapFrom/Mapper attributes
1 parent 132eb46 commit 06e85f0

19 files changed

+141
-21
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
- [GH#153](https://github.com/jolicode/automapper/pull/153) Handle DateTime format in MapTo/MapFrom/Mapper attributes
12+
1013
## [9.0.2] - 2024-05-23
1114
### Deprecated
1215
- [GH#136](https://github.com/jolicode/automapper/pull/136) Deprecate the ability to inject AST transformer factories withing stand-alone AutoMapper

docs/_nav.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- [Transformer](mapping/transformer.md)
1414
- [Provider](mapping/provider.md)
1515
- [Mapping inheritance](mapping/inheritance.md)
16+
- [DateTime format](mapping/date-time.md)
1617
- [Symfony Bundle](bundle/index.md)
1718
- [Installation](bundle/installation.md)
1819
- [Configuration](bundle/configuration.md)

docs/bundle/configuration.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ automapper:
4545
* When set to `always`, AutoMapper will always use the constructor, even if some mandatory properties are missing.
4646
In this case you may need to [provide a default value for the missing properties using the context](../getting-started/context.md).
4747

48-
* `date_time_format` (default: `\DateTimeInterface::RFC3339`): The format to use to transform a date from/to a string;
48+
* `date_time_format` (default: `\DateTimeInterface::RFC3339`): The format to use to transform a date from/to a string,
49+
can be overwritten by the attributes thanks to `dateTimeFormat` property, see [DateTime format](../mapping/date-time.md)
50+
for more details about it;
4951
* `check_attributes` (default: `true`): Check if the field should be mapped at runtime, this allow you to have dynamic
5052
partial mapping, if you don't use this feature set it to false as it will improve the performance;
5153
* `auto_register` (default: `true`): If the bundle should auto register the mappers in the container when it does not

docs/mapping/date-time.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# DateTime format
2+
3+
In addition to the default DateTime format you can set in AutoMapper context or in Bundle configuration, you can also
4+
use the `#[MapTo]` and `#[MapFrom]` attributes to define DateTime format of properties that should be mapped.
5+
6+
```php
7+
class Source
8+
{
9+
#[MapTo(target: 'array', dateTimeFormat: \DateTimeInterface::ATOM)]
10+
public \DateTimeImmutable $dateTime;
11+
}
12+
```
13+
14+
When doing so the property will be mapped using the given format.

docs/mapping/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ a `source` and a `target`.
1111
- [Transformer](transformer.md)
1212
- [Provider](provider.md)
1313
- [Mapping inheritance](inheritance.md)
14+
- [DateTime format](date-time.md)

src/Attribute/MapFrom.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111
final readonly class MapFrom
1212
{
1313
/**
14-
* @param class-string<object>|'array'|array<class-string<object>|'array'>|null $source The specific source class name or array. If null this attribute will be used for all source classes.
15-
* @param string|null $property The source property name. If null, the target property name will be used.
16-
* @param int|null $maxDepth The maximum depth of the mapping. If null, the default max depth will be used.
17-
* @param string|callable(mixed $value, object $object): mixed|null $transformer A transformer id or a callable that transform the value during mapping
18-
* @param bool|null $ignore if true, the property will be ignored during mapping
19-
* @param string|null $if The condition to map the property, using the expression language
20-
* @param string[]|null $groups The groups to map the property
14+
* @param class-string<object>|'array'|array<class-string<object>|'array'>|null $source The specific source class name or array. If null this attribute will be used for all source classes.
15+
* @param string|null $property The source property name. If null, the target property name will be used.
16+
* @param int|null $maxDepth The maximum depth of the mapping. If null, the default max depth will be used.
17+
* @param string|callable(mixed $value, object $object): mixed|null $transformer A transformer id or a callable that transform the value during mapping
18+
* @param bool|null $ignore If true, the property will be ignored during mapping
19+
* @param string|null $if The condition to map the property, using the expression language
20+
* @param string[]|null $groups The groups to map the property
21+
* @param string|null $dateTimeFormat The date-time format to use when transforming this property
2122
*/
2223
public function __construct(
2324
public string|array|null $source = null,
@@ -28,6 +29,7 @@ public function __construct(
2829
public ?string $if = null,
2930
public ?array $groups = null,
3031
public int $priority = 0,
32+
public ?string $dateTimeFormat = null,
3133
) {
3234
}
3335
}

src/Attribute/MapTo.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111
final readonly class MapTo
1212
{
1313
/**
14-
* @param class-string<object>|'array'|array<class-string<object>|'array'>|null $target The specific target class name or array. If null this attribute will be used for all target classes.
15-
* @param string|null $property The target property name. If null, the source property name will be used.
16-
* @param int|null $maxDepth The maximum depth of the mapping. If null, the default max depth will be used.
17-
* @param string|callable(mixed $value, object $object): mixed|null $transformer A transformer id or a callable that transform the value during mapping
18-
* @param bool|null $ignore if true, the property will be ignored during mapping
19-
* @param string|null $if The condition to map the property, using the expression language
20-
* @param string[]|null $groups The groups to map the property
14+
* @param class-string<object>|'array'|array<class-string<object>|'array'>|null $target The specific target class name or array. If null this attribute will be used for all target classes.
15+
* @param string|null $property The target property name. If null, the source property name will be used.
16+
* @param int|null $maxDepth The maximum depth of the mapping. If null, the default max depth will be used.
17+
* @param string|callable(mixed $value, object $object): mixed|null $transformer A transformer id or a callable that transform the value during mapping
18+
* @param bool|null $ignore If true, the property will be ignored during mapping
19+
* @param string|null $if The condition to map the property, using the expression language
20+
* @param string[]|null $groups The groups to map the property
21+
* @param string|null $dateTimeFormat The date-time format to use when transforming this property
2122
*/
2223
public function __construct(
2324
public string|array|null $target = null,
@@ -28,6 +29,7 @@ public function __construct(
2829
public ?string $if = null,
2930
public ?array $groups = null,
3031
public int $priority = 0,
32+
public ?string $dateTimeFormat = null,
3133
) {
3234
}
3335
}

src/Attribute/Mapper.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
final readonly class Mapper
1414
{
1515
/**
16-
* @param class-string<object>|'array'|array<class-string<object>|'array'>|null $source the source class or classes
17-
* @param class-string<object>|'array'|array<class-string<object>|'array'>|null $target the target class or classes
16+
* @param class-string<object>|'array'|array<class-string<object>|'array'>|null $source The source class or classes
17+
* @param class-string<object>|'array'|array<class-string<object>|'array'>|null $target The target class or classes
18+
* @param string|null $dateTimeFormat The date-time format to use when transforming this property
1819
*/
1920
public function __construct(
2021
public string|array|null $source = null,
@@ -23,6 +24,7 @@ public function __construct(
2324
public ?ConstructorStrategy $constructorStrategy = null,
2425
public ?bool $allowReadOnlyTargetToPopulate = null,
2526
public int $priority = 0,
27+
public ?string $dateTimeFormat = null,
2628
) {
2729
}
2830
}

src/Event/PropertyMetadataEvent.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function __construct(
2323
public ?TypesMatching $types = null,
2424
public ?int $maxDepth = null,
2525
public ?TransformerInterface $transformer = null,
26+
public ?string $dateTimeFormat = null,
2627
public ?bool $ignored = null,
2728
public ?string $ignoreReason = null,
2829
public ?string $if = null,

src/EventListener/MapFromListener.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ private function addPropertyFromTarget(GenerateMapperEvent $event, MapFrom $mapF
7777
target: $targetProperty,
7878
maxDepth: $mapFrom->maxDepth,
7979
transformer: $this->getTransformerFromMapAttribute($event->mapperMetadata->target, $mapFrom, false),
80+
dateTimeFormat: $mapFrom->dateTimeFormat,
8081
ignored: $mapFrom->ignore,
8182
ignoreReason: $mapFrom->ignore === true ? 'Property is ignored by MapFrom Attribute on Target' : null,
8283
if: $mapFrom->if,

0 commit comments

Comments
 (0)