[CLEANUP] The correct case must be used for standard PHP types in phpdoc
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Validation / Validator / AbstractValidator.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 use TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException;
18
19 /**
20 * Abstract validator
21 */
22 abstract class AbstractValidator implements ValidatorInterface
23 {
24 /**
25 * Specifies whether this validator accepts empty values.
26 *
27 * If this is TRUE, the validators isValid() method is not called in case of an empty value
28 * Note: A value is considered empty if it is NULL or an empty string!
29 * By default all validators except for NotEmpty and the Composite Validators accept empty values
30 *
31 * @var bool
32 */
33 protected $acceptsEmptyValues = true;
34
35 /**
36 * This contains the supported options, their default values, types and descriptions.
37 *
38 * @var array
39 */
40 protected $supportedOptions = [];
41
42 /**
43 * @var array
44 */
45 protected $options = [];
46
47 /**
48 * @var \TYPO3\CMS\Extbase\Error\Result
49 */
50 protected $result;
51
52 /**
53 * Constructs the validator and sets validation options
54 *
55 * @param array $options Options for the validator
56 * @throws InvalidValidationOptionsException
57 * @api
58 */
59 public function __construct(array $options = [])
60 {
61 // check for options given but not supported
62 if (($unsupportedOptions = array_diff_key($options, $this->supportedOptions)) !== []) {
63 throw new InvalidValidationOptionsException('Unsupported validation option(s) found: ' . implode(', ', array_keys($unsupportedOptions)), 1379981890);
64 }
65
66 // check for required options being set
67 array_walk(
68 $this->supportedOptions,
69 function ($supportedOptionData, $supportedOptionName, $options) {
70 if (isset($supportedOptionData[3]) && $supportedOptionData[3] === true && !array_key_exists($supportedOptionName, $options)) {
71 throw new InvalidValidationOptionsException('Required validation option not set: ' . $supportedOptionName, 1379981891);
72 }
73 },
74 $options
75 );
76
77 // merge with default values
78 $this->options = array_merge(
79 array_map(
80 function ($value) {
81 return $value[0];
82 },
83 $this->supportedOptions
84 ),
85 $options
86 );
87 }
88
89 /**
90 * Checks if the given value is valid according to the validator, and returns
91 * the error messages object which occurred.
92 *
93 * @param mixed $value The value that should be validated
94 * @return \TYPO3\CMS\Extbase\Error\Result
95 * @api
96 */
97 public function validate($value)
98 {
99 $this->result = new \TYPO3\CMS\Extbase\Error\Result();
100 if ($this->acceptsEmptyValues === false || $this->isEmpty($value) === false) {
101 $this->isValid($value);
102 }
103 return $this->result;
104 }
105
106 /**
107 * Check if $value is valid. If it is not valid, needs to add an error
108 * to result.
109 *
110 * @param mixed $value
111 */
112 abstract protected function isValid($value);
113
114 /**
115 * Creates a new validation error object and adds it to $this->results
116 *
117 * @param string $message The error message
118 * @param int $code The error code (a unix timestamp)
119 * @param array $arguments Arguments to be replaced in message
120 * @param string $title title of the error
121 */
122 protected function addError($message, $code, array $arguments = [], $title = '')
123 {
124 $this->result->addError(new \TYPO3\CMS\Extbase\Validation\Error($message, $code, $arguments, $title));
125 }
126
127 /**
128 * Returns the options of this validator
129 *
130 * @return array
131 */
132 public function getOptions()
133 {
134 return $this->options;
135 }
136
137 /**
138 * @param mixed $value
139 * @return bool TRUE if the given $value is NULL or an empty string ('')
140 */
141 final protected function isEmpty($value)
142 {
143 return $value === null || $value === '';
144 }
145
146 /**
147 * Wrap static call to LocalizationUtility to simplify unit testing
148 *
149 * @param string $translateKey
150 * @param string $extensionName
151 * @param array $arguments
152 *
153 * @return null|string
154 */
155 protected function translateErrorMessage($translateKey, $extensionName, $arguments = [])
156 {
157 return \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate(
158 $translateKey,
159 $extensionName,
160 $arguments
161 );
162 }
163 }