Skip to content

Commit

Permalink
Merge pull request #952 from iKsSs/date-deserialization
Browse files Browse the repository at this point in the history
Date deserialization format type(s)
  • Loading branch information
scyzoryck authored Nov 1, 2024
2 parents 3b832aa + 03baa00 commit 2eefc11
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
run: "vendor/bin/phpunit --coverage-clover=coverage.xml"

- name: "Upload coverage file"
uses: "actions/upload-artifact@v2"
uses: "actions/upload-artifact@v4"
with:
name: "phpunit-${{ matrix.dependencies }}-${{ matrix.php-version }}.coverage"
path: "coverage.xml"
Expand All @@ -67,7 +67,7 @@ jobs:
fetch-depth: 2

- name: "Download coverage files"
uses: "actions/download-artifact@v2"
uses: "actions/download-artifact@v4"
with:
path: "reports"

Expand Down
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([])
->end()
->scalarNode('default_timezone')->defaultValue(date_default_timezone_get())->end()
->scalarNode('cdata')->defaultTrue()->end()
->end()
Expand Down
4 changes: 3 additions & 1 deletion DependencyInjection/JMSSerializerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Finder\Finder;
Expand Down Expand Up @@ -126,7 +127,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'] ? [$config['handlers']['datetime']['default_format']] : $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([], $config['handlers']['datetime']['default_deserialization_formats']);
}

public function testDefaultUidFormat()
{
$processor = new Processor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ class ObjectUsingEnumDeserialize
{
private ObjectUsingEnumDeserializeCard $one;

/** @Type("enum<'JMS\SerializerBundle\Tests\DependencyInjection\Fixture\ObjectUsingEnumDeserializeCard'>") */
#[Type(name: 'enum<"JMS\SerializerBundle\Tests\DependencyInjection\Fixture\ObjectUsingEnumDeserializeCard">')]
/** @Type("enum<JMS\SerializerBundle\Tests\DependencyInjection\Fixture\ObjectUsingEnumDeserializeCard>") */
#[Type(name: 'enum<JMS\SerializerBundle\Tests\DependencyInjection\Fixture\ObjectUsingEnumDeserializeCard>')]
private $two;
/** @Type("array<enum<'JMS\SerializerBundle\Tests\DependencyInjection\Fixture\ObjectUsingEnumDeserializeCard'>>") */
#[Type(name: 'array<enum<"JMS\SerializerBundle\Tests\DependencyInjection\Fixture\ObjectUsingEnumDeserializeCard">>')]
/** @Type("array<enum<JMS\SerializerBundle\Tests\DependencyInjection\Fixture\ObjectUsingEnumDeserializeCard>>") */
#[Type(name: 'array<enum<JMS\SerializerBundle\Tests\DependencyInjection\Fixture\ObjectUsingEnumDeserializeCard>>')]
private array $three;

public function __construct()
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
],
"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",
"symfony/framework-bundle": "^5.4 || ^6.0 || ^7.0"
},
"require-dev": {
"doctrine/annotations": "^1.14 || ^2.0",
"doctrine/coding-standard": "^12.0",
"doctrine/orm": "^2.14",
"phpunit/phpunit": "^8.0 || ^9.0",
Expand Down

0 comments on commit 2eefc11

Please sign in to comment.