2010-06-24 09:40:05 +01:00
|
|
|
<?php
|
|
|
|
|
2010-10-02 11:42:31 +01:00
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2010-10-02 11:42:31 +01:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-10-02 11:42:31 +01:00
|
|
|
*
|
2011-01-15 13:29:43 +00:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2010-10-02 11:42:31 +01:00
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\Validator\Constraints;
|
|
|
|
|
2012-07-11 17:19:09 +01:00
|
|
|
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
|
2011-05-19 08:34:54 +01:00
|
|
|
|
2011-07-20 09:37:57 +01:00
|
|
|
/**
|
|
|
|
* @Annotation
|
2014-04-23 11:57:22 +01:00
|
|
|
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
|
2011-07-20 09:37:57 +01:00
|
|
|
*
|
2013-04-17 17:02:12 +01:00
|
|
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
2011-07-20 09:37:57 +01:00
|
|
|
*/
|
2014-07-29 11:16:36 +01:00
|
|
|
class Collection extends Composite
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2014-11-03 10:15:16 +00:00
|
|
|
const MISSING_FIELD_ERROR = '2fa2158c-2a7f-484b-98aa-975522539ff8';
|
|
|
|
const NO_SUCH_FIELD_ERROR = '7703c766-b5d5-4cef-ace7-ae0dd82304e9';
|
2014-09-24 11:08:18 +01:00
|
|
|
|
|
|
|
protected static $errorNames = array(
|
|
|
|
self::MISSING_FIELD_ERROR => 'MISSING_FIELD_ERROR',
|
|
|
|
self::NO_SUCH_FIELD_ERROR => 'NO_SUCH_FIELD_ERROR',
|
|
|
|
);
|
|
|
|
|
2013-11-10 17:06:47 +00:00
|
|
|
public $fields = array();
|
2010-06-24 10:24:08 +01:00
|
|
|
public $allowExtraFields = false;
|
|
|
|
public $allowMissingFields = false;
|
2012-04-13 15:47:58 +01:00
|
|
|
public $extraFieldsMessage = 'This field was not expected.';
|
|
|
|
public $missingFieldsMessage = 'This field is missing.';
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2011-03-31 14:36:04 +01:00
|
|
|
/**
|
2014-04-15 06:57:34 +01:00
|
|
|
* {@inheritdoc}
|
2011-03-31 14:36:04 +01:00
|
|
|
*/
|
|
|
|
public function __construct($options = null)
|
|
|
|
{
|
|
|
|
// no known options set? $options is the fields array
|
|
|
|
if (is_array($options)
|
|
|
|
&& !array_intersect(array_keys($options), array('groups', 'fields', 'allowExtraFields', 'allowMissingFields', 'extraFieldsMessage', 'missingFieldsMessage'))) {
|
|
|
|
$options = array('fields' => $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::__construct($options);
|
2014-07-29 11:16:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
protected function initializeNestedConstraints()
|
|
|
|
{
|
|
|
|
parent::initializeNestedConstraints();
|
2012-07-11 17:19:09 +01:00
|
|
|
|
|
|
|
if (!is_array($this->fields)) {
|
2013-04-10 11:24:37 +01:00
|
|
|
throw new ConstraintDefinitionException(sprintf('The option "fields" is expected to be an array in constraint %s', __CLASS__));
|
2012-07-11 17:19:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->fields as $fieldName => $field) {
|
2013-05-04 12:44:31 +01:00
|
|
|
// the XmlFileLoader and YamlFileLoader pass the field Optional
|
|
|
|
// and Required constraint as an array with exactly one element
|
|
|
|
if (is_array($field) && count($field) == 1) {
|
|
|
|
$this->fields[$fieldName] = $field = $field[0];
|
|
|
|
}
|
|
|
|
|
2012-07-11 17:19:09 +01:00
|
|
|
if (!$field instanceof Optional && !$field instanceof Required) {
|
|
|
|
$this->fields[$fieldName] = $field = new Required($field);
|
|
|
|
}
|
|
|
|
}
|
2011-03-31 14:36:04 +01:00
|
|
|
}
|
|
|
|
|
2011-03-30 22:18:15 +01:00
|
|
|
public function getRequiredOptions()
|
2010-06-24 10:24:08 +01:00
|
|
|
{
|
|
|
|
return array('fields');
|
|
|
|
}
|
2014-07-29 11:16:36 +01:00
|
|
|
|
|
|
|
protected function getCompositeOption()
|
|
|
|
{
|
|
|
|
return 'fields';
|
|
|
|
}
|
2011-06-08 11:16:48 +01:00
|
|
|
}
|