TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
Result.php
Go to the documentation of this file.
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  * */
29 class Result
30 {
34  protected $errors = [];
35 
40  protected $errorsExist = false;
41 
45  protected $warnings = [];
46 
51  protected $warningsExist = false;
52 
56  protected $notices = [];
57 
62  protected $noticesExist = false;
63 
69  protected $propertyResults = [];
70 
74  protected $parent = null;
75 
83  public function setParent(Result $parent)
84  {
85  if ($this->parent !== $parent) {
86  $this->parent = $parent;
87  if ($this->hasErrors()) {
88  $parent->setErrorsExist();
89  }
90  if ($this->hasWarnings()) {
91  $parent->setWarningsExist();
92  }
93  if ($this->hasNotices()) {
94  $parent->setNoticesExist();
95  }
96  }
97  }
98 
106  public function addError(\TYPO3\CMS\Extbase\Error\Error $error)
107  {
108  $this->errors[] = $error;
109  $this->setErrorsExist();
110  }
111 
119  public function addWarning(\TYPO3\CMS\Extbase\Error\Warning $warning)
120  {
121  $this->warnings[] = $warning;
122  $this->setWarningsExist();
123  }
124 
132  public function addNotice(\TYPO3\CMS\Extbase\Error\Notice $notice)
133  {
134  $this->notices[] = $notice;
135  $this->setNoticesExist();
136  }
137 
144  public function getErrors()
145  {
146  return $this->errors;
147  }
148 
155  public function getWarnings()
156  {
157  return $this->warnings;
158  }
159 
166  public function getNotices()
167  {
168  return $this->notices;
169  }
170 
177  public function getFirstError()
178  {
179  reset($this->errors);
180  return current($this->errors);
181  }
182 
189  public function getFirstWarning()
190  {
191  reset($this->warnings);
192  return current($this->warnings);
193  }
194 
201  public function getFirstNotice()
202  {
203  reset($this->notices);
204  return current($this->notices);
205  }
206 
217  public function forProperty($propertyPath)
218  {
219  if ($propertyPath === '' || $propertyPath === null) {
220  return $this;
221  }
222  if (strpos($propertyPath, '.') !== false) {
223  return $this->recurseThroughResult(explode('.', $propertyPath));
224  }
225  if (!isset($this->propertyResults[$propertyPath])) {
226  $this->propertyResults[$propertyPath] = new self();
227  $this->propertyResults[$propertyPath]->setParent($this);
228  }
229  return $this->propertyResults[$propertyPath];
230  }
231 
238  public function recurseThroughResult(array $pathSegments)
239  {
240  if (empty($pathSegments)) {
241  return $this;
242  }
243 
244  $propertyName = array_shift($pathSegments);
245 
246  if (!isset($this->propertyResults[$propertyName])) {
247  $this->propertyResults[$propertyName] = new self();
248  $this->propertyResults[$propertyName]->setParent($this);
249  }
250 
251  return $this->propertyResults[$propertyName]->recurseThroughResult($pathSegments);
252  }
253 
260  protected function setErrorsExist()
261  {
262  $this->errorsExist = true;
263  if ($this->parent !== null) {
264  $this->parent->setErrorsExist();
265  }
266  }
267 
274  protected function setWarningsExist()
275  {
276  $this->warningsExist = true;
277  if ($this->parent !== null) {
278  $this->parent->setWarningsExist();
279  }
280  }
281 
288  protected function setNoticesExist()
289  {
290  $this->noticesExist = true;
291  if ($this->parent !== null) {
292  $this->parent->setNoticesExist();
293  }
294  }
295 
301  public function hasMessages()
302  {
303  return $this->errorsExist || $this->noticesExist || $this->warningsExist;
304  }
305 
311  public function clear()
312  {
313  $this->errors = [];
314  $this->notices = [];
315  $this->warnings = [];
316 
317  $this->warningsExist = false;
318  $this->noticesExist = false;
319  $this->errorsExist = false;
320 
321  $this->propertyResults = [];
322  }
323 
331  protected function hasProperty($propertyName, $checkerMethodName)
332  {
333  if (!empty($this->{$propertyName})) {
334  return true;
335  }
336  foreach ($this->propertyResults as $subResult) {
337  if ($subResult->{$checkerMethodName}()) {
338  return true;
339  }
340  }
341  return false;
342  }
343 
350  public function hasErrors()
351  {
352  return $this->hasProperty('errors', 'hasErrors');
353  }
354 
361  public function hasWarnings()
362  {
363  return $this->hasProperty('warnings', 'hasWarnings');
364  }
365 
372  public function hasNotices()
373  {
374  return $this->hasProperty('notices', 'hasNotices');
375  }
376 
385  public function getFlattenedErrors()
386  {
387  $result = [];
388  $this->flattenTree('errors', $result, []);
389  return $result;
390  }
391 
400  public function getFlattenedWarnings()
401  {
402  $result = [];
403  $this->flattenTree('warnings', $result, []);
404  return $result;
405  }
406 
415  public function getFlattenedNotices()
416  {
417  $result = [];
418  $this->flattenTree('notices', $result, []);
419  return $result;
420  }
421 
432  public function flattenTree($propertyName, &$result, $level)
433  {
434  if (!empty($this->$propertyName)) {
435  $result[implode('.', $level)] = $this->$propertyName;
436  }
437  foreach ($this->propertyResults as $subPropertyName => $subResult) {
438  array_push($level, $subPropertyName);
439  $subResult->flattenTree($propertyName, $result, $level);
440  array_pop($level);
441  }
442  }
443 
451  public function merge(Result $otherResult)
452  {
453  if ($otherResult->errorsExist) {
454  $this->mergeProperty($otherResult, 'getErrors', 'addError');
455  }
456  if ($otherResult->warningsExist) {
457  $this->mergeProperty($otherResult, 'getWarnings', 'addWarning');
458  }
459  if ($otherResult->noticesExist) {
460  $this->mergeProperty($otherResult, 'getNotices', 'addNotice');
461  }
462 
463  foreach ($otherResult->getSubResults() as $subPropertyName => $subResult) {
465  if (array_key_exists($subPropertyName, $this->propertyResults) && $this->propertyResults[$subPropertyName]->hasMessages()) {
466  $this->forProperty($subPropertyName)->merge($subResult);
467  } else {
468  $this->propertyResults[$subPropertyName] = $subResult;
469  $subResult->setParent($this);
470  }
471  }
472  }
473 
482  protected function mergeProperty(Result $otherResult, $getterName, $adderName)
483  {
484  foreach ($otherResult->$getterName() as $messageInOtherResult) {
485  $this->$adderName($messageInOtherResult);
486  }
487  }
488 
494  public function getSubResults()
495  {
496  return $this->propertyResults;
497  }
498 }
forProperty($propertyPath)
Definition: Result.php:217
setParent(Result $parent)
Definition: Result.php:83
flattenTree($propertyName, &$result, $level)
Definition: Result.php:432
mergeProperty(Result $otherResult, $getterName, $adderName)
Definition: Result.php:482
addError(\TYPO3\CMS\Extbase\Error\Error $error)
Definition: Result.php:106
addNotice(\TYPO3\CMS\Extbase\Error\Notice $notice)
Definition: Result.php:132
recurseThroughResult(array $pathSegments)
Definition: Result.php:238
addWarning(\TYPO3\CMS\Extbase\Error\Warning $warning)
Definition: Result.php:119
hasProperty($propertyName, $checkerMethodName)
Definition: Result.php:331