DiscriminatorMap.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 @DiscriminatorMap().
  14. *
  15. * @Annotation
  16. * @Target({"CLASS"})
  17. *
  18. * @author Samuel Roze <samuel.roze@gmail.com>
  19. */
  20. #[\Attribute(\Attribute::TARGET_CLASS)]
  21. class DiscriminatorMap
  22. {
  23. /**
  24. * @var string
  25. */
  26. private $typeProperty;
  27. /**
  28. * @var array
  29. */
  30. private $mapping;
  31. /**
  32. * @param string|array $typeProperty
  33. *
  34. * @throws InvalidArgumentException
  35. */
  36. public function __construct($typeProperty, array $mapping = null)
  37. {
  38. if (\is_array($typeProperty)) {
  39. $mapping = $typeProperty['mapping'] ?? null;
  40. $typeProperty = $typeProperty['typeProperty'] ?? null;
  41. } elseif (!\is_string($typeProperty)) {
  42. throw new \TypeError(sprintf('"%s": Argument $typeProperty was expected to be a string or array, got "%s".', __METHOD__, get_debug_type($typeProperty)));
  43. }
  44. if (empty($typeProperty)) {
  45. throw new InvalidArgumentException(sprintf('Parameter "typeProperty" of annotation "%s" cannot be empty.', static::class));
  46. }
  47. if (empty($mapping)) {
  48. throw new InvalidArgumentException(sprintf('Parameter "mapping" of annotation "%s" cannot be empty.', static::class));
  49. }
  50. $this->typeProperty = $typeProperty;
  51. $this->mapping = $mapping;
  52. }
  53. public function getTypeProperty(): string
  54. {
  55. return $this->typeProperty;
  56. }
  57. public function getMapping(): array
  58. {
  59. return $this->mapping;
  60. }
  61. }