SerializedName.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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\Annotation;
  11. use Symfony\Component\Serializer\Exception\InvalidArgumentException;
  12. /**
  13. * Annotation class for @SerializedName().
  14. *
  15. * @Annotation
  16. * @Target({"PROPERTY", "METHOD"})
  17. *
  18. * @author Fabien Bourigault <bourigaultfabien@gmail.com>
  19. */
  20. #[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)]
  21. final class SerializedName
  22. {
  23. /**
  24. * @var string
  25. */
  26. private $serializedName;
  27. /**
  28. * @param string|array $serializedName
  29. */
  30. public function __construct($serializedName)
  31. {
  32. if (\is_array($serializedName)) {
  33. if (!isset($serializedName['value'])) {
  34. throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" should be set.', static::class));
  35. }
  36. $serializedName = $serializedName['value'];
  37. }
  38. if (!\is_string($serializedName) || empty($serializedName)) {
  39. throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be a non-empty string.', static::class));
  40. }
  41. $this->serializedName = $serializedName;
  42. }
  43. public function getSerializedName(): string
  44. {
  45. return $this->serializedName;
  46. }
  47. }