2b5679b22d07e8ba84b9ae70fef2c5effcfbc91c
[yaffs-website] / vendor / symfony / validator / Constraints / Collection.php
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\Constraints;
13
14 use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
15
16 /**
17  * @Annotation
18  * @Target({"PROPERTY", "METHOD", "ANNOTATION"})
19  *
20  * @author Bernhard Schussek <bschussek@gmail.com>
21  */
22 class Collection extends Composite
23 {
24     const MISSING_FIELD_ERROR = '2fa2158c-2a7f-484b-98aa-975522539ff8';
25     const NO_SUCH_FIELD_ERROR = '7703c766-b5d5-4cef-ace7-ae0dd82304e9';
26
27     protected static $errorNames = array(
28         self::MISSING_FIELD_ERROR => 'MISSING_FIELD_ERROR',
29         self::NO_SUCH_FIELD_ERROR => 'NO_SUCH_FIELD_ERROR',
30     );
31
32     public $fields = array();
33     public $allowExtraFields = false;
34     public $allowMissingFields = false;
35     public $extraFieldsMessage = 'This field was not expected.';
36     public $missingFieldsMessage = 'This field is missing.';
37
38     /**
39      * {@inheritdoc}
40      */
41     public function __construct($options = null)
42     {
43         // no known options set? $options is the fields array
44         if (\is_array($options)
45             && !array_intersect(array_keys($options), array('groups', 'fields', 'allowExtraFields', 'allowMissingFields', 'extraFieldsMessage', 'missingFieldsMessage'))) {
46             $options = array('fields' => $options);
47         }
48
49         parent::__construct($options);
50     }
51
52     /**
53      * {@inheritdoc}
54      */
55     protected function initializeNestedConstraints()
56     {
57         parent::initializeNestedConstraints();
58
59         if (!\is_array($this->fields)) {
60             throw new ConstraintDefinitionException(sprintf('The option "fields" is expected to be an array in constraint %s', __CLASS__));
61         }
62
63         foreach ($this->fields as $fieldName => $field) {
64             // the XmlFileLoader and YamlFileLoader pass the field Optional
65             // and Required constraint as an array with exactly one element
66             if (\is_array($field) && 1 == \count($field)) {
67                 $this->fields[$fieldName] = $field = $field[0];
68             }
69
70             if (!$field instanceof Optional && !$field instanceof Required) {
71                 $this->fields[$fieldName] = $field = new Required($field);
72             }
73         }
74     }
75
76     public function getRequiredOptions()
77     {
78         return array('fields');
79     }
80
81     protected function getCompositeOption()
82     {
83         return 'fields';
84     }
85 }