[Serializer] Use Serializer's LogicException when applicable

This commit is contained in:
Kévin Dunglas 2015-02-03 23:24:43 +01:00
parent fcd6d6085f
commit 8ddc888f00
3 changed files with 8 additions and 3 deletions

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\Serializer\Normalizer;
use Symfony\Component\Serializer\Exception\CircularReferenceException; use Symfony\Component\Serializer\Exception\CircularReferenceException;
use Symfony\Component\Serializer\Exception\InvalidArgumentException; use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Exception\RuntimeException; use Symfony\Component\Serializer\Exception\RuntimeException;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory; use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter; use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
@ -125,13 +126,15 @@ abstract class AbstractNormalizer extends SerializerAwareNormalizer implements N
* @param array $camelizedAttributes * @param array $camelizedAttributes
* *
* @return self * @return self
*
* @throws LogicException
*/ */
public function setCamelizedAttributes(array $camelizedAttributes) public function setCamelizedAttributes(array $camelizedAttributes)
{ {
trigger_error(sprintf('%s is deprecated since version 2.7 and will be removed in 3.0. Use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter instead.', __METHOD__), E_USER_DEPRECATED); trigger_error(sprintf('%s is deprecated since version 2.7 and will be removed in 3.0. Use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter instead.', __METHOD__), E_USER_DEPRECATED);
if ($this->nameConverter && !$this->nameConverter instanceof CamelCaseToSnakeCaseNameConverter) { if ($this->nameConverter && !$this->nameConverter instanceof CamelCaseToSnakeCaseNameConverter) {
throw new \LogicException(sprintf('%s cannot be called if a custom Name Converter is defined.', __METHOD__)); throw new LogicException(sprintf('%s cannot be called if a custom Name Converter is defined.', __METHOD__));
} }
$attributes = array(); $attributes = array();

View File

@ -12,6 +12,7 @@
namespace Symfony\Component\Serializer\Normalizer; namespace Symfony\Component\Serializer\Normalizer;
use Symfony\Component\Serializer\Exception\CircularReferenceException; use Symfony\Component\Serializer\Exception\CircularReferenceException;
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Exception\RuntimeException; use Symfony\Component\Serializer\Exception\RuntimeException;
/** /**
@ -40,6 +41,7 @@ class GetSetMethodNormalizer extends AbstractNormalizer
/** /**
* {@inheritdoc} * {@inheritdoc}
* *
* @throws LogicException
* @throws CircularReferenceException * @throws CircularReferenceException
*/ */
public function normalize($object, $format = null, array $context = array()) public function normalize($object, $format = null, array $context = array())
@ -71,7 +73,7 @@ class GetSetMethodNormalizer extends AbstractNormalizer
} }
if (null !== $attributeValue && !is_scalar($attributeValue)) { if (null !== $attributeValue && !is_scalar($attributeValue)) {
if (!$this->serializer instanceof NormalizerInterface) { if (!$this->serializer instanceof NormalizerInterface) {
throw new \LogicException(sprintf('Cannot normalize attribute "%s" because injected serializer is not a normalizer', $attributeName)); throw new LogicException(sprintf('Cannot normalize attribute "%s" because injected serializer is not a normalizer', $attributeName));
} }
$attributeValue = $this->serializer->normalize($attributeValue, $format, $context); $attributeValue = $this->serializer->normalize($attributeValue, $format, $context);

View File

@ -348,7 +348,7 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @expectedException \LogicException * @expectedException \Symfony\Component\Serializer\Exception\LogicException
* @expectedExceptionMessage Cannot normalize attribute "object" because injected serializer is not a normalizer * @expectedExceptionMessage Cannot normalize attribute "object" because injected serializer is not a normalizer
*/ */
public function testUnableToNormalizeObjectAttribute() public function testUnableToNormalizeObjectAttribute()