TYPO3 CMS  TYPO3_6-2
LengthValidator.php
Go to the documentation of this file.
1 <?php
3 
23 
29  const LOCALISATION_OBJECT_NAME = 'tx_form_system_validate_length';
30 
36  protected $minimum;
37 
43  protected $maximum;
44 
50  protected $charsetConverter = NULL;
51 
57  public function __construct($arguments) {
58  $this->charsetConverter = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Charset\\CharsetConverter');
59  $this->setMinimum($arguments['minimum'])->setMaximum($arguments['maximum']);
60  parent::__construct($arguments);
61  }
62 
69  public function isValid() {
70  if ($this->requestHandler->has($this->fieldName)) {
71  $value = $this->requestHandler->getByMethod($this->fieldName);
72  $length = $this->charsetConverter->strlen('utf-8', $value);
73  if ($length < $this->minimum) {
74  return FALSE;
75  }
76  if ($this->maximum !== NULL && $length > $this->maximum) {
77  return FALSE;
78  }
79  }
80  return TRUE;
81  }
82 
89  public function setMinimum($minimum) {
90  $this->minimum = (int)$minimum;
91  return $this;
92  }
93 
100  public function setMaximum($maximum) {
101  if (empty($maximum)) {
102  $this->maximum = NULL;
103  } else {
104  $this->maximum = (int)$maximum;
105  }
106  return $this;
107  }
108 
117  protected function getLocalLanguageLabel($type) {
118  $label = static::LOCALISATION_OBJECT_NAME . '.' . $type;
119  $messages[] = $this->localizationHandler->getLocalLanguageLabel($label);
120  if ($this->maximum !== NULL) {
121  $messages[] = $this->localizationHandler->getLocalLanguageLabel($label . '2');
122  }
123  $message = implode(', ', $messages);
124  return $message;
125  }
126 
134  protected function substituteValues($message) {
135  return str_replace(
136  array('%minimum', '%maximum'),
137  array($this->minimum, $this->maximum),
138  $message
139  );
140  }
141 
142 }