[BUGFIX] Correctly apply Extbase validator options and add tests
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Validation / Validator / AbstractValidator.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Validation\Validator;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2013 Extbase Team (http://forge.typo3.org/projects/typo3v4-mvc)
8 * Extbase is a backport of TYPO3 Flow. All credits go to the TYPO3 Flow team.
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 * A copy is found in the text file GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30 use TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException;
31
32 /**
33 * Abstract validator
34 */
35 abstract class AbstractValidator implements ValidatorInterface {
36
37 /**
38 * Specifies whether this validator accepts empty values.
39 *
40 * If this is TRUE, the validators isValid() method is not called in case of an empty value
41 * Note: A value is considered empty if it is NULL or an empty string!
42 * By default all validators except for NotEmpty and the Composite Validators accept empty values
43 *
44 * @var boolean
45 */
46 protected $acceptsEmptyValues = TRUE;
47
48 /**
49 * This contains the supported options, their default values, types and descriptions.
50 *
51 * @var array
52 */
53 protected $supportedOptions = array();
54
55 /**
56 * @var array
57 */
58 protected $options = array();
59
60 /**
61 * @var array
62 * @deprecated since Extbase 1.4.0, will be removed two versions after Extbase 6.1. You should use constructor parameter to set validation options.
63 */
64 protected $errors = array();
65
66 /**
67 * @var \TYPO3\CMS\Extbase\Error\Result
68 */
69 protected $result;
70
71 /**
72 * Constructs the validator and sets validation options
73 *
74 * @param array $options Options for the validator
75 * @throws InvalidValidationOptionsException
76 * @api
77 */
78 public function __construct(array $options = array()) {
79 // check for options given but not supported
80 if (($unsupportedOptions = array_diff_key($options, $this->supportedOptions)) !== array()) {
81 throw new InvalidValidationOptionsException('Unsupported validation option(s) found: ' . implode(', ', array_keys($unsupportedOptions)), 1379981890);
82 }
83
84 // check for required options being set
85 array_walk(
86 $this->supportedOptions,
87 function($supportedOptionData, $supportedOptionName, $options) {
88 if (isset($supportedOptionData[3]) && $supportedOptionData[3] === TRUE && !array_key_exists($supportedOptionName, $options)) {
89 throw new InvalidValidationOptionsException('Required validation option not set: ' . $supportedOptionName, 1379981891);
90 }
91 },
92 $options
93 );
94
95 // merge with default values
96 $this->options = array_merge(
97 array_map(
98 function ($value) {
99 return $value[0];
100 },
101 $this->supportedOptions
102 ),
103 $options
104 );
105 }
106
107 /**
108 * Checks if the given value is valid according to the validator, and returns
109 * the Error Messages object which occurred.
110 *
111 * @param mixed $value The value that should be validated
112 * @return \TYPO3\CMS\Extbase\Error\Result
113 * @api
114 */
115 public function validate($value) {
116 $this->result = new \TYPO3\CMS\Extbase\Error\Result();
117 if ($this->acceptsEmptyValues === FALSE || $this->isEmpty($value) === FALSE) {
118 $this->isValid($value);
119 }
120 return $this->result;
121 }
122
123 /**
124 * Check if $value is valid. If it is not valid, needs to add an error
125 * to Result.
126 *
127 * @param mixed $value
128 * @return void
129 */
130 abstract protected function isValid($value);
131
132 /**
133 * Sets options for the validator
134 *
135 * @param array $options Options for the validator
136 * @return void
137 * @deprecated since Extbase 1.4.0, will be removed two versions after Extbase 6.1. use constructor instead.
138 */
139 public function setOptions(array $options) {
140 $this->options = $options;
141 }
142
143 /**
144 * Returns an array of errors which occurred during the last isValid() call.
145 *
146 * @return array An array of \TYPO3\CMS\Extbase\Validation\Error objects or an empty array if no errors occurred.
147 * @deprecated since Extbase 1.4.0, will be removed two versions after Extbase 6.1. use validate() instead.
148 */
149 public function getErrors() {
150 return $this->errors;
151 }
152
153 /**
154 * Creates a new validation error object and adds it to $this->errors
155 *
156 * @param string $message The error message
157 * @param integer $code The error code (a unix timestamp)
158 * @param array $arguments Arguments to be replaced in message
159 * @param string $title title of the error
160 * @return void
161 */
162 protected function addError($message, $code, array $arguments = array(), $title = '') {
163 if ($this->result !== NULL) {
164 // backwards compatibility before Extbase 1.4.0: we cannot expect the "result" object to be there.
165 $this->result->addError(new \TYPO3\CMS\Extbase\Validation\Error($message, $code, $arguments, $title));
166 }
167 // the following is @deprecated since Extbase 1.4.0:
168 $this->errors[] = new \TYPO3\CMS\Extbase\Validation\Error($message, $code, $arguments, $title);
169 }
170
171 /**
172 * Returns the options of this validator
173 *
174 * @return array
175 */
176 public function getOptions() {
177 return $this->options;
178 }
179
180 /**
181 * @param mixed $value
182 * @return boolean TRUE if the given $value is NULL or an empty string ('')
183 */
184 final protected function isEmpty($value) {
185 return $value === NULL || $value === '';
186 }
187
188 /**
189 * Wrap static call to LocalizationUtility to simplify unit testing
190 *
191 * @param string $translateKey
192 * @param string $extensionName
193 * @param array $arguments
194 *
195 * @return NULL|string
196 */
197 protected function translateErrorMessage($translateKey, $extensionName, $arguments = array()) {
198 return \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate(
199 $translateKey,
200 $extensionName,
201 $arguments
202 );
203 }
204 }