‪TYPO3CMS  9.5
Result.php
Go to the documentation of this file.
1 <?php
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  */
19 class ‪Result
20 {
24  protected ‪$errors = [];
25 
30  protected ‪$errorsExist = false;
31 
35  protected ‪$warnings = [];
36 
41  protected ‪$warningsExist = false;
42 
46  protected ‪$notices = [];
47 
52  protected ‪$noticesExist = false;
53 
59  protected ‪$propertyResults = [];
60 
64  protected ‪$parent;
65 
72  public function ‪setParent(‪Result ‪$parent)
73  {
74  if ($this->parent !== ‪$parent) {
75  $this->parent = ‪$parent;
76  if ($this->‪hasErrors()) {
77  $parent->‪setErrorsExist();
78  }
79  if ($this->‪hasWarnings()) {
80  $parent->‪setWarningsExist();
81  }
82  if ($this->‪hasNotices()) {
83  $parent->‪setNoticesExist();
84  }
85  }
86  }
87 
93  public function ‪addError(Error $error)
94  {
95  $this->errors[] = $error;
97  }
98 
104  public function ‪addWarning(‪Warning $warning)
105  {
106  $this->warnings[] = $warning;
108  }
109 
115  public function ‪addNotice(‪Notice $notice)
116  {
117  $this->notices[] = $notice;
119  }
120 
126  public function ‪getErrors()
127  {
129  }
130 
136  public function ‪getWarnings()
137  {
139  }
140 
146  public function ‪getNotices()
147  {
149  }
150 
156  public function ‪getFirstError()
157  {
158  reset($this->errors);
159  return current($this->errors);
160  }
161 
167  public function ‪getFirstWarning()
168  {
169  reset($this->warnings);
170  return current($this->warnings);
171  }
172 
178  public function ‪getFirstNotice()
179  {
180  reset($this->notices);
181  return current($this->notices);
182  }
183 
193  public function ‪forProperty($propertyPath)
194  {
195  if ($propertyPath === '' || $propertyPath === null) {
196  return $this;
197  }
198  if (strpos($propertyPath, '.') !== false) {
199  return $this->‪recurseThroughResult(explode('.', $propertyPath));
200  }
201  if (!isset($this->propertyResults[$propertyPath])) {
202  $this->propertyResults[$propertyPath] = new self();
203  $this->propertyResults[$propertyPath]->setParent($this);
204  }
205  return $this->propertyResults[$propertyPath];
206  }
207 
214  public function ‪recurseThroughResult(array $pathSegments)
215  {
216  if (empty($pathSegments)) {
217  return $this;
218  }
219 
220  $propertyName = array_shift($pathSegments);
221 
222  if (!isset($this->propertyResults[$propertyName])) {
223  $this->propertyResults[$propertyName] = new self();
224  $this->propertyResults[$propertyName]->setParent($this);
225  }
226 
227  return $this->propertyResults[$propertyName]->recurseThroughResult($pathSegments);
228  }
229 
234  protected function ‪setErrorsExist()
235  {
236  $this->errorsExist = true;
237  if ($this->parent !== null) {
238  $this->parent->setErrorsExist();
239  }
240  }
241 
246  protected function ‪setWarningsExist()
247  {
248  $this->warningsExist = true;
249  if ($this->parent !== null) {
250  $this->parent->setWarningsExist();
251  }
252  }
253 
258  protected function ‪setNoticesExist()
259  {
260  $this->noticesExist = true;
261  if ($this->parent !== null) {
262  $this->parent->setNoticesExist();
263  }
264  }
265 
271  public function ‪hasMessages()
272  {
273  return $this->errorsExist || $this->noticesExist || ‪$this->warningsExist;
274  }
275 
279  public function ‪clear()
280  {
281  $this->errors = [];
282  $this->notices = [];
283  $this->warnings = [];
284 
285  $this->warningsExist = false;
286  $this->noticesExist = false;
287  $this->errorsExist = false;
288 
289  $this->propertyResults = [];
290  }
291 
299  protected function ‪hasProperty($propertyName, $checkerMethodName)
300  {
301  if (!empty($this->{$propertyName})) {
302  return true;
303  }
304  foreach ($this->propertyResults as $subResult) {
305  if ($subResult->{$checkerMethodName}()) {
306  return true;
307  }
308  }
309  return false;
310  }
311 
317  public function hasErrors()
318  {
319  return $this->hasProperty('errors', 'hasErrors');
320  }
321 
327  public function hasWarnings()
328  {
329  return $this->hasProperty('warnings', 'hasWarnings');
330  }
331 
337  public function hasNotices()
338  {
339  return $this->hasProperty('notices', 'hasNotices');
340  }
341 
349  public function getFlattenedErrors()
350  {
351  $result = [];
352  $this->flattenTree('errors', $result, []);
353  return $result;
354  }
355 
363  public function getFlattenedWarnings()
364  {
365  $result = [];
366  $this->flattenTree('warnings', $result, []);
367  return $result;
368  }
369 
377  public function getFlattenedNotices()
378  {
379  $result = [];
380  $this->flattenTree('notices', $result, []);
381  return $result;
382  }
383 
393  public function flattenTree($propertyName, &$result, $level)
394  {
395  if (!empty($this->$propertyName)) {
396  $result[implode('.', $level)] = $this->$propertyName;
397  }
398  foreach ($this->propertyResults as $subPropertyName => $subResult) {
399  $level[] = $subPropertyName;
400  $subResult->flattenTree($propertyName, $result, $level);
401  array_pop($level);
402  }
403  }
404 
410  public function merge(Result $otherResult)
411  {
412  if ($otherResult->errorsExist) {
413  $this->mergeProperty($otherResult, 'getErrors', 'addError');
414  }
415  if ($otherResult->warningsExist) {
416  $this->mergeProperty($otherResult, 'getWarnings', 'addWarning');
417  }
418  if ($otherResult->noticesExist) {
419  $this->mergeProperty($otherResult, 'getNotices', 'addNotice');
420  }
421 
422  foreach ($otherResult->getSubResults() as $subPropertyName => $subResult) {
424  if (array_key_exists($subPropertyName, $this->propertyResults) && $this->propertyResults[$subPropertyName]->hasMessages()) {
425  $this->forProperty($subPropertyName)->merge($subResult);
426  } else {
427  $this->propertyResults[$subPropertyName] = $subResult;
428  $subResult->setParent($this);
429  }
430  }
431  }
432 
440  protected function mergeProperty(‪Result $otherResult, $getterName, $adderName)
441  {
442  foreach ($otherResult->$getterName() as $messageInOtherResult) {
443  $this->$adderName($messageInOtherResult);
444  }
445  }
446 
452  public function getSubResults()
453  {
454  return $this->propertyResults;
455  }
456 }
‪TYPO3\CMS\Extbase\Error\Result\getSubResults
‪Result[] getSubResults()
Definition: Result.php:444
‪TYPO3\CMS\Extbase\Error\Result\getFlattenedNotices
‪Notice[] getFlattenedNotices()
Definition: Result.php:369
‪TYPO3\CMS\Extbase\Error\Result\getFirstError
‪Error getFirstError()
Definition: Result.php:148
‪TYPO3\CMS\Extbase\Error\Result\setParent
‪setParent(Result $parent)
Definition: Result.php:64
‪TYPO3\CMS\Extbase\Error\Result\$noticesExist
‪bool $noticesExist
Definition: Result.php:46
‪TYPO3\CMS\Extbase\Error\Result\$warningsExist
‪bool $warningsExist
Definition: Result.php:37
‪TYPO3\CMS\Extbase\Error\Result\$errorsExist
‪bool $errorsExist
Definition: Result.php:28
‪TYPO3\CMS\Extbase\Error\Result\clear
‪clear()
Definition: Result.php:271
‪TYPO3\CMS\Extbase\Error
Definition: Error.php:2
‪TYPO3\CMS\Extbase\Error\Result\$notices
‪Notice[] $notices
Definition: Result.php:41
‪TYPO3\CMS\Extbase\Error\Result\hasErrors
‪bool hasErrors()
Definition: Result.php:309
‪TYPO3\CMS\Extbase\Error\Result\hasProperty
‪bool hasProperty($propertyName, $checkerMethodName)
Definition: Result.php:291
‪TYPO3\CMS\Extbase\Error\Result\getFlattenedWarnings
‪Warning[] getFlattenedWarnings()
Definition: Result.php:355
‪TYPO3\CMS\Extbase\Error\Result\getFirstNotice
‪Notice getFirstNotice()
Definition: Result.php:170
‪TYPO3\CMS\Extbase\Error\Result\$propertyResults
‪Result[] $propertyResults
Definition: Result.php:52
‪TYPO3\CMS\Extbase\Error\Notice
Definition: Notice.php:22
‪TYPO3\CMS\Extbase\Error\Result
Definition: Result.php:20
‪TYPO3\CMS\Extbase\Error\Result\merge
‪merge(Result $otherResult)
Definition: Result.php:402
‪TYPO3\CMS\Extbase\Error\Result\addError
‪addError(Error $error)
Definition: Result.php:85
‪TYPO3\CMS\Extbase\Error\Result\getWarnings
‪Warning[] getWarnings()
Definition: Result.php:128
‪TYPO3\CMS\Extbase\Error\Result\addNotice
‪addNotice(Notice $notice)
Definition: Result.php:107
‪TYPO3\CMS\Extbase\Error\Error
Definition: Error.php:22
‪TYPO3\CMS\Extbase\Error\Result\setErrorsExist
‪setErrorsExist()
Definition: Result.php:226
‪TYPO3\CMS\Extbase\Error\Result\setWarningsExist
‪setWarningsExist()
Definition: Result.php:238
‪TYPO3\CMS\Extbase\Error\Result\recurseThroughResult
‪Result recurseThroughResult(array $pathSegments)
Definition: Result.php:206
‪TYPO3\CMS\Extbase\Error\Result\forProperty
‪Result forProperty($propertyPath)
Definition: Result.php:185
‪TYPO3\CMS\Extbase\Error\Result\getFirstWarning
‪Warning getFirstWarning()
Definition: Result.php:159
‪TYPO3\CMS\Extbase\Error\Result\hasNotices
‪bool hasNotices()
Definition: Result.php:329
‪TYPO3\CMS\Extbase\Error\Result\$parent
‪Result $parent
Definition: Result.php:56
‪TYPO3\CMS\Extbase\Error\Result\hasMessages
‪bool hasMessages()
Definition: Result.php:263
‪TYPO3\CMS\Extbase\Error\Warning
Definition: Warning.php:22
‪TYPO3\CMS\Extbase\Error\Result\getErrors
‪Error[] getErrors()
Definition: Result.php:118
‪TYPO3\CMS\Extbase\Error\Result\$errors
‪Error[] $errors
Definition: Result.php:23
‪TYPO3\CMS\Extbase\Error\Result\getFlattenedErrors
‪Error[] getFlattenedErrors()
Definition: Result.php:341
‪TYPO3\CMS\Extbase\Error\Result\flattenTree
‪flattenTree($propertyName, &$result, $level)
Definition: Result.php:385
‪TYPO3\CMS\Extbase\Error\Result\mergeProperty
‪mergeProperty(Result $otherResult, $getterName, $adderName)
Definition: Result.php:432
‪TYPO3\CMS\Extbase\Error\Result\hasWarnings
‪bool hasWarnings()
Definition: Result.php:319
‪TYPO3\CMS\Extbase\Error\Result\setNoticesExist
‪setNoticesExist()
Definition: Result.php:250
‪TYPO3\CMS\Extbase\Error\Result\addWarning
‪addWarning(Warning $warning)
Definition: Result.php:96
‪TYPO3\CMS\Extbase\Error\Result\$warnings
‪Warning[] $warnings
Definition: Result.php:32
‪TYPO3\CMS\Extbase\Error\Result\getNotices
‪Notice[] getNotices()
Definition: Result.php:138