TYPO3  7.6
BooleanValidator.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Extbase\Validation\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 
21 {
25  protected $supportedOptions = array(
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' => array(null, 'Boolean value', 'boolean|string|integer')
32  );
33 
34 
45  public function isValid($value)
46  {
47  // see comment above, check if expectation is NULL, then nothing to do!
48  if ($this->options['is'] === null) {
49  return;
50  }
51  switch (strtolower((string)$this->options['is'])) {
52  case 'true':
53  case '1':
54  $expectation = true;
55  break;
56  case 'false':
57  case '':
58  case '0':
59  $expectation = false;
60  break;
61  default:
62  $this->addError('The given expectation is not valid.', 1361959227);
63  return;
64  }
65 
66  if ($value !== $expectation) {
67  if (!is_bool($value)) {
68  $this->addError($this->translateErrorMessage(
69  'validator.boolean.nottrue',
70  'extbase'
71  ), 1361959230);
72  } else {
73  if ($expectation) {
74  $this->addError($this->translateErrorMessage(
75  'validator.boolean.nottrue',
76  'extbase'
77  ), 1361959228);
78  } else {
79  $this->addError($this->translateErrorMessage(
80  'validator.boolean.notfalse',
81  'extbase'
82  ), 1361959229);
83  }
84  }
85  }
86  }
87 }