TYPO3 CMS  TYPO3_7-6
BooleanValidator.php
Go to the documentation of this file.
1 <?php
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 
21 {
25  protected $supportedOptions = [
26  // The default is set to NULL here, because we need to be backward compatible here, because this
27  // BooleanValidator is called automatically on boolean action arguments. If we would set it to TRUE,
28  // every FALSE value for an action argument would break.
29  // @todo with next patches: deprecate this BooleanValidator and introduce a BooleanValueValidator, like
30  // in Flow, which won't be called on boolean action arguments.
31  'is' => [null, 'Boolean value', 'boolean|string|integer']
32  ];
33 
44  public function isValid($value)
45  {
46  // see comment above, check if expectation is NULL, then nothing to do!
47  if ($this->options['is'] === null) {
48  return;
49  }
50  switch (strtolower((string)$this->options['is'])) {
51  case 'true':
52  case '1':
53  $expectation = true;
54  break;
55  case 'false':
56  case '':
57  case '0':
58  $expectation = false;
59  break;
60  default:
61  $this->addError('The given expectation is not valid.', 1361959227);
62  return;
63  }
64 
65  if ($value !== $expectation) {
66  if (!is_bool($value)) {
67  $this->addError($this->translateErrorMessage(
68  'validator.boolean.nottrue',
69  'extbase'
70  ), 1361959230);
71  } else {
72  if ($expectation) {
73  $this->addError($this->translateErrorMessage(
74  'validator.boolean.nottrue',
75  'extbase'
76  ), 1361959228);
77  } else {
78  $this->addError($this->translateErrorMessage(
79  'validator.boolean.notfalse',
80  'extbase'
81  ), 1361959229);
82  }
83  }
84  }
85  }
86 }
addError($message, $code, array $arguments=[], $title='')
translateErrorMessage($translateKey, $extensionName, $arguments=[])