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