Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 489f341

Browse files
committed
Replace exception workflow with DenormalizationResult
1 parent 57bd3c7 commit 489f341

14 files changed

+232
-144
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer;
13+
14+
final class DenormalizationResult
15+
{
16+
private $denormalizedValue;
17+
private $invariantViolations = [];
18+
19+
private function __construct()
20+
{
21+
}
22+
23+
public static function success($denormalizedValue): self
24+
{
25+
$result = new self();
26+
$result->denormalizedValue = $denormalizedValue;
27+
28+
return $result;
29+
}
30+
31+
/**
32+
* @param array<string, array<InvariantViolation>> $invariantViolations
33+
*/
34+
public static function failure(array $invariantViolations): self
35+
{
36+
$result = new self();
37+
$result->invariantViolations = $invariantViolations;
38+
39+
return $result;
40+
}
41+
42+
public function isSucessful(): bool
43+
{
44+
return [] === $this->invariantViolations;
45+
}
46+
47+
public function getDenormalizedValue()
48+
{
49+
return $this->denormalizedValue;
50+
}
51+
52+
public function getInvariantViolations(): array
53+
{
54+
return $this->invariantViolations;
55+
}
56+
57+
/**
58+
* @return array<string, array<InvariantViolation>>
59+
*/
60+
public function getInvariantViolationsNestedIn(string $parentPath): array
61+
{
62+
if ('' === $parentPath) {
63+
throw new \InvalidArgumentException('Parent path cannot be empty.');
64+
}
65+
66+
$nestedViolations = [];
67+
68+
foreach ($this->invariantViolations as $path => $violations) {
69+
$path = '' !== $path ? "{$parentPath}.{$path}" : $parentPath;
70+
71+
$nestedViolations[$path] = $violations;
72+
}
73+
74+
return $nestedViolations;
75+
}
76+
77+
/**
78+
* @return array<string, array<string>>
79+
*/
80+
public function getInvariantViolationMessages(): array
81+
{
82+
$messages = [];
83+
84+
foreach ($this->invariantViolations as $path => $violations) {
85+
foreach ($violations as $violation) {
86+
$messages[$path][] = $violation->getMessage();
87+
}
88+
}
89+
90+
return $messages;
91+
}
92+
}

src/Symfony/Component/Serializer/Exception/InvariantViolationException.php

Lines changed: 0 additions & 73 deletions
This file was deleted.

src/Symfony/Component/Serializer/InvariantViolation.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111

1212
namespace Symfony\Component\Serializer;
1313

14-
class InvariantViolation
14+
final class InvariantViolation
1515
{
1616
private $normalizedValue;
1717
private $message;
1818
private $exception;
1919

20-
public function __construct($normalizedValue, string $message, \Throwable $exception)
20+
public function __construct($normalizedValue, string $message, ?\Throwable $exception = null)
2121
{
2222
$this->normalizedValue = $normalizedValue;
2323
$this->message = $message;
@@ -34,7 +34,7 @@ public function getMessage(): string
3434
return $this->message;
3535
}
3636

37-
public function getException(): \Throwable
37+
public function getException(): ?\Throwable
3838
{
3939
return $this->exception;
4040
}

src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
1616
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
1717
use Symfony\Component\PropertyInfo\Type;
18+
use Symfony\Component\Serializer\DenormalizationResult;
1819
use Symfony\Component\Serializer\Encoder\CsvEncoder;
1920
use Symfony\Component\Serializer\Encoder\JsonEncoder;
2021
use Symfony\Component\Serializer\Encoder\XmlEncoder;
2122
use Symfony\Component\Serializer\Exception\ExtraAttributesException;
22-
use Symfony\Component\Serializer\Exception\InvariantViolationException;
2323
use Symfony\Component\Serializer\Exception\LogicException;
2424
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
2525
use Symfony\Component\Serializer\Exception\RuntimeException;
@@ -334,10 +334,16 @@ public function denormalize($data, string $type, string $format = null, array $c
334334
}
335335
}
336336

337-
try {
338-
$value = $this->validateAndDenormalize($resolvedClass, $attribute, $value, $format, $context);
339-
} catch (InvariantViolationException $exception) {
340-
$invariantViolations += $exception->getViolationsNestedIn($attribute);
337+
$value = $this->validateAndDenormalize($resolvedClass, $attribute, $value, $format, $context);
338+
339+
if ($value instanceof DenormalizationResult) {
340+
if (!$value->isSucessful()) {
341+
$invariantViolations += $value->getInvariantViolationsNestedIn($attribute);
342+
343+
continue;
344+
}
345+
346+
$value = $value->getDenormalizedValue();
341347
}
342348

343349
try {
@@ -348,7 +354,11 @@ public function denormalize($data, string $type, string $format = null, array $c
348354
}
349355

350356
if ([] !== $invariantViolations) {
351-
throw new InvariantViolationException($invariantViolations);
357+
return DenormalizationResult::failure($invariantViolations);
358+
}
359+
360+
if ($context[self::COLLECT_INVARIANT_VIOLATIONS] ?? false) {
361+
return DenormalizationResult::success($object);
352362
}
353363

354364
if (!empty($extraAttributes)) {

src/Symfony/Component/Serializer/Normalizer/ArrayDenormalizer.php

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111

1212
namespace Symfony\Component\Serializer\Normalizer;
1313

14+
use Symfony\Component\Serializer\DenormalizationResult;
1415
use Symfony\Component\Serializer\Exception\BadMethodCallException;
1516
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
16-
use Symfony\Component\Serializer\Exception\InvariantViolationException;
1717
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
18+
use Symfony\Component\Serializer\InvariantViolation;
1819
use Symfony\Component\Serializer\SerializerAwareInterface;
1920
use Symfony\Component\Serializer\SerializerInterface;
2021

@@ -53,22 +54,43 @@ public function denormalize($data, string $type, string $format = null, array $c
5354
$type = substr($type, 0, -2);
5455

5556
$invariantViolations = [];
57+
$collectInvariantViolations = $context[self::COLLECT_INVARIANT_VIOLATIONS] ?? false;
5658

5759
$builtinType = isset($context['key_type']) ? $context['key_type']->getBuiltinType() : null;
5860
foreach ($data as $key => $value) {
59-
try {
60-
if (null !== $builtinType && !('is_'.$builtinType)($key)) {
61-
throw new NotNormalizableValueException($key, $value, sprintf('The type of the key "%s" must be "%s" ("%s" given).', $key, $builtinType, get_debug_type($key)));
61+
if (null !== $builtinType && !('is_'.$builtinType)($key)) {
62+
$message = sprintf('The type of the key "%s" must be "%s" ("%s" given).', $key, $builtinType, get_debug_type($key));
63+
64+
if ($collectInvariantViolations) {
65+
$invariantViolations[$key][] = new InvariantViolation($value, $message);
66+
67+
continue;
6268
}
6369

64-
$data[$key] = $serializer->denormalize($value, $type, $format, $context);
65-
} catch (InvariantViolationException $exception) {
66-
$invariantViolations += $exception->getViolationsNestedIn($key);
70+
throw new NotNormalizableValueException($key, $value, );
6771
}
72+
73+
$denormalizedValue = $serializer->denormalize($value, $type, $format, $context);
74+
75+
if ($denormalizedValue instanceof DenormalizationResult) {
76+
if (!$denormalizedValue->isSucessful()) {
77+
$invariantViolations += $denormalizedValue->getInvariantViolationsNestedIn($key);
78+
79+
continue;
80+
}
81+
82+
$denormalizedValue = $denormalizedValue->getDenormalizedValue();
83+
}
84+
85+
$data[$key] = $denormalizedValue;
6886
}
6987

7088
if ([] !== $invariantViolations) {
71-
throw new InvariantViolationException($invariantViolations);
89+
return DenormalizationResult::failure($invariantViolations);
90+
}
91+
92+
if ($collectInvariantViolations) {
93+
return DenormalizationResult::success($data);
7294
}
7395

7496
return $data;

src/Symfony/Component/Serializer/Normalizer/DataUriNormalizer.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
use Symfony\Component\HttpFoundation\File\File;
1515
use Symfony\Component\Mime\MimeTypeGuesserInterface;
1616
use Symfony\Component\Mime\MimeTypes;
17+
use Symfony\Component\Serializer\DenormalizationResult;
1718
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
18-
use Symfony\Component\Serializer\Exception\InvariantViolationException;
1919
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
2020
use Symfony\Component\Serializer\InvariantViolation;
2121

@@ -94,16 +94,22 @@ public function supportsNormalization($data, string $format = null)
9494
public function denormalize($data, string $type, string $format = null, array $context = [])
9595
{
9696
try {
97-
return $this->doDenormalize($data, $type);
97+
$result = $this->doDenormalize($data, $type);
9898
} catch (NotNormalizableValueException $exception) {
9999
if ($context[self::COLLECT_INVARIANT_VIOLATIONS] ?? false) {
100100
$violation = new InvariantViolation($data, 'This value is not a valid data URI.', $exception);
101101

102-
throw new InvariantViolationException(['' => [$violation]]);
102+
return DenormalizationResult::failure(['' => [$violation]]);
103103
}
104104

105105
throw $exception;
106106
}
107+
108+
if ($context[self::COLLECT_INVARIANT_VIOLATIONS] ?? false) {
109+
return DenormalizationResult::success($result);
110+
}
111+
112+
return $result;
107113
}
108114

109115
private function doDenormalize($data, string $type)

src/Symfony/Component/Serializer/Normalizer/DateIntervalNormalizer.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
namespace Symfony\Component\Serializer\Normalizer;
1313

14+
use Symfony\Component\Serializer\DenormalizationResult;
1415
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
15-
use Symfony\Component\Serializer\Exception\InvariantViolationException;
1616
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
1717
use Symfony\Component\Serializer\InvariantViolation;
1818

@@ -74,16 +74,22 @@ public function hasCacheableSupportsMethod(): bool
7474
public function denormalize($data, string $type, string $format = null, array $context = [])
7575
{
7676
try {
77-
return $this->doDenormalize($data, $context);
77+
$result = $this->doDenormalize($data, $context);
7878
} catch (\Throwable $exception) {
7979
if ($context[self::COLLECT_INVARIANT_VIOLATIONS] ?? false) {
8080
$violation = new InvariantViolation($data, 'This value is not a valid date interval.', $exception);
8181

82-
throw new InvariantViolationException(['' => [$violation]]);
82+
return DenormalizationResult::failure(['' => [$violation]]);
8383
}
8484

8585
throw $exception;
8686
}
87+
88+
if ($context[self::COLLECT_INVARIANT_VIOLATIONS] ?? false) {
89+
return DenormalizationResult::success($result);
90+
}
91+
92+
return $result;
8793
}
8894

8995
private function doDenormalize($data, array $context = [])

0 commit comments

Comments
 (0)