8000 [Serializer] Added a ConstraintViolationListNormalizer · symfony/symfony@2a35d09 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2a35d09

Browse files
committed
[Serializer] Added a ConstraintViolationListNormalizer
1 parent bbeca51 commit 2a35d09

File tree

6 files changed

+137
-1
lines changed

6 files changed

+137
-1
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorFromClassMetadata;
7474
use Symfony\Component\Serializer\Mapping\Factory\CacheClassMetadataFactory;
7575
use Symfony\Component\Serializer\Normalizer\DateIntervalNormalizer;
76+
use Symfony\Component\Serializer\Normalizer\ConstraintViolationListNormalizer;
7677
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
7778
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
7879
use Symfony\Component\Stopwatch\Stopwatch;
@@ -1227,6 +1228,10 @@ private function registerSerializerConfiguration(array $config, ContainerBuilder
12271228
$container->removeDefinition('serializer.normalizer.dateinterval');
12281229
}
12291230

1231+
if (!class_exists(ConstraintViolationListNormalizer::class)) {
1232+
$container->removeDefinition('serializer.normalizer.constraint_violation_list');
1233+
}
1234+
12301235
if (!class_exists(ClassDiscriminatorFromClassMetadata::class)) {
12311236
$container->removeAlias('Symfony\Component\Serializer\Mapping\ClassDiscriminatorResolverInterface');
12321237
$container->removeDefinition('serializer.mapping.class_discriminator_resolver');

src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
3131
<service id="Symfony\Component\Serializer\Mapping\ClassDiscriminatorResolverInterface" alias="serializer.mapping.class_discriminator_resolver" />
3232

3333
<!-- Normalizer -->
34+
<service id="serializer.normalizer.constraint_violation_list" class="Symfony\Component\Serializer\Normalizer\ConstraintViolationListNormalizer">
35+
<!-- Run before serializer.normalizer.object -->
36+
<tag name="serializer.normalizer" priority="-915" />
37+
</service>
38+
3439
<service id="serializer.normalizer.dateinterval" class="Symfony\Component\Serializer\Normalizer\DateIntervalNormalizer">
3540
<!-- Run before serializer.normalizer.object -->
3641
<tag name="serializer.normalizer" priority="-915" />

src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ CHANGELOG
1313
maximum depth is reached
1414
* added optional `int[] $ignoredNodeTypes` argument to `XmlEncoder::__construct`. XML decoding now
1515
ignores comment node types by default.
16+
* added `ConstraintViolationListNormalizer`
1617

1718
4.0.0
1819
-----
@@ -33,7 +34,7 @@ CHANGELOG
3334
* added support for serializing `DateInterval` objects
3435
* added getter for extra attributes in `ExtraAttributesException`
3536
* improved `CsvEncoder` to handle variable nested structures
36-
* CSV headers can be passed to the `CsvEncoder` via the `csv_headers` serialization context variable
37+
* CSV headers can be passed to the `CsvEncoder` via the `csv_headers` serialization context variable
3738
* added `$context` when checking for encoding, decoding and normalizing in `Serializer`
3839

3940
3.3.0
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Normalizer;
13+
14+
use Symfony\Component\Validator\ConstraintViolationListInterface;
15+
16+
/**
17+
* A normalizer that normalizes a ConstraintViolationListInterface instance.
< 9E88 /td>
18+
*
19+
* This Normalizer implements RFC7807 {@link https://tools.ietf.org/html/rfc7807}.
20+
*
21+
*
22+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
23+
* @author Kévin Dunglas <dunglas@gmail.com>
24+
*/
25+
class ConstraintViolationListNormalizer implements NormalizerInterface
26+
{
27+
/**
28+
* {@inheritdoc}
29+
*/
30+
public function normalize($object, $format = null, array $context = array())
31+
{
32+
$violations = array();
33+
$messages = array();
34+
foreach ($object as $violation) {
35+
$violations[] = array(
36+
'propertyPath' => $violation->getPropertyPath(),
37+
'message' => $violation->getMessage(),
38+
'code' => $violation->getCode(),
39+
);
40+
$propertyPath = $violation->getPropertyPath();
41+
$prefix = $propertyPath ? sprintf('%s: ', $propertyPath) : '';
42+
$messages[] = $prefix.$violation->getMessage();
43+
}
44+
45+
return array(
46+
'title' => isset($context['title']) ? $context['title'] : 'An error occurred',
47+
'detail' => $messages ? implode("\n", $messages) : '',
48+
'violations' => $violations,
49+
);
50+
}
51+
52+
/**
53+
* {@inheritdoc}
54+
*/
55+
public function supportsNormalization($data, $format = null)
56+
{
57+
return $data instanceof ConstraintViolationListInterface;
58+
}
59+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Tests\Normalizer;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Serializer\Normalizer\ConstraintViolationListNormalizer;
16+
use Symfony\Component\Validator\ConstraintViolation;
17+
use Symfony\Component\Validator\ConstraintViolationList;
18+
19+
/**
20+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
21+
* @author Kévin Dunglas <dunglas@gmail.com>
22+
*/
23+
class ConstraintViolationListNormalizerTest extends TestCase
24+
{
25+
private $normalizer;
26+
27+
protected function setUp()
28+
{
29+
$this->normalizer = new ConstraintViolationListNormalizer();
30+
}
31+
32+
public function testSupportsNormalization()
33+
{
34+
$this->assertTrue($this->normalizer->supportsNormalization(new ConstraintViolationList()));
35+
$this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
36+
}
37+
38+
public function testNormalize()
39+
{
40+
$list = new ConstraintViolationList(array(
41+
new ConstraintViolation('a', 'b', array(), 'c', 'd', 'e', null, 'f'),
42+
new ConstraintViolation('1', '2', array(), '3', '4', '5', null, '6'),
43+
));
44+
45+
$expected = array(
46+
'title' => 'An error occurred',
47+
'detail' => 'd: a
48+
4: 1',
49+
'violations' => array(
50+
array(
51+
'propertyPath' => 'd',
52+
'message' => 'a',
53+
'code' => 'f',
54+
),
55+
array(
56+
'propertyPath' => '4',
57+
'message' => '1',
58+
'code' => '6',
59+
),
60+
),
61+
);
62+
63+
$this->assertEquals($expected, $this->normalizer->normalize($list));
64+
}
65+
}

src/Symfony/Component/Serializer/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"symfony/http-foundation": "~3.4|~4.0",
2626
"symfony/cache": "~3.4|~4.0",
2727
"symfony/property-info": "~3.4|~4.0",
28+
"symfony/validator": "~3.4|~4.0",
2829
"doctrine/annotations": "~1.0",
2930
"symfony/dependency-injection": "~3.4|~4.0",
3031
"doctrine/cache": "~1.0",

0 commit comments

Comments
 (0)
0