GroupSequence.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. /**
  12. * A sequence of validation groups.
  13. *
  14. * When validating a group sequence, each group will only be validated if all
  15. * of the previous groups in the sequence succeeded. For example:
  16. *
  17. * $validator->validate($address, null, new GroupSequence(['Basic', 'Strict']));
  18. *
  19. * In the first step, all constraints that belong to the group "Basic" will be
  20. * validated. If none of the constraints fail, the validator will then validate
  21. * the constraints in group "Strict". This is useful, for example, if "Strict"
  22. * contains expensive checks that require a lot of CPU or slow, external
  23. * services. You usually don't want to run expensive checks if any of the cheap
  24. * checks fail.
  25. *
  26. * When adding metadata to a class, you can override the "Default" group of
  27. * that class with a group sequence:
  28. * /**
  29. * * @GroupSequence({"Address", "Strict"})
  30. * *\/
  31. * class Address
  32. * {
  33. * // ...
  34. * }
  35. *
  36. * Whenever you validate that object in the "Default" group, the group sequence
  37. * will be validated:
  38. *
  39. * $validator->validate($address);
  40. *
  41. * If you want to execute the constraints of the "Default" group for a class
  42. * with an overridden default group, pass the class name as group name instead:
  43. *
  44. * $validator->validate($address, null, "Address")
  45. *
  46. * @Annotation
  47. * @Target({"CLASS", "ANNOTATION"})
  48. *
  49. * @author Bernhard Schussek <bschussek@gmail.com>
  50. */
  51. #[\Attribute(\Attribute::TARGET_CLASS)]
  52. class GroupSequence
  53. {
  54. /**
  55. * The groups in the sequence.
  56. *
  57. * @var string[]|string[][]|GroupSequence[]
  58. */
  59. public $groups;
  60. /**
  61. * The group in which cascaded objects are validated when validating
  62. * this sequence.
  63. *
  64. * By default, cascaded objects are validated in each of the groups of
  65. * the sequence.
  66. *
  67. * If a class has a group sequence attached, that sequence replaces the
  68. * "Default" group. When validating that class in the "Default" group, the
  69. * group sequence is used instead, but still the "Default" group should be
  70. * cascaded to other objects.
  71. *
  72. * @var string|GroupSequence
  73. */
  74. public $cascadedGroup;
  75. /**
  76. * Creates a new group sequence.
  77. *
  78. * @param string[] $groups The groups in the sequence
  79. */
  80. public function __construct(array $groups)
  81. {
  82. // Support for Doctrine annotations
  83. $this->groups = $groups['value'] ?? $groups;
  84. }
  85. }