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

Skip to content

Commit b82f7a6

Browse files
committed
[Serializer] Added a ConstraintViolationListNormalizer
1 parent 522d079 commit b82f7a6

File tree

4 files changed

+137
-1
lines changed

4 files changed

+137
-1
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
use Symfony\Component\Serializer\Encoder\EncoderInterface;
7373
use Symfony\Component\Serializer\Encoder\YamlEncoder;
7474
use Symfony\Component\Serializer\Mapping\Factory\CacheClassMetadataFactory;
75+
use Symfony\Component\Serializer\Normalizer\ConstraintViolationListNormalizer;
7576
use Symfony\Component\Serializer\Normalizer\DataUriNormalizer;
7677
use Symfony\Component\Serializer\Normalizer\DateIntervalNormalizer;
7778
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
@@ -1298,6 +1299,13 @@ private function registerSerializerConfiguration(array $config, ContainerBuilder
12981299
$definition->addTag('serializer.encoder');
12991300
}
13001301

1302+
if (class_exists(ConstraintViolationListNormalizer::class)) {
1303+
// Run before serializer.normalizer.object
1304+
$definition = $container->register('serializer.normalizer.constraint_violation_list_normalizer', ConstraintViolationListNormalizer::class);
1305+
$definition->setPublic(false);
1306+
$definition->addTag('serializer.normalizer', array('priority' => -900));
1307+
}
1308+
13011309
$loader->load('serializer.xml');
13021310

13031311
$chainLoader = $container->getDefinition('serializer.mapping.chain_loader');

src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
4.1.0
5+
-----
6+
7+
* added `ConstraintViolationListNormalizer`
8+
49
4.0.0
510
-----
611

@@ -20,7 +25,7 @@ CHANGELOG
2025
* added support for serializing `DateInterval` objects
2126
* added getter for extra attributes in `ExtraAttributesException`
2227
* improved `CsvEncoder` to handle variable nested structures
23-
* CSV headers can be passed to the `CsvEncoder` via the `csv_headers` serialization context variable
28+
* CSV headers can be passed to the `CsvEncoder` via the `csv_headers` serialization context variable
2429

2530
3.3.0
2631
-----
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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\Normalizer;
13+
14+
use Symfony\Component\Validator\ConstraintViolationListInterface;
15+
16+
/**
17+
* A normalizer that normalize a ConstraintViolationListInterface instance.
18+
*
19+
* This Normalizer implements RFC7807 {@link https://tools.ietf.org/html/rfc7807}.
20+
*
21+
* @author Grégoire Pineau <[email protected]>
22+
* @author Kévin Dunglas <[email protected]>
23+
*/
24+
class ConstraintViolationListNormalizer implements NormalizerInterface
25+
{
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public function normalize($object, $format = null, array $context = array())
30+
{
31+
$violations = array();
32+
$messages = array();
33+
foreach ($object as $violation) {
34+
$violations[] = array(
35+
'propertyPath' => $violation->getPropertyPath(),
36+
'message' => $violation->getMessage(),
37+
'code' => $violation->getCode(),
38+
);
39+
$propertyPath = $violation->getPropertyPath();
40+
$prefix = $propertyPath ? sprintf('%s: ', $propertyPath) : '';
41+
$messages[] = $prefix.$violation->getMessage();
42+
}
43+
44+
return array(
45+
'title' => isset($context['title']) ? $context['title'] : 'An error occurred',
46+
'detail' => $messages ? implode("\n", $messages) : '',
47+
'violations' => $violations,
48+
);
49+
}
50+
51+
/**
52+
* {@inheritdoc}
53+
*/
54+
public function supportsNormalization($data, $format = null)
55+
{
56+
return $data instanceof ConstraintViolationListInterface;
57+
}
58+
}
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 <[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\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 <[email protected]>
21+
* @author Kévin Dunglas <[email protected]>
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+
}

0 commit comments

Comments
 (0)