TYPO3 CMS  TYPO3_6-2
StringLengthValidator.php
Go to the documentation of this file.
1 <?php
3 
23 
27  protected $supportedOptions = array(
28  'minimum' => array(0, 'Minimum length for a valid string', 'integer'),
29  'maximum' => array(PHP_INT_MAX, 'Maximum length for a valid string', 'integer')
30  );
31 
42  public function isValid($value) {
43  if ($this->options['maximum'] < $this->options['minimum']) {
44  throw new \TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException('The \'maximum\' is shorter than the \'minimum\' in the StringLengthValidator.', 1238107096);
45  }
46 
47  if (is_object($value)) {
48  if (!method_exists($value, '__toString')) {
49  $this->addError('The given object could not be converted to a string.', 1238110957);
50  return;
51  }
52  } elseif (!is_string($value)) {
53  $this->addError('The given value was not a valid string.', 1269883975);
54  return;
55  }
56 
57  // TODO Use \TYPO3\CMS\Core\Charset\CharsetConverter::strlen() instead; How do we get the charset?
58  $stringLength = strlen($value);
59  $isValid = TRUE;
60  if ($stringLength < $this->options['minimum']) {
61  $isValid = FALSE;
62  }
63  if ($stringLength > $this->options['maximum']) {
64  $isValid = FALSE;
65  }
66 
67  if ($isValid === FALSE) {
68  if ($this->options['minimum'] > 0 && $this->options['maximum'] < PHP_INT_MAX) {
69  $this->addError(
70  $this->translateErrorMessage(
71  'validator.stringlength.between',
72  'extbase',
73  array (
74  $this->options['minimum'],
75  $this->options['maximum']
76  )
77  ), 1428504122, array($this->options['minimum'], $this->options['maximum']));
78  } elseif ($this->options['minimum'] > 0) {
79  $this->addError(
80  $this->translateErrorMessage(
81  'validator.stringlength.less',
82  'extbase',
83  array(
84  $this->options['minimum']
85  )
86  ), 1238108068, array($this->options['minimum']));
87  } else {
88  $this->addError(
89  $this->translateErrorMessage(
90  'validator.stringlength.exceed',
91  'extbase',
92  array(
93  $this->options['maximum']
94  )
95  ), 1238108069, array($this->options['maximum']));
96  }
97  }
98  }
99 }
addError($message, $code, array $arguments=array(), $title='')
translateErrorMessage($translateKey, $extensionName, $arguments=array())