2012-07-11 18:08:03 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Component\Validator\Constraints;
|
|
|
|
|
|
|
|
use Symfony\Component\Validator\Constraint;
|
|
|
|
use Symfony\Component\Validator\Exception\MissingOptionsException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Annotation
|
2014-04-23 11:57:22 +01:00
|
|
|
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
|
2012-07-11 18:08:03 +01:00
|
|
|
*
|
2013-04-17 17:02:12 +01:00
|
|
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
2012-07-11 18:08:03 +01:00
|
|
|
*/
|
|
|
|
class Count extends Constraint
|
|
|
|
{
|
2014-11-03 10:15:16 +00:00
|
|
|
const TOO_FEW_ERROR = 'bef8e338-6ae5-4caf-b8e2-50e7b0579e69';
|
|
|
|
const TOO_MANY_ERROR = '756b1212-697c-468d-a9ad-50dd783bb169';
|
2014-09-24 11:08:18 +01:00
|
|
|
|
2019-01-16 09:39:14 +00:00
|
|
|
protected static $errorNames = [
|
2014-09-24 11:08:18 +01:00
|
|
|
self::TOO_FEW_ERROR => 'TOO_FEW_ERROR',
|
|
|
|
self::TOO_MANY_ERROR => 'TOO_MANY_ERROR',
|
2019-01-16 09:39:14 +00:00
|
|
|
];
|
2014-09-24 11:08:18 +01:00
|
|
|
|
2012-12-03 15:49:07 +00:00
|
|
|
public $minMessage = 'This collection should contain {{ limit }} element or more.|This collection should contain {{ limit }} elements or more.';
|
|
|
|
public $maxMessage = 'This collection should contain {{ limit }} element or less.|This collection should contain {{ limit }} elements or less.';
|
|
|
|
public $exactMessage = 'This collection should contain exactly {{ limit }} element.|This collection should contain exactly {{ limit }} elements.';
|
2012-07-11 18:08:03 +01:00
|
|
|
public $min;
|
|
|
|
public $max;
|
|
|
|
|
|
|
|
public function __construct($options = null)
|
|
|
|
{
|
2018-07-05 12:24:53 +01:00
|
|
|
if (null !== $options && !\is_array($options)) {
|
2019-01-16 09:39:14 +00:00
|
|
|
$options = [
|
2012-07-11 18:08:03 +01:00
|
|
|
'min' => $options,
|
|
|
|
'max' => $options,
|
2019-01-16 09:39:14 +00:00
|
|
|
];
|
2019-03-27 23:59:01 +00:00
|
|
|
} elseif (\is_array($options) && isset($options['value']) && !isset($options['min']) && !isset($options['max'])) {
|
|
|
|
$options['min'] = $options['max'] = $options['value'];
|
|
|
|
unset($options['value']);
|
2012-07-11 18:08:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
parent::__construct($options);
|
|
|
|
|
|
|
|
if (null === $this->min && null === $this->max) {
|
2019-01-16 09:39:14 +00:00
|
|
|
throw new MissingOptionsException(sprintf('Either option "min" or "max" must be given for constraint %s', __CLASS__), ['min', 'max']);
|
2012-07-11 18:08:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|