TYPO3  7.6
LengthValidator.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Form\Domain\Validator;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
18 {
24  protected $charsetConverter = null;
25 
30  public function injectCharsetConverter(\TYPO3\CMS\Core\Charset\CharsetConverter $charsetConverter)
31  {
32  $this->charsetConverter = $charsetConverter;
33  }
34 
38  protected $supportedOptions = array(
39  'element' => array('', 'The name of the element', 'string', true),
40  'errorMessage' => array('', 'The error message', 'array', true),
41  'minimum' => array('', 'The minimum value', 'integer', true),
42  'maximum' => array('', 'The maximum value', 'integer', false),
43  );
44 
50  const LOCALISATION_OBJECT_NAME = 'tx_form_system_validate_length';
51 
59  public function isValid($value)
60  {
61  $length = $this->charsetConverter->strlen('utf-8', $value);
62  if ($length < (int)$this->options['minimum']) {
63  $this->addError(
64  $this->renderMessage(
65  $this->options['errorMessage'][0],
66  $this->options['errorMessage'][1],
67  'error'
68  ),
69  1441999425
70  );
71  return;
72  }
73  if (
74  !isset($this->options['maximum'])
75  || $this->options['maximum'] === ''
76  ) {
77  $this->options['maximum'] = null;
78  }
79  if (
80  $this->options['maximum'] !== null
81  && $length > (int)$this->options['maximum']
82  ) {
83  $this->addError(
84  $this->renderMessage(
85  $this->options['errorMessage'][0],
86  $this->options['errorMessage'][1],
87  'error'
88  ),
89  1441999425
90  );
91  }
92  }
93 
102  public function getLocalLanguageLabel($type = '')
103  {
104  $label = static::LOCALISATION_OBJECT_NAME . '.' . $type;
105  $messages[] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($label, 'form');
106  if ($this->options['maximum'] !== null) {
107  $messages[] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($label . 2, 'form');
108  }
109  $message = implode(', ', $messages);
110  return $message;
111  }
112 
120  public function substituteMarkers($message)
121  {
122  return str_replace(
123  array('%minimum', '%maximum'),
124  array($this->options['minimum'], $this->options['maximum']),
125  $message
126  );
127  }
128 }