‪TYPO3CMS  10.4
Result.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
23 class ‪Result
24 {
28  protected ‪$errors = [];
29 
34  protected ‪$errorsExist = false;
35 
39  protected ‪$warnings = [];
40 
45  protected ‪$warningsExist = false;
46 
50  protected ‪$notices = [];
51 
56  protected ‪$noticesExist = false;
57 
63  protected ‪$propertyResults = [];
64 
68  protected ‪$parent;
69 
76  public function ‪setParent(‪Result ‪$parent): void
77  {
78  if ($this->parent !== ‪$parent) {
79  $this->parent = ‪$parent;
80  if ($this->‪hasErrors()) {
81  $parent->‪setErrorsExist();
82  }
83  if ($this->‪hasWarnings()) {
84  $parent->‪setWarningsExist();
85  }
86  if ($this->‪hasNotices()) {
87  $parent->‪setNoticesExist();
88  }
89  }
90  }
91 
97  public function ‪addError(Error $error): void
98  {
99  $this->errors[] = $error;
101  }
102 
108  public function ‪addWarning(‪Warning $warning): void
109  {
110  $this->warnings[] = $warning;
112  }
113 
119  public function ‪addNotice(‪Notice $notice): void
120  {
121  $this->notices[] = $notice;
123  }
124 
130  public function ‪getErrors(): array
131  {
133  }
134 
140  public function ‪getWarnings(): array
141  {
143  }
144 
150  public function ‪getNotices(): array
151  {
153  }
154 
160  public function ‪getFirstError()
161  {
162  reset($this->errors);
163  return current($this->errors);
164  }
165 
171  public function ‪getFirstWarning()
172  {
173  reset($this->warnings);
174  return current($this->warnings);
175  }
176 
182  public function ‪getFirstNotice()
183  {
184  reset($this->notices);
185  return current($this->notices);
186  }
187 
197  public function ‪forProperty(?string $propertyPath): Result
198  {
199  if ($propertyPath === '' || $propertyPath === null) {
200  return $this;
201  }
202  if (strpos($propertyPath, '.') !== false) {
203  return $this->‪recurseThroughResult(explode('.', $propertyPath));
204  }
205  if (!isset($this->propertyResults[$propertyPath])) {
206  $this->propertyResults[$propertyPath] = new self();
207  $this->propertyResults[$propertyPath]->setParent($this);
208  }
209  return $this->propertyResults[$propertyPath];
210  }
211 
220  public function ‪recurseThroughResult(array $pathSegments): Result
221  {
222  if (count($pathSegments) === 0) {
223  return $this;
224  }
225 
226  $propertyName = array_shift($pathSegments);
227 
228  if (!isset($this->propertyResults[$propertyName])) {
229  $this->propertyResults[$propertyName] = new self();
230  $this->propertyResults[$propertyName]->setParent($this);
231  }
232 
233  return $this->propertyResults[$propertyName]->recurseThroughResult($pathSegments);
234  }
235 
240  protected function ‪setErrorsExist(): void
241  {
242  $this->errorsExist = true;
243  if ($this->parent !== null) {
244  $this->parent->setErrorsExist();
245  }
246  }
247 
252  protected function ‪setWarningsExist(): void
253  {
254  $this->warningsExist = true;
255  if ($this->parent !== null) {
256  $this->parent->setWarningsExist();
257  }
258  }
259 
264  protected function ‪setNoticesExist(): void
265  {
266  $this->noticesExist = true;
267  if ($this->parent !== null) {
268  $this->parent->setNoticesExist();
269  }
270  }
271 
277  public function ‪hasMessages(): bool
278  {
279  return $this->errorsExist || $this->noticesExist || ‪$this->warningsExist;
280  }
281 
285  public function ‪clear(): void
286  {
287  $this->errors = [];
288  $this->notices = [];
289  $this->warnings = [];
290 
291  $this->warningsExist = false;
292  $this->noticesExist = false;
293  $this->errorsExist = false;
294 
295  $this->propertyResults = [];
296  }
297 
303  public function ‪hasErrors(): bool
304  {
305  if (count($this->errors) > 0) {
306  return true;
307  }
308 
309  foreach ($this->propertyResults as $subResult) {
310  if ($subResult->hasErrors()) {
311  return true;
312  }
313  }
314 
315  return false;
316  }
317 
323  public function ‪hasWarnings(): bool
324  {
325  if (count($this->warnings) > 0) {
326  return true;
327  }
328 
329  foreach ($this->propertyResults as $subResult) {
330  if ($subResult->hasWarnings()) {
331  return true;
332  }
333  }
334 
335  return false;
336  }
337 
343  public function ‪hasNotices(): bool
344  {
345  if (count($this->notices) > 0) {
346  return true;
347  }
348 
349  foreach ($this->propertyResults as $subResult) {
350  if ($subResult->hasNotices()) {
351  return true;
352  }
353  }
354 
355  return false;
356  }
357 
365  public function ‪getFlattenedErrors(): array
366  {
367  $result = [];
368  $this->‪flattenErrorTree($result, []);
369  return $result;
370  }
371 
379  public function ‪getFlattenedWarnings(): array
380  {
381  $result = [];
382  $this->‪flattenWarningsTree($result, []);
383  return $result;
384  }
385 
393  public function ‪getFlattenedNotices(): array
394  {
395  $result = [];
396  $this->‪flattenNoticesTree($result, []);
397  return $result;
398  }
399 
404  protected function ‪flattenErrorTree(array &$result, array $level): void
405  {
406  if (count($this->errors) > 0) {
407  $result[implode('.', $level)] = ‪$this->errors;
408  }
409  foreach ($this->propertyResults as $subPropertyName => $subResult) {
410  $level[] = $subPropertyName;
411  $subResult->flattenErrorTree($result, $level);
412  array_pop($level);
413  }
414  }
415 
420  protected function ‪flattenWarningsTree(array &$result, array $level): void
421  {
422  if (count($this->warnings) > 0) {
423  $result[implode('.', $level)] = ‪$this->warnings;
424  }
425  foreach ($this->propertyResults as $subPropertyName => $subResult) {
426  $level[] = $subPropertyName;
427  $subResult->flattenWarningsTree($result, $level);
428  array_pop($level);
429  }
430  }
431 
436  protected function ‪flattenNoticesTree(array &$result, array $level): void
437  {
438  if (count($this->notices) > 0) {
439  $result[implode('.', $level)] = ‪$this->notices;
440  }
441  foreach ($this->propertyResults as $subPropertyName => $subResult) {
442  $level[] = $subPropertyName;
443  $subResult->flattenNoticesTree($result, $level);
444  array_pop($level);
445  }
446  }
447 
453  public function ‪merge(Result $otherResult): void
454  {
455  if ($otherResult->errorsExist) {
456  $this->‪mergeProperty($otherResult, 'getErrors', 'addError');
457  }
458  if ($otherResult->warningsExist) {
459  $this->‪mergeProperty($otherResult, 'getWarnings', 'addWarning');
460  }
461  if ($otherResult->noticesExist) {
462  $this->‪mergeProperty($otherResult, 'getNotices', 'addNotice');
463  }
464 
465  foreach ($otherResult->getSubResults() as $subPropertyName => $subResult) {
467  if (array_key_exists($subPropertyName, $this->propertyResults) && $this->propertyResults[$subPropertyName]->‪hasMessages()) {
468  $this->‪forProperty($subPropertyName)->‪merge($subResult);
469  } else {
470  $this->propertyResults[$subPropertyName] = $subResult;
471  $subResult->setParent($this);
472  }
473  }
474  }
475 
483  protected function ‪mergeProperty(‪Result $otherResult, string $getterName, string $adderName): void
484  {
485  $getter = [$otherResult, $getterName];
486  $adder = [$this, $adderName];
487 
488  if (!is_callable($getter) || !is_callable($adder)) {
489  return;
490  }
491 
492  foreach ($getter() as $messageInOtherResult) {
493  $adder($messageInOtherResult);
494  }
495  }
496 
502  public function ‪getSubResults(): array
503  {
505  }
506 }
‪TYPO3\CMS\Extbase\Error\Result\getSubResults
‪Result[] getSubResults()
Definition: Result.php:494
‪TYPO3\CMS\Extbase\Error\Result\getFirstWarning
‪bool Warning getFirstWarning()
Definition: Result.php:163
‪TYPO3\CMS\Extbase\Error\Result\getFlattenedNotices
‪array< string, array< Notice > > getFlattenedNotices()
Definition: Result.php:385
‪TYPO3\CMS\Extbase\Error\Result\setParent
‪setParent(Result $parent)
Definition: Result.php:68
‪TYPO3\CMS\Extbase\Error\Result\$noticesExist
‪bool $noticesExist
Definition: Result.php:50
‪TYPO3\CMS\Extbase\Error\Result\$warningsExist
‪bool $warningsExist
Definition: Result.php:41
‪TYPO3\CMS\Extbase\Error\Result\$errorsExist
‪bool $errorsExist
Definition: Result.php:32
‪TYPO3\CMS\Extbase\Error\Result\clear
‪clear()
Definition: Result.php:277
‪TYPO3\CMS\Extbase\Error
Definition: Error.php:18
‪TYPO3\CMS\Extbase\Error\Result\$notices
‪Notice[] $notices
Definition: Result.php:45
‪TYPO3\CMS\Extbase\Error\Result\hasErrors
‪bool hasErrors()
Definition: Result.php:295
‪TYPO3\CMS\Extbase\Error\Result\forProperty
‪Result forProperty(?string $propertyPath)
Definition: Result.php:189
‪TYPO3\CMS\Extbase\Error\Result\$propertyResults
‪Result[] $propertyResults
Definition: Result.php:56
‪TYPO3\CMS\Extbase\Error\Notice
Definition: Notice.php:25
‪TYPO3\CMS\Extbase\Error\Result
Definition: Result.php:24
‪TYPO3\CMS\Extbase\Error\Result\merge
‪merge(Result $otherResult)
Definition: Result.php:445
‪TYPO3\CMS\Extbase\Error\Result\addError
‪addError(Error $error)
Definition: Result.php:89
‪TYPO3\CMS\Extbase\Error\Result\getWarnings
‪Warning[] getWarnings()
Definition: Result.php:132
‪TYPO3\CMS\Extbase\Error\Result\addNotice
‪addNotice(Notice $notice)
Definition: Result.php:111
‪TYPO3\CMS\Extbase\Error\Error
Definition: Error.php:25
‪TYPO3\CMS\Extbase\Error\Result\setErrorsExist
‪setErrorsExist()
Definition: Result.php:232
‪TYPO3\CMS\Extbase\Error\Result\setWarningsExist
‪setWarningsExist()
Definition: Result.php:244
‪TYPO3\CMS\Extbase\Error\Result\recurseThroughResult
‪Result recurseThroughResult(array $pathSegments)
Definition: Result.php:212
‪TYPO3\CMS\Extbase\Error\Result\getFlattenedErrors
‪array< string, array< Error > > getFlattenedErrors()
Definition: Result.php:357
‪TYPO3\CMS\Extbase\Error\Result\mergeProperty
‪mergeProperty(Result $otherResult, string $getterName, string $adderName)
Definition: Result.php:475
‪TYPO3\CMS\Extbase\Error\Result\hasNotices
‪bool hasNotices()
Definition: Result.php:335
‪TYPO3\CMS\Extbase\Error\Result\$parent
‪Result $parent
Definition: Result.php:60
‪TYPO3\CMS\Extbase\Error\Result\hasMessages
‪bool hasMessages()
Definition: Result.php:269
‪TYPO3\CMS\Extbase\Error\Warning
Definition: Warning.php:25
‪TYPO3\CMS\Extbase\Error\Result\flattenNoticesTree
‪flattenNoticesTree(array &$result, array $level)
Definition: Result.php:428
‪TYPO3\CMS\Extbase\Error\Result\getErrors
‪Error[] getErrors()
Definition: Result.php:122
‪TYPO3\CMS\Extbase\Error\Result\flattenWarningsTree
‪flattenWarningsTree(array &$result, array $level)
Definition: Result.php:412
‪TYPO3\CMS\Extbase\Error\Result\$errors
‪Error[] $errors
Definition: Result.php:27
‪TYPO3\CMS\Extbase\Error\Result\hasWarnings
‪bool hasWarnings()
Definition: Result.php:315
‪TYPO3\CMS\Extbase\Error\Result\setNoticesExist
‪setNoticesExist()
Definition: Result.php:256
‪TYPO3\CMS\Extbase\Error\Result\getFlattenedWarnings
‪array< string, array< Warning > > getFlattenedWarnings()
Definition: Result.php:371
‪TYPO3\CMS\Extbase\Error\Result\addWarning
‪addWarning(Warning $warning)
Definition: Result.php:100
‪TYPO3\CMS\Extbase\Error\Result\getFirstError
‪bool Error getFirstError()
Definition: Result.php:152
‪TYPO3\CMS\Extbase\Error\Result\flattenErrorTree
‪flattenErrorTree(array &$result, array $level)
Definition: Result.php:396
‪TYPO3\CMS\Extbase\Error\Result\getFirstNotice
‪bool Notice getFirstNotice()
Definition: Result.php:174
‪TYPO3\CMS\Extbase\Error\Result\$warnings
‪Warning[] $warnings
Definition: Result.php:36
‪TYPO3\CMS\Extbase\Error\Result\getNotices
‪Notice[] getNotices()
Definition: Result.php:142