[TASK] Use "bool" in @param notation
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Utility / ValidatorUtility.php
1 <?php
2 namespace TYPO3\CMS\Form\Utility;
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 /**
18 * Static methods for validation
19 *
20 * @author Patrick Broens <patrick@patrickbroens.nl>
21 */
22 class ValidatorUtility implements \TYPO3\CMS\Core\SingletonInterface {
23
24 /**
25 * Validation objects to use
26 *
27 * @var array
28 */
29 protected $rules = array();
30
31 /**
32 * Messages for all form fields
33 *
34 * @var array
35 */
36 protected $messages = array();
37
38 /**
39 * Errors for all form fields
40 *
41 * @var unknown_type
42 */
43 protected $errors = array();
44
45 /**
46 * Constructor
47 */
48 public function __construct() {}
49
50 /**
51 * Returns the current prefix of the form
52 * The RequestHandler is asked
53 *
54 * @return String
55 */
56 protected function getPrefix() {
57 return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Form\\Request')->getPrefix();
58 }
59
60 /**
61 * Create a rule object according to class
62 * and sent some arguments
63 *
64 * @param string $class Name of the validation rule
65 * @param array $arguments Configuration of the rule
66 * @return \TYPO3\CMS\Form\Validation\AbstractValidator The rule object
67 */
68 public function createRule($class, $arguments = array()) {
69 $class = strtolower((string) $class);
70 $className = 'TYPO3\\CMS\\Form\\Validation\\' . ucfirst($class) . 'Validator';
71 $rule = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($className, $arguments);
72 return $rule;
73 }
74
75 /**
76 * Add a rule to the rules array
77 * The rule needs to be completely configured before adding it to the array
78 *
79 * @param object $rule Rule object
80 * @param string $fieldName Field name the rule belongs to
81 * @param bool $breakOnError Break the rule chain when TRUE
82 * @return \TYPO3\CMS\Form\Utility\ValidatorUtility
83 */
84 public function addRule($rule, $fieldName, $breakOnError = FALSE) {
85 $prefix = $this->getPrefix();
86 $this->rules[$prefix][] = array(
87 'instance' => (object) $rule,
88 'fieldName' => (string) $fieldName,
89 'breakOnError' => (bool) $breakOnError
90 );
91 if ($rule->messageMustBeDisplayed()) {
92 if (!isset($this->messages[$prefix][$fieldName])) {
93 $this->messages[$prefix][$fieldName] = array();
94 }
95 end($this->rules[$prefix]);
96 $key = key($this->rules[$prefix]);
97 $message = $rule->getMessage();
98 $this->messages[$prefix][$fieldName][$key][$key + 1] = $message['cObj'];
99 $this->messages[$prefix][$fieldName][$key][($key + 1) . '.'] = $message['cObj.'];
100 }
101 return $this;
102 }
103
104 /**
105 * Returns TRUE when each rule in the chain returns valid
106 * When a rule has breakOnError set and the rule does not validate,
107 * the check for the remaining rules will stop and method returns FALSE
108 *
109 * @return boolean True if all rules validate
110 */
111 public function isValid() {
112 $prefix = $this->getPrefix();
113 $this->errors[$prefix] = array();
114 $result = TRUE;
115 foreach ($this->rules[$prefix] as $key => $element) {
116 $rule = $element['instance'];
117 $fieldName = $element['fieldName'];
118 if ($rule->isValid()) {
119 continue;
120 }
121 $result = FALSE;
122 if (!isset($this->errors[$prefix][$fieldName])) {
123 $this->errors[$prefix][$fieldName] = array();
124 }
125 $error = $rule->getError();
126 $this->errors[$prefix][$fieldName][$key][$key + 1] = $error['cObj'];
127 $this->errors[$prefix][$fieldName][$key][($key + 1) . '.'] = $error['cObj.'];
128 if ($element['breakOnError']) {
129 break;
130 }
131 }
132 return $result;
133 }
134
135 /**
136 * Returns all messages from all rules
137 *
138 * @return array
139 */
140 public function getMessages() {
141 return $this->messages[$this->getPrefix()];
142 }
143
144 /**
145 * Returns messages for a single form object
146 *
147 * @param string $name Name of the form object
148 * @return array
149 */
150 public function getMessagesByName($name) {
151 return $this->messages[$this->getPrefix()][$name];
152 }
153
154 /**
155 * Returns TRUE when a form object has a message
156 *
157 * @param string $name Name of the form object
158 * @return boolean
159 */
160 public function hasMessage($name) {
161 if (isset($this->messages[$this->getPrefix()][$name])) {
162 return TRUE;
163 }
164 return FALSE;
165 }
166
167 /**
168 * Returns all error messages from all rules
169 *
170 * @return array
171 */
172 public function getErrors() {
173 return $this->errors[$this->getPrefix()];
174 }
175
176 /**
177 * Returns error messages for a single form object
178 *
179 * @param string $name Name of the form object
180 * @return array
181 */
182 public function getErrorsByName($name) {
183 return $this->errors[$this->getPrefix()][$name];
184 }
185
186 /**
187 * Returns TRUE when a form object has an error
188 *
189 * @param string $name Name of the form object
190 * @return boolean
191 */
192 public function hasErrors($name) {
193 if (isset($this->errors[$this->getPrefix()][$name])) {
194 return TRUE;
195 }
196 return FALSE;
197 }
198
199 }