[TASK] Removes extra empty lines
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Validation / Validator / BooleanValidator.php
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
17 /**
18 * Validator for boolean values
19 */
20 class BooleanValidator extends AbstractValidator
21 {
22 /**
23 * @var array
24 */
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 /**
35 * Check if $value matches the expectation given to the validator.
36 * If it does not match, the function adds an error to the result.
37 *
38 * Also testing for '1' (true), '0' and '' (false) because casting varies between
39 * tests and actual usage. This makes the validator loose but still keeping functionality.
40 *
41 * @param mixed $value The value that should be validated
42 * @return void
43 */
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 }