b03a735a397640eebe6188b57185ce3ffbecc88e
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Error / Result.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Error;
3
4 /* *
5 * This script belongs to the Extbase framework *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License as published by the *
9 * Free Software Foundation, either version 3 of the License, or (at your *
10 * option) any later version. *
11 * *
12 * This script is distributed in the hope that it will be useful, but *
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
15 * General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with the script. *
19 * If not, see http://www.gnu.org/licenses/lgpl.html *
20 * *
21 * The TYPO3 project - inspiring people to share! *
22 * */
23 /**
24 * Result object for operations dealing with objects, such as the Property Mapper or the Validators.
25 *
26 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
27 * @api
28 */
29 class Result {
30
31 /**
32 * @var array<\TYPO3\CMS\Extbase\Error\Error>
33 */
34 protected $errors = array();
35
36 /**
37 * @var array<\TYPO3\CMS\Extbase\Error\Warning>
38 */
39 protected $warnings = array();
40
41 /**
42 * @var array<\TYPO3\CMS\Extbase\Error\Notice>
43 */
44 protected $notices = array();
45
46 /**
47 * The result objects for the sub properties
48 *
49 * @var array<\TYPO3\CMS\Extbase\Error\Result>
50 */
51 protected $propertyResults = array();
52
53 /**
54 * Add an error to the current Result object
55 *
56 * @param \TYPO3\CMS\Extbase\Error\Error $error
57 * @return void
58 * @api
59 */
60 public function addError(\TYPO3\CMS\Extbase\Error\Error $error) {
61 $this->errors[] = $error;
62 }
63
64 /**
65 * Add a warning to the current Result object
66 *
67 * @param \TYPO3\CMS\Extbase\Error\Warning $warning
68 * @return void
69 * @api
70 */
71 public function addWarning(\TYPO3\CMS\Extbase\Error\Warning $warning) {
72 $this->warnings[] = $warning;
73 }
74
75 /**
76 * Add a notice to the current Result object
77 *
78 * @param \TYPO3\CMS\Extbase\Error\Notice $notice
79 * @return void
80 * @api
81 */
82 public function addNotice(\TYPO3\CMS\Extbase\Error\Notice $notice) {
83 $this->notices[] = $notice;
84 }
85
86 /**
87 * Get all errors in the current Result object (non-recursive)
88 *
89 * @return array<\TYPO3\CMS\Extbase\Error\Error>
90 * @api
91 */
92 public function getErrors() {
93 return $this->errors;
94 }
95
96 /**
97 * Get all warnings in the current Result object (non-recursive)
98 *
99 * @return array<\TYPO3\CMS\Extbase\Error\Warning>
100 * @api
101 */
102 public function getWarnings() {
103 return $this->warnings;
104 }
105
106 /**
107 * Get all notices in the current Result object (non-recursive)
108 *
109 * @return array<\TYPO3\CMS\Extbase\Error\Notice>
110 * @api
111 */
112 public function getNotices() {
113 return $this->notices;
114 }
115
116 /**
117 * Get the first error object of the current Result object (non-recursive)
118 *
119 * @return \TYPO3\CMS\Extbase\Error\Error
120 * @api
121 */
122 public function getFirstError() {
123 reset($this->errors);
124 return current($this->errors);
125 }
126
127 /**
128 * Get the first warning object of the current Result object (non-recursive)
129 *
130 * @return \TYPO3\CMS\Extbase\Error\Warning
131 * @api
132 */
133 public function getFirstWarning() {
134 reset($this->warnings);
135 return current($this->warnings);
136 }
137
138 /**
139 * Get the first notice object of the curren Result object (non-recursive)
140 *
141 * @return \TYPO3\CMS\Extbase\Error\Notice
142 * @api
143 */
144 public function getFirstNotice() {
145 reset($this->notices);
146 return current($this->notices);
147 }
148
149 /**
150 * Return a Result object for the given property path. This is
151 * a fluent interface, so you will proboably use it like:
152 * $result->forProperty('foo.bar')->getErrors() -- to get all errors
153 * for property "foo.bar"
154 *
155 * @param string $propertyPath
156 * @return \TYPO3\CMS\Extbase\Error\Result
157 * @api
158 */
159 public function forProperty($propertyPath) {
160 if ($propertyPath === '' || $propertyPath === NULL) {
161 return $this;
162 }
163 $propertyPathSegments = explode('.', $propertyPath);
164 return $this->recurseThroughResult($propertyPathSegments);
165 }
166
167 /**
168 * Internal use only!
169 *
170 * @param array $pathSegments
171 * @return \TYPO3\CMS\Extbase\Error\Result
172 */
173 public function recurseThroughResult(array $pathSegments) {
174 if (count($pathSegments) === 0) {
175 return $this;
176 }
177 $propertyName = array_shift($pathSegments);
178 if (!isset($this->propertyResults[$propertyName])) {
179 $this->propertyResults[$propertyName] = new \TYPO3\CMS\Extbase\Error\Result();
180 }
181 return $this->propertyResults[$propertyName]->recurseThroughResult($pathSegments);
182 }
183
184 /**
185 * Internal use only!
186 *
187 * @param string $propertyName
188 * @param string $checkerMethodName
189 * @return boolean
190 */
191 protected function hasProperty($propertyName, $checkerMethodName) {
192 if (count($this->{$propertyName}) > 0) {
193 return TRUE;
194 }
195 foreach ($this->propertyResults as $subResult) {
196 if ($subResult->{$checkerMethodName}()) {
197 return TRUE;
198 }
199 }
200 return FALSE;
201 }
202
203 /**
204 * Does the current Result object have Errors? (Recursively)
205 *
206 * @return boolean
207 * @api
208 */
209 public function hasErrors() {
210 return $this->hasProperty('errors', 'hasErrors');
211 }
212
213 /**
214 * Does the current Result object have Warnings? (Recursively)
215 *
216 * @return boolean
217 * @api
218 */
219 public function hasWarnings() {
220 return $this->hasProperty('warnings', 'hasWarnings');
221 }
222
223 /**
224 * Does the current Result object have Notices? (Recursively)
225 *
226 * @return boolean
227 * @api
228 */
229 public function hasNotices() {
230 return $this->hasProperty('notices', 'hasNotices');
231 }
232
233 /**
234 * Get a list of all Error objects recursively. The result is an array,
235 * where the key is the property path where the error occured, and the
236 * value is a list of all errors (stored as array)
237 *
238 * @return array<\TYPO3\CMS\Extbase\Error\Error>
239 * @api
240 */
241 public function getFlattenedErrors() {
242 $result = array();
243 $this->flattenTree('errors', $result, array());
244 return $result;
245 }
246
247 /**
248 * Get a list of all Warning objects recursively. The result is an array,
249 * where the key is the property path where the warning occured, and the
250 * value is a list of all warnings (stored as array)
251 *
252 * @return array<\TYPO3\CMS\Extbase\Error\Warning>
253 * @api
254 */
255 public function getFlattenedWarnings() {
256 $result = array();
257 $this->flattenTree('warnings', $result, array());
258 return $result;
259 }
260
261 /**
262 * Get a list of all Notice objects recursively. The result is an array,
263 * where the key is the property path where the notice occured, and the
264 * value is a list of all notices (stored as array)
265 *
266 * @return array<\TYPO3\CMS\Extbase\Error\Notice>
267 * @api
268 */
269 public function getFlattenedNotices() {
270 $result = array();
271 $this->flattenTree('notices', $result, array());
272 return $result;
273 }
274
275 /**
276 * Only use internally!
277 *
278 * Flatten a tree of Result objects, based on a certain property.
279 *
280 * @param string $propertyName
281 * @param array &$result
282 * @param array $level
283 * @return void
284 */
285 public function flattenTree($propertyName, &$result, $level) {
286 if (count($this->{$propertyName}) > 0) {
287 $result[implode('.', $level)] = $this->{$propertyName};
288 }
289 foreach ($this->propertyResults as $subPropertyName => $subResult) {
290 array_push($level, $subPropertyName);
291 $subResult->flattenTree($propertyName, $result, $level);
292 array_pop($level);
293 }
294 }
295
296 /**
297 * Merge the given Result object into this one.
298 *
299 * @param \TYPO3\CMS\Extbase\Error\Result $otherResult
300 * @return void
301 * @api
302 */
303 public function merge(\TYPO3\CMS\Extbase\Error\Result $otherResult) {
304 $this->mergeProperty($otherResult, 'getErrors', 'addError');
305 $this->mergeProperty($otherResult, 'getWarnings', 'addWarning');
306 $this->mergeProperty($otherResult, 'getNotices', 'addNotice');
307 foreach ($otherResult->getSubResults() as $subPropertyName => $subResult) {
308 $this->forProperty($subPropertyName)->merge($subResult);
309 }
310 }
311
312 /**
313 * Merge a single property from the other result object.
314 *
315 * @param \TYPO3\CMS\Extbase\Error\Result $otherResult
316 * @param string $getterName
317 * @param string $adderName
318 * @return void
319 */
320 protected function mergeProperty(\TYPO3\CMS\Extbase\Error\Result $otherResult, $getterName, $adderName) {
321 foreach ($otherResult->{$getterName}() as $messageInOtherResult) {
322 $this->{$adderName}($messageInOtherResult);
323 }
324 }
325
326 /**
327 * Get a list of all sub Result objects available.
328 *
329 * @return array<\TYPO3\CMS\Extbase\Error\Result>
330 */
331 public function getSubResults() {
332 return $this->propertyResults;
333 }
334 }
335
336 ?>