60f3ff5adeab4c0b83dcca398ab1bac6113c1e3c
[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 /**
31 * Abstract validator
32 */
33 abstract class AbstractValidator implements \TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface {
34
35 /**
36 * Specifies whether this validator accepts empty values.
37 *
38 * If this is TRUE, the validators isValid() method is not called in case of an empty value
39 * Note: A value is considered empty if it is NULL or an empty string!
40 * By default all validators except for NotEmpty and the Composite Validators accept empty values
41 *
42 * @var boolean
43 */
44 protected $acceptsEmptyValues = TRUE;
45
46 /**
47 * This contains the supported options, their default values, types and descriptions.
48 *
49 * @var array
50 */
51 protected $supportedOptions = array();
52
53 /**
54 * @var array
55 */
56 protected $options = array();
57
58 /**
59 * @var array
60 * @deprecated since Extbase 1.4.0, will be removed two versions after Extbase 6.1. You should use constructor parameter to set validation options.
61 */
62 protected $errors = array();
63
64 /**
65 * @var \TYPO3\CMS\Extbase\Error\Result
66 */
67 protected $result;
68
69 /**
70 * Sets options for the validator
71 *
72 * @param array $validationOptions Options for the validator
73 * @api
74 */
75 public function __construct($validationOptions = array()) {
76 $this->options = $validationOptions;
77 }
78
79 /**
80 * Checks if the given value is valid according to the validator, and returns
81 * the Error Messages object which occurred.
82 *
83 * @param mixed $value The value that should be validated
84 * @return \TYPO3\CMS\Extbase\Error\Result
85 * @api
86 */
87 public function validate($value) {
88 $this->result = new \TYPO3\CMS\Extbase\Error\Result();
89 if ($this->acceptsEmptyValues === FALSE || $this->isEmpty($value) === FALSE) {
90 $this->isValid($value);
91 }
92 return $this->result;
93 }
94
95 /**
96 * Check if $value is valid. If it is not valid, needs to add an error
97 * to Result.
98 *
99 * @param mixed $value
100 * @return void
101 */
102 abstract protected function isValid($value);
103
104 /**
105 * Sets options for the validator
106 *
107 * @param array $options Options for the validator
108 * @return void
109 * @deprecated since Extbase 1.4.0, will be removed two versions after Extbase 6.1. use constructor instead.
110 */
111 public function setOptions(array $options) {
112 $this->options = $options;
113 }
114
115 /**
116 * Returns an array of errors which occurred during the last isValid() call.
117 *
118 * @return array An array of \TYPO3\CMS\Extbase\Validation\Error objects or an empty array if no errors occurred.
119 * @deprecated since Extbase 1.4.0, will be removed two versions after Extbase 6.1. use validate() instead.
120 */
121 public function getErrors() {
122 return $this->errors;
123 }
124
125 /**
126 * Creates a new validation error object and adds it to $this->errors
127 *
128 * @param string $message The error message
129 * @param integer $code The error code (a unix timestamp)
130 * @param array $arguments Arguments to be replaced in message
131 * @param string $title title of the error
132 * @return void
133 */
134 protected function addError($message, $code, array $arguments = array(), $title = '') {
135 if ($this->result !== NULL) {
136 // backwards compatibility before Extbase 1.4.0: we cannot expect the "result" object to be there.
137 $this->result->addError(new \TYPO3\CMS\Extbase\Validation\Error($message, $code, $arguments, $title));
138 }
139 // the following is @deprecated since Extbase 1.4.0:
140 $this->errors[] = new \TYPO3\CMS\Extbase\Validation\Error($message, $code, $arguments, $title);
141 }
142
143 /**
144 * Returns the options of this validator
145 *
146 * @return array
147 */
148 public function getOptions() {
149 return $this->options;
150 }
151
152 /**
153 * @param mixed $value
154 * @return boolean TRUE if the given $value is NULL or an empty string ('')
155 */
156 final protected function isEmpty($value) {
157 return $value === NULL || $value === '';
158 }
159
160 /**
161 * Wrap static call to LocalizationUtility to simplify unit testing
162 *
163 * @param string $translateKey
164 * @param string $extensionName
165 * @param array $arguments
166 *
167 * @return NULL|string
168 */
169 protected function translateErrorMessage($translateKey, $extensionName, $arguments = array()) {
170 return \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate(
171 $translateKey,
172 $extensionName,
173 $arguments
174 );
175 }
176 }