Groups.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 @Groups().
  14. *
  15. * @Annotation
  16. * @Target({"PROPERTY", "METHOD"})
  17. *
  18. * @author Kévin Dunglas <dunglas@gmail.com>
  19. */
  20. #[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY)]
  21. class Groups
  22. {
  23. /**
  24. * @var string[]
  25. */
  26. private $groups;
  27. /**
  28. * @throws InvalidArgumentException
  29. */
  30. public function __construct(array $groups)
  31. {
  32. if (isset($groups['value'])) {
  33. $groups = (array) $groups['value'];
  34. }
  35. if (empty($groups)) {
  36. throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" cannot be empty.', static::class));
  37. }
  38. foreach ($groups as $group) {
  39. if (!\is_string($group)) {
  40. throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be a string or an array of strings.', static::class));
  41. }
  42. }
  43. $this->groups = $groups;
  44. }
  45. /**
  46. * Gets groups.
  47. *
  48. * @return string[]
  49. */
  50. public function getGroups()
  51. {
  52. return $this->groups;
  53. }
  54. }