MaxDepth.php 1.3 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 @MaxDepth().
  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 MaxDepth
  22. {
  23. /**
  24. * @var int
  25. */
  26. private $maxDepth;
  27. /**
  28. * @param int|array $maxDepth
  29. */
  30. public function __construct($maxDepth)
  31. {
  32. if (\is_array($maxDepth)) {
  33. if (!isset($maxDepth['value'])) {
  34. throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" should be set.', static::class));
  35. }
  36. $maxDepth = $maxDepth['value'];
  37. }
  38. if (!\is_int($maxDepth) || $maxDepth <= 0) {
  39. throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be a positive integer.', static::class));
  40. }
  41. $this->maxDepth = $maxDepth;
  42. }
  43. public function getMaxDepth()
  44. {
  45. return $this->maxDepth;
  46. }
  47. }