Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Validator\Constraints;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\Validator\Constraint;
|
Chris@0
|
15 use Symfony\Component\Validator\Exception\MissingOptionsException;
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * @Annotation
|
Chris@0
|
19 * @Target({"PROPERTY", "METHOD", "ANNOTATION"})
|
Chris@0
|
20 *
|
Chris@0
|
21 * @author Bernhard Schussek <bschussek@gmail.com>
|
Chris@0
|
22 */
|
Chris@0
|
23 class Count extends Constraint
|
Chris@0
|
24 {
|
Chris@0
|
25 const TOO_FEW_ERROR = 'bef8e338-6ae5-4caf-b8e2-50e7b0579e69';
|
Chris@0
|
26 const TOO_MANY_ERROR = '756b1212-697c-468d-a9ad-50dd783bb169';
|
Chris@0
|
27
|
Chris@17
|
28 protected static $errorNames = [
|
Chris@0
|
29 self::TOO_FEW_ERROR => 'TOO_FEW_ERROR',
|
Chris@0
|
30 self::TOO_MANY_ERROR => 'TOO_MANY_ERROR',
|
Chris@17
|
31 ];
|
Chris@0
|
32
|
Chris@0
|
33 public $minMessage = 'This collection should contain {{ limit }} element or more.|This collection should contain {{ limit }} elements or more.';
|
Chris@0
|
34 public $maxMessage = 'This collection should contain {{ limit }} element or less.|This collection should contain {{ limit }} elements or less.';
|
Chris@0
|
35 public $exactMessage = 'This collection should contain exactly {{ limit }} element.|This collection should contain exactly {{ limit }} elements.';
|
Chris@0
|
36 public $min;
|
Chris@0
|
37 public $max;
|
Chris@0
|
38
|
Chris@0
|
39 public function __construct($options = null)
|
Chris@0
|
40 {
|
Chris@17
|
41 if (null !== $options && !\is_array($options)) {
|
Chris@17
|
42 $options = [
|
Chris@0
|
43 'min' => $options,
|
Chris@0
|
44 'max' => $options,
|
Chris@17
|
45 ];
|
Chris@18
|
46 } elseif (\is_array($options) && isset($options['value']) && !isset($options['min']) && !isset($options['max'])) {
|
Chris@18
|
47 $options['min'] = $options['max'] = $options['value'];
|
Chris@18
|
48 unset($options['value']);
|
Chris@0
|
49 }
|
Chris@0
|
50
|
Chris@0
|
51 parent::__construct($options);
|
Chris@0
|
52
|
Chris@0
|
53 if (null === $this->min && null === $this->max) {
|
Chris@17
|
54 throw new MissingOptionsException(sprintf('Either option "min" or "max" must be given for constraint %s', __CLASS__), ['min', 'max']);
|
Chris@0
|
55 }
|
Chris@0
|
56 }
|
Chris@0
|
57 }
|