2 /***************************************************************
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
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.
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
32 * @subpackage Validation\Validator
35 abstract class Tx_Extbase_Validation_Validator_AbstractValidator
implements Tx_Extbase_Validation_Validator_ValidatorInterface
{
39 protected $options = array();
43 * @deprecated since Extbase 1.4.0, will be removed in Extbase 6.0. You should use constructor parameter to set validation options.
45 protected $errors = array();
48 * @var Tx_Extbase_Error_Result
53 * Sets options for the validator
55 * @param array $validationOptions Options for the validator
59 public function __construct($validationOptions = array()) {
60 $this->options
= $validationOptions;
64 * Checks if the given value is valid according to the validator, and returns
65 * the Error Messages object which occured.
67 * @param mixed $value The value that should be validated
68 * @return Tx_Extbase_Error_Result
71 public function validate($value) {
72 $this->result
= new Tx_Extbase_Error_Result();
73 $this->isValid($value);
78 * Check if $value is valid. If it is not valid, needs to add an error
83 abstract protected function isValid($value);
86 * Sets options for the validator
88 * @param array $options Options for the validator
90 * @deprecated since Extbase 1.4.0, will be removed in Extbase 6.0. use constructor instead.
92 public function setOptions(array $options) {
93 $this->options
= $options;
97 * Returns an array of errors which occurred during the last isValid() call.
99 * @return array An array of Tx_Extbase_Validation_Error objects or an empty array if no errors occurred.
100 * @deprecated since Extbase 1.4.0, will be removed in Extbase 6.0. use validate() instead.
102 public function getErrors() {
103 return $this->errors
;
107 * Creates a new validation error object and adds it to $this->errors
109 * @param string $message The error message
110 * @param integer $code The error code (a unix timestamp)
111 * @param array $arguments Arguments to be replaced in message
112 * @param string $title title of the error
115 protected function addError($message, $code, array $arguments = array(), $title = '') {
116 if ($this->result
!== NULL) {
117 // backwards compatibility before Extbase 1.4.0: we cannot expect the "result" object to be there.
118 $this->result
->addError(new Tx_Extbase_Validation_Error($message, $code, $arguments, $title));
120 // the following is @deprecated since Extbase 1.4.0:
121 $this->errors
[] = new Tx_Extbase_Validation_Error($message, $code, $arguments, $title);