‪TYPO3CMS  ‪main
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 
74  public function ‪setParent(‪Result ‪$parent): void
75  {
76  if ($this->parent !== ‪$parent) {
77  $this->parent = ‪$parent;
78  if ($this->‪hasErrors()) {
79  $parent->‪setErrorsExist();
80  }
81  if ($this->‪hasWarnings()) {
82  $parent->‪setWarningsExist();
83  }
84  if ($this->‪hasNotices()) {
85  $parent->‪setNoticesExist();
86  }
87  }
88  }
89 
93  public function ‪addError(Error $error): void
94  {
95  $this->errors[] = $error;
96  $this->‪setErrorsExist();
97  }
98 
102  public function ‪addWarning(‪Warning $warning): void
103  {
104  $this->warnings[] = $warning;
105  $this->‪setWarningsExist();
106  }
107 
111  public function ‪addNotice(‪Notice $notice): void
112  {
113  $this->notices[] = $notice;
115  }
116 
122  public function ‪getErrors(): array
123  {
125  }
126 
132  public function ‪getWarnings(): array
133  {
135  }
136 
142  public function ‪getNotices(): array
143  {
145  }
146 
152  public function ‪getFirstError()
153  {
154  reset($this->errors);
155  return current($this->errors);
156  }
157 
163  public function ‪getFirstWarning()
164  {
165  reset($this->warnings);
166  return current($this->warnings);
167  }
168 
174  public function ‪getFirstNotice()
175  {
176  reset($this->notices);
177  return current($this->notices);
178  }
179 
186  public function ‪forProperty(?string $propertyPath): ‪Result
187  {
188  if ($propertyPath === '' || $propertyPath === null) {
189  return $this;
190  }
191  if (str_contains($propertyPath, '.')) {
192  return $this->‪recurseThroughResult(explode('.', $propertyPath));
193  }
194  if (!isset($this->propertyResults[$propertyPath])) {
195  $this->propertyResults[$propertyPath] = new self();
196  $this->propertyResults[$propertyPath]->setParent($this);
197  }
198  return $this->propertyResults[$propertyPath];
199  }
200 
206  public function ‪recurseThroughResult(array $pathSegments): ‪Result
207  {
208  if (count($pathSegments) === 0) {
209  return $this;
210  }
211 
212  $propertyName = array_shift($pathSegments);
213 
214  if (!isset($this->propertyResults[$propertyName])) {
215  $this->propertyResults[$propertyName] = new self();
216  $this->propertyResults[$propertyName]->setParent($this);
217  }
218 
219  return $this->propertyResults[$propertyName]->recurseThroughResult($pathSegments);
220  }
221 
226  protected function ‪setErrorsExist(): void
227  {
228  $this->errorsExist = true;
229  if ($this->parent !== null) {
230  $this->parent->setErrorsExist();
231  }
232  }
233 
238  protected function ‪setWarningsExist(): void
239  {
240  $this->warningsExist = true;
241  if ($this->parent !== null) {
242  $this->parent->setWarningsExist();
243  }
244  }
245 
250  protected function ‪setNoticesExist(): void
251  {
252  $this->noticesExist = true;
253  if ($this->parent !== null) {
254  $this->parent->setNoticesExist();
255  }
256  }
257 
261  public function ‪hasMessages(): bool
262  {
263  return $this->errorsExist || $this->noticesExist || ‪$this->warningsExist;
264  }
265 
269  public function ‪clear(): void
270  {
271  $this->errors = [];
272  $this->notices = [];
273  $this->warnings = [];
274 
275  $this->warningsExist = false;
276  $this->noticesExist = false;
277  $this->errorsExist = false;
278 
279  $this->propertyResults = [];
280  }
281 
285  public function ‪hasErrors(): bool
286  {
287  if (count($this->errors) > 0) {
288  return true;
289  }
290 
291  foreach ($this->propertyResults as $subResult) {
292  if ($subResult->hasErrors()) {
293  return true;
294  }
295  }
296 
297  return false;
298  }
299 
303  public function ‪hasWarnings(): bool
304  {
305  if (count($this->warnings) > 0) {
306  return true;
307  }
308 
309  foreach ($this->propertyResults as $subResult) {
310  if ($subResult->hasWarnings()) {
311  return true;
312  }
313  }
314 
315  return false;
316  }
317 
321  public function ‪hasNotices(): bool
322  {
323  if (count($this->notices) > 0) {
324  return true;
325  }
326 
327  foreach ($this->propertyResults as $subResult) {
328  if ($subResult->hasNotices()) {
329  return true;
330  }
331  }
332 
333  return false;
334  }
335 
343  public function ‪getFlattenedErrors(): array
344  {
345  $result = [];
346  $this->‪flattenErrorTree($result, []);
347  return $result;
348  }
349 
357  public function ‪getFlattenedWarnings(): array
358  {
359  $result = [];
360  $this->‪flattenWarningsTree($result, []);
361  return $result;
362  }
363 
371  public function ‪getFlattenedNotices(): array
372  {
373  $result = [];
374  $this->‪flattenNoticesTree($result, []);
375  return $result;
376  }
377 
378  protected function ‪flattenErrorTree(array &$result, array $level): void
379  {
380  if (count($this->errors) > 0) {
381  $result[implode('.', $level)] = ‪$this->errors;
382  }
383  foreach ($this->propertyResults as $subPropertyName => $subResult) {
384  $level[] = $subPropertyName;
385  $subResult->flattenErrorTree($result, $level);
386  array_pop($level);
387  }
388  }
389 
390  protected function ‪flattenWarningsTree(array &$result, array $level): void
391  {
392  if (count($this->warnings) > 0) {
393  $result[implode('.', $level)] = ‪$this->warnings;
394  }
395  foreach ($this->propertyResults as $subPropertyName => $subResult) {
396  $level[] = $subPropertyName;
397  $subResult->flattenWarningsTree($result, $level);
398  array_pop($level);
399  }
400  }
401 
402  protected function ‪flattenNoticesTree(array &$result, array $level): void
403  {
404  if (count($this->notices) > 0) {
405  $result[implode('.', $level)] = ‪$this->notices;
406  }
407  foreach ($this->propertyResults as $subPropertyName => $subResult) {
408  $level[] = $subPropertyName;
409  $subResult->flattenNoticesTree($result, $level);
410  array_pop($level);
411  }
412  }
413 
417  public function ‪merge(Result $otherResult): void
418  {
419  if ($otherResult->errorsExist) {
420  $this->‪mergeProperty($otherResult, 'getErrors', 'addError');
421  }
422  if ($otherResult->warningsExist) {
423  $this->‪mergeProperty($otherResult, 'getWarnings', 'addWarning');
424  }
425  if ($otherResult->noticesExist) {
426  $this->‪mergeProperty($otherResult, 'getNotices', 'addNotice');
427  }
428 
429  foreach ($otherResult->getSubResults() as $subPropertyName => $subResult) {
431  if (array_key_exists($subPropertyName, $this->propertyResults) && $this->propertyResults[$subPropertyName]->‪hasMessages()) {
432  $this->‪forProperty($subPropertyName)->merge($subResult);
433  } else {
434  $this->propertyResults[$subPropertyName] = $subResult;
435  $subResult->setParent($this);
436  }
437  }
438  }
439 
443  protected function ‪mergeProperty(Result $otherResult, string $getterName, string $adderName): void
444  {
445  $getter = [$otherResult, $getterName];
446  $adder = [$this, $adderName];
447 
448  if (!is_callable($getter) || !is_callable($adder)) {
449  return;
450  }
451 
452  foreach ($getter() as $messageInOtherResult) {
453  $adder($messageInOtherResult);
454  }
455  }
456 
462  public function ‪getSubResults(): array
463  {
465  }
466 }
‪TYPO3\CMS\Extbase\Error\Result\getSubResults
‪Result[] getSubResults()
Definition: Result.php:454
‪TYPO3\CMS\Extbase\Error\Result\getFirstWarning
‪bool Warning getFirstWarning()
Definition: Result.php:155
‪TYPO3\CMS\Extbase\Error\Result\getFlattenedNotices
‪array< string, array< Notice > > getFlattenedNotices()
Definition: Result.php:363
‪TYPO3\CMS\Extbase\Error\Result\hasWarnings
‪hasWarnings()
Definition: Result.php:295
‪TYPO3\CMS\Extbase\Error\Result\hasErrors
‪hasErrors()
Definition: Result.php:277
‪TYPO3\CMS\Extbase\Error\Result\setParent
‪setParent(Result $parent)
Definition: Result.php:66
‪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:261
‪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\recurseThroughResult
‪recurseThroughResult(array $pathSegments)
Definition: Result.php:198
‪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:409
‪TYPO3\CMS\Extbase\Error\Result\addError
‪addError(Error $error)
Definition: Result.php:85
‪TYPO3\CMS\Extbase\Error\Result\getWarnings
‪Warning[] getWarnings()
Definition: Result.php:124
‪TYPO3\CMS\Extbase\Error\Result\addNotice
‪addNotice(Notice $notice)
Definition: Result.php:103
‪TYPO3\CMS\Extbase\Error\Error
Definition: Error.php:25
‪TYPO3\CMS\Extbase\Error\Result\setErrorsExist
‪setErrorsExist()
Definition: Result.php:218
‪TYPO3\CMS\Extbase\Error\Result\setWarningsExist
‪setWarningsExist()
Definition: Result.php:230
‪TYPO3\CMS\Extbase\Error\Result\getFlattenedErrors
‪array< string, array< Error > > getFlattenedErrors()
Definition: Result.php:335
‪TYPO3\CMS\Extbase\Error\Result\hasNotices
‪hasNotices()
Definition: Result.php:313
‪TYPO3\CMS\Extbase\Error\Result\mergeProperty
‪mergeProperty(Result $otherResult, string $getterName, string $adderName)
Definition: Result.php:435
‪TYPO3\CMS\Extbase\Error\Result\$parent
‪Result $parent
Definition: Result.php:60
‪TYPO3\CMS\Extbase\Error\Result\forProperty
‪forProperty(?string $propertyPath)
Definition: Result.php:178
‪TYPO3\CMS\Extbase\Error\Warning
Definition: Warning.php:25
‪TYPO3\CMS\Extbase\Error\Result\flattenNoticesTree
‪flattenNoticesTree(array &$result, array $level)
Definition: Result.php:394
‪TYPO3\CMS\Extbase\Error\Result\getErrors
‪Error[] getErrors()
Definition: Result.php:114
‪TYPO3\CMS\Extbase\Error\Result\flattenWarningsTree
‪flattenWarningsTree(array &$result, array $level)
Definition: Result.php:382
‪TYPO3\CMS\Extbase\Error\Result\$errors
‪Error[] $errors
Definition: Result.php:27
‪TYPO3\CMS\Extbase\Error\Result\setNoticesExist
‪setNoticesExist()
Definition: Result.php:242
‪TYPO3\CMS\Extbase\Error\Result\getFlattenedWarnings
‪array< string, array< Warning > > getFlattenedWarnings()
Definition: Result.php:349
‪TYPO3\CMS\Extbase\Error\Result\addWarning
‪addWarning(Warning $warning)
Definition: Result.php:94
‪TYPO3\CMS\Extbase\Error\Result\getFirstError
‪bool Error getFirstError()
Definition: Result.php:144
‪TYPO3\CMS\Extbase\Error\Result\flattenErrorTree
‪flattenErrorTree(array &$result, array $level)
Definition: Result.php:370
‪TYPO3\CMS\Extbase\Error\Result\getFirstNotice
‪bool Notice getFirstNotice()
Definition: Result.php:166
‪TYPO3\CMS\Extbase\Error\Result\$warnings
‪Warning[] $warnings
Definition: Result.php:36
‪TYPO3\CMS\Extbase\Error\Result\getNotices
‪Notice[] getNotices()
Definition: Result.php:134
‪TYPO3\CMS\Extbase\Error\Result\hasMessages
‪hasMessages()
Definition: Result.php:253