public function normalize($object, $format = null, array $context = [])
{
$violations = [];
$messages = [];
foreach ($object as $violation) {
$violations[] = ['propertyPath' => $violation->getPropertyPath(), 'message' => $violation->getMessage()];
$propertyPath = $violation->getPropertyPath();
$prefix = $propertyPath ? sprintf('%s: ', $propertyPath) : '';
$messages[] = $prefix . $violation->getMessage();
}
return ['type' => $context['type'] ?? 'https://tools.ietf.org/html/rfc2616#section-10', 'title' => $context['title'] ?? 'An error occurred', 'detail' => $messages ? implode("\n", $messages) : (string) $object, 'violations' => $violations];
}
public function testNormalize() { $normalizer = new ConstraintViolationListNormalizer(); $list = new ConstraintViolationList([new ConstraintViolation('a', 'b', [], 'c', 'd', 'e'), new ConstraintViolation('1', '2', [], '3', '4', '5')]); $expected = ['type' => 'https://tools.ietf.org/html/rfc2616#section-10', 'title' => 'An error occurred', 'detail' => 'd: a 4: 1', 'violations' => [['propertyPath' => 'd', 'message' => 'a'], ['propertyPath' => '4', 'message' => '1']]]; $this->assertEquals($expected, $normalizer->normalize($list)); }