[TASK] PHP7 compliance - part 1
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Validation / AlphabeticValidator.php
1 <?php
2 namespace TYPO3\CMS\Form\Validation;
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 * Alphabetic rule
19 */
20 class AlphabeticValidator extends \TYPO3\CMS\Form\Validation\AbstractValidator {
21
22 /**
23 * Constant for localisation
24 *
25 * @var string
26 */
27 const LOCALISATION_OBJECT_NAME = 'tx_form_system_validate_alphabetic';
28
29 /**
30 * Allow white space in the submitted value
31 *
32 * @var bool
33 */
34 protected $allowWhiteSpace;
35
36 /**
37 * Alphabetic filter used for validation
38 *
39 * @var \TYPO3\CMS\Form\Filter\AlphabeticFilter
40 */
41 protected $filter;
42
43 /**
44 * Constructor
45 *
46 * @param array $arguments Typoscript configuration
47 */
48 public function __construct($arguments = array()) {
49 $this->setAllowWhiteSpace($arguments['allowWhiteSpace']);
50 parent::__construct($arguments);
51 }
52
53 /**
54 * Returns TRUE if submitted value validates according to rule
55 *
56 * @return bool
57 * @see \TYPO3\CMS\Form\Validation\ValidatorInterface::isValid()
58 */
59 public function isValid() {
60 if ($this->requestHandler->has($this->fieldName)) {
61 $value = $this->requestHandler->getByMethod($this->fieldName);
62 if ($this->filter === NULL) {
63 $this->filter = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Form\Filter\AlphabeticFilter::class);
64 }
65 $this->filter->setAllowWhiteSpace($this->allowWhiteSpace);
66 if ($this->filter->filter($value) !== $value) {
67 return FALSE;
68 }
69 }
70 return TRUE;
71 }
72
73 /**
74 * Set TRUE if white space is allowed in submitted value
75 *
76 * @param bool $allowWhiteSpace TRUE if white space allowed
77 * @return AlphabeticValidator Rule object
78 */
79 public function setAllowWhiteSpace($allowWhiteSpace) {
80 if ($allowWhiteSpace === NULL) {
81 $this->allowWhiteSpace = FALSE;
82 } else {
83 $this->allowWhiteSpace = (bool)$allowWhiteSpace;
84 }
85 return $this;
86 }
87
88 /**
89 * Get the local language label(s) for the message
90 * Overrides the abstract
91 *
92 * @param string $type
93 * @return string The local language message label
94 * @see \TYPO3\CMS\Form\Validation\AbstractValidator::_getLocalLanguageLabel()
95 */
96 protected function getLocalLanguageLabel($type = '') {
97 $label = static::LOCALISATION_OBJECT_NAME . '.message';
98 $messages[] = $this->localizationHandler->getLocalLanguageLabel($label);
99 if ($this->allowWhiteSpace) {
100 $messages[] = $this->localizationHandler->getLocalLanguageLabel($label . '2');
101 }
102 $message = implode(', ', $messages);
103 return $message;
104 }
105
106 }