Skip to content

Commit

Permalink
DateTime - Deserialize from more formats (default)
Browse files Browse the repository at this point in the history
  • Loading branch information
iksss-invia committed Sep 24, 2024
1 parent 3b832aa commit c7c3840
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 2 deletions.
4 changes: 4 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ private function addHandlersSection(NodeBuilder $builder)
->addDefaultsIfNotSet()
->children()
->scalarNode('default_format')->defaultValue(\DateTime::RFC3339)->end()
->arrayNode('default_deserialization_formats')
->scalarPrototype()->end()
->defaultValue([\DateTime::RFC3339])
->end()
->scalarNode('default_timezone')->defaultValue(date_default_timezone_get())->end()
->scalarNode('cdata')->defaultTrue()->end()
->end()
Expand Down
3 changes: 2 additions & 1 deletion DependencyInjection/JMSSerializerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ private function loadInternal(array $config, ScopedContainer $container, array $
$container->getDefinition('jms_serializer.datetime_handler')
->replaceArgument(0, $config['handlers']['datetime']['default_format'])
->replaceArgument(1, $config['handlers']['datetime']['default_timezone'])
->replaceArgument(2, $config['handlers']['datetime']['cdata']);
->replaceArgument(2, $config['handlers']['datetime']['cdata'])
->replaceArgument(3, $config['handlers']['datetime']['default_deserialization_formats']);

$container->getDefinition('jms_serializer.array_collection_handler')
->replaceArgument(0, $config['handlers']['array_collection']['initialize_excluded']);
Expand Down
1 change: 1 addition & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<argument/> <!-- default_format -->
<argument/> <!-- default_timezone -->
<argument/> <!-- cdata -->
<argument/> <!-- default_deserialization_formats -->
</service>
<service id="jms_serializer.form_error_handler" class="JMS\Serializer\Handler\FormErrorHandler" public="false">
<argument type="service" id="translator" on-invalid="null" />
Expand Down
2 changes: 2 additions & 0 deletions Resources/doc/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ values:
handlers:
datetime:
default_format: "Y-m-d\\TH:i:sP" # ATOM
default_deserialization_formats:
- "Y-m-d\\TH:i:sP" # ATOM
default_timezone: "UTC" # defaults to whatever timezone set in php.ini or via date_default_timezone_set
array_collection:
initialize_excluded: false
Expand Down
8 changes: 8 additions & 0 deletions Tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,14 @@ public function testDefaultDateFormat()
$this->assertEquals(\DateTime::ATOM, $config['handlers']['datetime']['default_format']);
}

public function testDefaultDateDeserializationFormats()
{
$processor = new Processor();
$config = $processor->processConfiguration(new Configuration(true), []);

$this->assertEquals([\DateTime::ATOM], $config['handlers']['datetime']['default_deserialization_formats']);
}

public function testDefaultUidFormat()
{
$processor = new Processor();
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
],
"require": {
"php": "^7.4 || ^8.0",
"jms/serializer": "^3.28",
"jms/serializer": "^3.31",
"jms/metadata": "^2.6",
"symfony/config": "^5.4 || ^6.0 || ^7.0",
"symfony/dependency-injection": "^5.4 || ^6.0 || ^7.0",
Expand Down

0 comments on commit c7c3840

Please sign in to comment.