Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Validator\Constraints; Chris@0: Chris@0: use Symfony\Component\Validator\Exception\ConstraintDefinitionException; Chris@0: Chris@0: /** Chris@0: * @Annotation Chris@0: * @Target({"PROPERTY", "METHOD", "ANNOTATION"}) Chris@0: * Chris@0: * @author Bernhard Schussek Chris@0: */ Chris@0: class Collection extends Composite Chris@0: { Chris@0: const MISSING_FIELD_ERROR = '2fa2158c-2a7f-484b-98aa-975522539ff8'; Chris@0: const NO_SUCH_FIELD_ERROR = '7703c766-b5d5-4cef-ace7-ae0dd82304e9'; Chris@0: Chris@17: protected static $errorNames = [ Chris@0: self::MISSING_FIELD_ERROR => 'MISSING_FIELD_ERROR', Chris@0: self::NO_SUCH_FIELD_ERROR => 'NO_SUCH_FIELD_ERROR', Chris@17: ]; Chris@0: Chris@17: public $fields = []; Chris@0: public $allowExtraFields = false; Chris@0: public $allowMissingFields = false; Chris@0: public $extraFieldsMessage = 'This field was not expected.'; Chris@0: public $missingFieldsMessage = 'This field is missing.'; Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function __construct($options = null) Chris@0: { Chris@0: // no known options set? $options is the fields array Chris@17: if (\is_array($options) Chris@17: && !array_intersect(array_keys($options), ['groups', 'fields', 'allowExtraFields', 'allowMissingFields', 'extraFieldsMessage', 'missingFieldsMessage'])) { Chris@17: $options = ['fields' => $options]; Chris@0: } Chris@0: Chris@0: parent::__construct($options); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function initializeNestedConstraints() Chris@0: { Chris@0: parent::initializeNestedConstraints(); Chris@0: Chris@17: if (!\is_array($this->fields)) { Chris@0: throw new ConstraintDefinitionException(sprintf('The option "fields" is expected to be an array in constraint %s', __CLASS__)); Chris@0: } Chris@0: Chris@0: foreach ($this->fields as $fieldName => $field) { Chris@0: // the XmlFileLoader and YamlFileLoader pass the field Optional Chris@0: // and Required constraint as an array with exactly one element Chris@17: if (\is_array($field) && 1 == \count($field)) { Chris@0: $this->fields[$fieldName] = $field = $field[0]; Chris@0: } Chris@0: Chris@0: if (!$field instanceof Optional && !$field instanceof Required) { Chris@18: $this->fields[$fieldName] = new Required($field); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: public function getRequiredOptions() Chris@0: { Chris@17: return ['fields']; Chris@0: } Chris@0: Chris@0: protected function getCompositeOption() Chris@0: { Chris@0: return 'fields'; Chris@0: } Chris@0: }