Callback.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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\Validator\Constraints;
  11. use Symfony\Component\Validator\Constraint;
  12. /**
  13. * @Annotation
  14. * @Target({"CLASS", "PROPERTY", "METHOD", "ANNOTATION"})
  15. *
  16. * @author Bernhard Schussek <bschussek@gmail.com>
  17. */
  18. #[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
  19. class Callback extends Constraint
  20. {
  21. /**
  22. * @var string|callable
  23. */
  24. public $callback;
  25. /**
  26. * {@inheritdoc}
  27. *
  28. * @param array|string|callable $callback The callback or a set of options
  29. */
  30. public function __construct($callback = null, array $groups = null, $payload = null, array $options = [])
  31. {
  32. // Invocation through annotations with an array parameter only
  33. if (\is_array($callback) && 1 === \count($callback) && isset($callback['value'])) {
  34. $callback = $callback['value'];
  35. }
  36. if (!\is_array($callback) || (!isset($callback['callback']) && !isset($callback['groups']) && !isset($callback['payload']))) {
  37. $options['callback'] = $callback;
  38. } else {
  39. $options = array_merge($callback, $options);
  40. }
  41. parent::__construct($options, $groups, $payload);
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function getDefaultOption()
  47. {
  48. return 'callback';
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function getTargets()
  54. {
  55. return [self::CLASS_CONSTRAINT, self::PROPERTY_CONSTRAINT];
  56. }
  57. }