[TASK] Backport changes to Extbase_Error_Message
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Validation / Validator / AbstractValidator.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
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 *
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.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * Abstract validator
30 *
31 * @package Extbase
32 * @subpackage Validation\Validator
33 * @version $Id$
34 */
35 abstract class Tx_Extbase_Validation_Validator_AbstractValidator implements Tx_Extbase_Validation_Validator_ValidatorInterface {
36 /**
37 * @var array
38 */
39 protected $options = array();
40
41 /**
42 * @var array
43 * @deprecated since Extbase 1.4.0, will be removed in Extbase 6.0. You should use constructor parameter to set validation options.
44 */
45 protected $errors = array();
46
47 /**
48 * @var Tx_Extbase_Error_Result
49 */
50 protected $result;
51
52 /**
53 * Sets options for the validator
54 *
55 * @param array $validationOptions Options for the validator
56 * @return void
57 * @api
58 */
59 public function __construct($validationOptions = array()) {
60 $this->options = $validationOptions;
61 }
62
63 /**
64 * Checks if the given value is valid according to the validator, and returns
65 * the Error Messages object which occured.
66 *
67 * @param mixed $value The value that should be validated
68 * @return Tx_Extbase_Error_Result
69 * @api
70 */
71 public function validate($value) {
72 $this->result = new Tx_Extbase_Error_Result();
73 $this->isValid($value);
74 return $this->result;
75 }
76
77 /**
78 * Check if $value is valid. If it is not valid, needs to add an error
79 * to Result.
80 *
81 * @return void
82 */
83 abstract protected function isValid($value);
84
85 /**
86 * Sets options for the validator
87 *
88 * @param array $options Options for the validator
89 * @return void
90 * @deprecated since Extbase 1.4.0, will be removed in Extbase 6.0. use constructor instead.
91 */
92 public function setOptions(array $options) {
93 $this->options = $options;
94 }
95
96 /**
97 * Returns an array of errors which occurred during the last isValid() call.
98 *
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.
101 */
102 public function getErrors() {
103 return $this->errors;
104 }
105
106 /**
107 * Creates a new validation error object and adds it to $this->errors
108 *
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
113 * @return void
114 */
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));
119 }
120 // the following is @deprecated since Extbase 1.4.0:
121 $this->errors[] = new Tx_Extbase_Validation_Error($message, $code, $arguments, $title);
122 }
123 }
124
125 ?>