-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] Added a ConstraintViolationListNormalizer #22150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Serializer\Normalizer; | ||
|
||
use Symfony\Component\Validator\ConstraintViolationListInterface; | ||
|
||
/** | ||
* A normalizer that normalizes a ConstraintViolationListInterface instance. | ||
* | ||
* This Normalizer implements RFC7807 {@link https://tools.ietf.org/html/rfc7807}. | ||
* | ||
* | ||
* @author Grégoire Pineau <lyrixx@lyrixx.info> | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
*/ | ||
class ConstraintViolationListNormalizer implements NormalizerInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function normalize($object, $format = null, array $context = array()) | ||
{ | ||
$violations = array(); | ||
$messages = array(); | ||
foreach ($object as $violation) { | ||
$violations[] = array( | ||
'propertyPath' => $violation->getPropertyPath(), | ||
'message' => $violation->getMessage(), | ||
lyrixx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'code' => $violation->getCode(), | ||
); | ||
$propertyPath = $violation->getPropertyPath(); | ||
$prefix = $propertyPath ? sprintf('%s: ', $propertyPath) : ''; | ||
$messages[] = $prefix.$violation->getMessage(); | ||
} | ||
|
||
return array( | ||
'title' => isset($context['title']) ? $context['title'] : 'An error occurred', | ||
'detail' => $messages ? implode("\n", $messages) : '', | ||
'violations' => $violations, | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsNormalization($data, $format = null) | ||
{ | ||
return $data instanceof ConstraintViolationListInterface; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Serializer\Tests\Normalizer; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Serializer\Normalizer\ConstraintViolationListNormalizer; | ||
use Symfony\Component\Validator\ConstraintViolation; | ||
use Symfony\Component\Validator\ConstraintViolationList; | ||
|
||
/** | ||
* @author Grégoire Pineau <lyrixx@lyrixx.info> | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
*/ | ||
class ConstraintViolationListNormalizerTest extends TestCase | ||
{ | ||
private $normalizer; | ||
|
||
protected function setUp() | ||
{ | ||
$this->normalizer = new ConstraintViolationListNormalizer(); | ||
} | ||
|
||
public function testSupportsNormalization() | ||
{ | ||
$this->assertTrue($this->normalizer->supportsNormalization(new ConstraintViolationList())); | ||
$this->assertFalse($this->normalizer->supportsNormalization(new \stdClass())); | ||
} | ||
|
||
public function testNormalize() | ||
{ | ||
$list = new ConstraintViolationList(array( | ||
new ConstraintViolation('a', 'b', array(), 'c', 'd', 'e', null, 'f'), | ||
new ConstraintViolation('1', '2', array(), '3', '4', '5', null, '6'), | ||
)); | ||
|
||
$expected = array( | ||
'title' => 'An error occurred', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. RFC 7807 is unambiguous on this point:
Perhaps: {
"type": "/validation-error",
"title": "Validation failed",
...
} |
||
'detail' => 'd: a | ||
4: 1', | ||
'violations' => array( | ||
array( | ||
'propertyPath' => 'd', | ||
'message' => 'a', | ||
'code' => 'f', | ||
), | ||
array( | ||
'propertyPath' => '4', | ||
'message' => '1', | ||
'code' => '6', | ||
), | ||
), | ||
); | ||
|
||
$this->assertEquals($expected, $this->normalizer->normalize($list)); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.