[BUGFIX] EXT:form - let the CountValidator provide its arguments
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Mvc / Validation / CountValidator.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Form\Mvc\Validation;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It originated from the Neos.Form package (www.neos.io)
9 *
10 * It is free software; you can redistribute it and/or modify it under
11 * the terms of the GNU General Public License, either version 2
12 * of the License, or any later version.
13 *
14 * For the full copyright and license information, please read the
15 * LICENSE.txt file that was distributed with this source code.
16 *
17 * The TYPO3 project - inspiring people to share!
18 */
19
20 use TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator;
21
22 /**
23 * Validator for countable types
24 *
25 * Scope: frontend
26 * @internal
27 */
28 class CountValidator extends AbstractValidator
29 {
30 /**
31 * @var array
32 */
33 protected $supportedOptions = [
34 'minimum' => [0, 'The minimum count to accept', 'integer'],
35 'maximum' => [PHP_INT_MAX, 'The maximum count to accept', 'integer']
36 ];
37
38 /**
39 * The given value is valid if it is an array or \Countable that contains the specified amount of elements.
40 *
41 * @param mixed $value
42 * @api
43 */
44 public function isValid($value)
45 {
46 if (!is_array($value) && !($value instanceof \Countable)) {
47 $this->addError(
48 $this->translateErrorMessage(
49 'validation.error.1475002976',
50 'form'
51 ),
52 1475002976
53 );
54 return;
55 }
56
57 $minimum = (int)$this->options['minimum'];
58 $maximum = (int)$this->options['maximum'];
59 if (count($value) < $minimum || count($value) > $maximum) {
60 $this->addError(
61 $this->translateErrorMessage(
62 'validation.error.1475002994',
63 'form',
64 [$minimum, $maximum]
65 ),
66 1475002994, [$this->options['minimum'], $this->options['maximum']]
67 );
68 }
69 }
70 }