Skip to content

Commit

Permalink
Merge pull request #5 from geosocio/normalize-property-path
Browse files Browse the repository at this point in the history
Normalize the propertyPath so it matches the repsonse
  • Loading branch information
davidbarratt authored Mar 18, 2018
2 parents 8eb45b1 + 247b28a commit aa7f9c6
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
41 changes: 39 additions & 2 deletions src/Serializer/ConstraintViolationNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace GeoSocio\HttpSerializer\Serializer;

use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Validator\ConstraintViolationInterface;

Expand All @@ -10,16 +11,52 @@
*/
class ConstraintViolationNormalizer implements NormalizerInterface
{
/**
* @var NameConverterInterface|null
*/
protected $nameConverter;

/**
* Constraint Violoation Normalizer
*
* @param NameConverterInterface|null $nameConverter
*/
public function __construct(?NameConverterInterface $nameConverter = null)
{
$this->nameConverter = $nameConverter;
}

/**
* {@inheritdoc}
*/
public function normalize($object, $format = null, array $context = array())
{
return [

// Convert the peroperty paths so they match the serialization.
$propertyPath = $object->getPropertyPath();
if ($this->nameConverter) {
$properties = explode('.', $propertyPath);
$properties = array_map(function ($property) {
return $this->nameConverter->normalize($property);
}, $properties);
$propertyPath = implode('.', $properties);
}

$data = [
'message' => $object->getMessage(),
'propertyPath' => $object->getPropertyPath(),
'propertyPath' => $propertyPath,
'code' => $object->getCode(),
];

if ($this->nameConverter) {
foreach ($data as $key => $value) {
$normalized[$this->nameConverter->normalize($key)] = $value;
}
} else {
$normalized = $data;
}

return $normalized;
}

/**
Expand Down
39 changes: 38 additions & 1 deletion tests/Serializer/ConstraintViolationNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace GeoSocio\HttpSerializer\Serializer;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
use Symfony\Component\Validator\ConstraintViolationInterface;

/**
Expand All @@ -11,7 +12,7 @@
class ConstraintViolationNormalizerTest extends TestCase
{
/**
* Test Supports Normalization.
* Test Normalization without Serializer.
*/
public function testNormalize()
{
Expand Down Expand Up @@ -40,6 +41,42 @@ public function testNormalize()
$this->assertEquals($code, $result['code']);
}

/**
* Test Normalization with Serializer.
*/
public function testNormalizeWithNameConvertor()
{
$message = 'Test Message';
$propertyPath = 'id';
$code = 123;

$nameConverter = $this->createMock(NameConverterInterface::class);
$nameConverter->method('normalize')
->willReturnArgument(0);

$normalizer = new ConstraintViolationNormalizer($nameConverter);

$violation = $this->createMock(ConstraintViolationInterface::class);
$violation->expects($this->once())
->method('getMessage')
->willReturn($message);
$violation->expects($this->once())
->method('getPropertyPath')
->willReturn($propertyPath);
$violation->expects($this->once())
->method('getCode')
->willReturn($code);

$result = $normalizer->normalize($violation);

$this->assertArrayHasKey('message', $result);
$this->assertEquals($message, $result['message']);
$this->assertArrayHasKey('propertyPath', $result);
$this->assertEquals($propertyPath, $result['propertyPath']);
$this->assertArrayHasKey('code', $result);
$this->assertEquals($code, $result['code']);
}

/**
* Test Supports Normalization.
*/
Expand Down

0 comments on commit aa7f9c6

Please sign in to comment.