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