Drupal investigation

ArrayDenormalizer.php 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Serializer\Normalizer;
  11. use Symfony\Component\Serializer\Exception\BadMethodCallException;
  12. use Symfony\Component\Serializer\Exception\InvalidArgumentException;
  13. use Symfony\Component\Serializer\SerializerAwareInterface;
  14. use Symfony\Component\Serializer\SerializerInterface;
  15. /**
  16. * Denormalizes arrays of objects.
  17. *
  18. * @author Alexander M. Turek <me@derrabus.de>
  19. */
  20. class ArrayDenormalizer implements DenormalizerInterface, SerializerAwareInterface
  21. {
  22. /**
  23. * @var SerializerInterface|DenormalizerInterface
  24. */
  25. private $serializer;
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function denormalize($data, $class, $format = null, array $context = array())
  30. {
  31. if ($this->serializer === null) {
  32. throw new BadMethodCallException('Please set a serializer before calling denormalize()!');
  33. }
  34. if (!is_array($data)) {
  35. throw new InvalidArgumentException('Data expected to be an array, '.gettype($data).' given.');
  36. }
  37. if (substr($class, -2) !== '[]') {
  38. throw new InvalidArgumentException('Unsupported class: '.$class);
  39. }
  40. $serializer = $this->serializer;
  41. $class = substr($class, 0, -2);
  42. return array_map(
  43. function ($data) use ($serializer, $class, $format, $context) {
  44. return $serializer->denormalize($data, $class, $format, $context);
  45. },
  46. $data
  47. );
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function supportsDenormalization($data, $type, $format = null)
  53. {
  54. return substr($type, -2) === '[]'
  55. && $this->serializer->supportsDenormalization($data, substr($type, 0, -2), $format);
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function setSerializer(SerializerInterface $serializer)
  61. {
  62. if (!$serializer instanceof DenormalizerInterface) {
  63. throw new InvalidArgumentException('Expected a serializer that also implements DenormalizerInterface.');
  64. }
  65. $this->serializer = $serializer;
  66. }
  67. }