|
1 <?php |
|
2 |
|
3 /* |
|
4 * This file is part of the Symfony package. |
|
5 * |
|
6 * (c) Fabien Potencier <fabien@symfony.com> |
|
7 * |
|
8 * For the full copyright and license information, please view the LICENSE |
|
9 * file that was distributed with this source code. |
|
10 */ |
|
11 |
|
12 namespace Symfony\Component\Validator\Mapping; |
|
13 |
|
14 use Symfony\Component\Validator\Constraint; |
|
15 |
|
16 abstract class ElementMetadata |
|
17 { |
|
18 public $constraints = array(); |
|
19 public $constraintsByGroup = array(); |
|
20 |
|
21 /** |
|
22 * Returns the names of the properties that should be serialized. |
|
23 * |
|
24 * @return array |
|
25 */ |
|
26 public function __sleep() |
|
27 { |
|
28 return array( |
|
29 'constraints', |
|
30 'constraintsByGroup', |
|
31 ); |
|
32 } |
|
33 |
|
34 /** |
|
35 * Clones this object. |
|
36 */ |
|
37 public function __clone() |
|
38 { |
|
39 $constraints = $this->constraints; |
|
40 |
|
41 $this->constraints = array(); |
|
42 $this->constraintsByGroup = array(); |
|
43 |
|
44 foreach ($constraints as $constraint) { |
|
45 $this->addConstraint(clone $constraint); |
|
46 } |
|
47 } |
|
48 |
|
49 /** |
|
50 * Adds a constraint to this element. |
|
51 * |
|
52 * @param Constraint $constraint |
|
53 */ |
|
54 public function addConstraint(Constraint $constraint) |
|
55 { |
|
56 $this->constraints[] = $constraint; |
|
57 |
|
58 foreach ($constraint->groups as $group) { |
|
59 $this->constraintsByGroup[$group][] = $constraint; |
|
60 } |
|
61 |
|
62 return $this; |
|
63 } |
|
64 |
|
65 /** |
|
66 * Returns all constraints of this element. |
|
67 * |
|
68 * @return array An array of Constraint instances |
|
69 */ |
|
70 public function getConstraints() |
|
71 { |
|
72 return $this->constraints; |
|
73 } |
|
74 |
|
75 /** |
|
76 * Returns whether this element has any constraints. |
|
77 * |
|
78 * @return Boolean |
|
79 */ |
|
80 public function hasConstraints() |
|
81 { |
|
82 return count($this->constraints) > 0; |
|
83 } |
|
84 |
|
85 /** |
|
86 * Returns the constraints of the given group and global ones (* group). |
|
87 * |
|
88 * @param string $group The group name |
|
89 * |
|
90 * @return array An array with all Constraint instances belonging to the group |
|
91 */ |
|
92 public function findConstraints($group) |
|
93 { |
|
94 return isset($this->constraintsByGroup[$group]) |
|
95 ? $this->constraintsByGroup[$group] |
|
96 : array(); |
|
97 } |
|
98 } |