TYPO3 CMS  TYPO3_8-7
Result.php
Go to the documentation of this file.
1 <?php
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 
82  public function setParent(Result $parent)
83  {
84  if ($this->parent !== $parent) {
85  $this->parent = $parent;
86  if ($this->hasErrors()) {
87  $parent->setErrorsExist();
88  }
89  if ($this->hasWarnings()) {
90  $parent->setWarningsExist();
91  }
92  if ($this->hasNotices()) {
93  $parent->setNoticesExist();
94  }
95  }
96  }
97 
104  public function addError(Error $error)
105  {
106  $this->errors[] = $error;
107  $this->setErrorsExist();
108  }
109 
116  public function addWarning(Warning $warning)
117  {
118  $this->warnings[] = $warning;
119  $this->setWarningsExist();
120  }
121 
128  public function addNotice(Notice $notice)
129  {
130  $this->notices[] = $notice;
131  $this->setNoticesExist();
132  }
133 
140  public function getErrors()
141  {
142  return $this->errors;
143  }
144 
151  public function getWarnings()
152  {
153  return $this->warnings;
154  }
155 
162  public function getNotices()
163  {
164  return $this->notices;
165  }
166 
173  public function getFirstError()
174  {
175  reset($this->errors);
176  return current($this->errors);
177  }
178 
185  public function getFirstWarning()
186  {
187  reset($this->warnings);
188  return current($this->warnings);
189  }
190 
197  public function getFirstNotice()
198  {
199  reset($this->notices);
200  return current($this->notices);
201  }
202 
213  public function forProperty($propertyPath)
214  {
215  if ($propertyPath === '' || $propertyPath === null) {
216  return $this;
217  }
218  if (strpos($propertyPath, '.') !== false) {
219  return $this->recurseThroughResult(explode('.', $propertyPath));
220  }
221  if (!isset($this->propertyResults[$propertyPath])) {
222  $this->propertyResults[$propertyPath] = new self();
223  $this->propertyResults[$propertyPath]->setParent($this);
224  }
225  return $this->propertyResults[$propertyPath];
226  }
227 
234  public function recurseThroughResult(array $pathSegments)
235  {
236  if (empty($pathSegments)) {
237  return $this;
238  }
239 
240  $propertyName = array_shift($pathSegments);
241 
242  if (!isset($this->propertyResults[$propertyName])) {
243  $this->propertyResults[$propertyName] = new self();
244  $this->propertyResults[$propertyName]->setParent($this);
245  }
246 
247  return $this->propertyResults[$propertyName]->recurseThroughResult($pathSegments);
248  }
249 
254  protected function setErrorsExist()
255  {
256  $this->errorsExist = true;
257  if ($this->parent !== null) {
258  $this->parent->setErrorsExist();
259  }
260  }
261 
266  protected function setWarningsExist()
267  {
268  $this->warningsExist = true;
269  if ($this->parent !== null) {
270  $this->parent->setWarningsExist();
271  }
272  }
273 
278  protected function setNoticesExist()
279  {
280  $this->noticesExist = true;
281  if ($this->parent !== null) {
282  $this->parent->setNoticesExist();
283  }
284  }
285 
291  public function hasMessages()
292  {
293  return $this->errorsExist || $this->noticesExist || $this->warningsExist;
294  }
295 
299  public function clear()
300  {
301  $this->errors = [];
302  $this->notices = [];
303  $this->warnings = [];
304 
305  $this->warningsExist = false;
306  $this->noticesExist = false;
307  $this->errorsExist = false;
308 
309  $this->propertyResults = [];
310  }
311 
319  protected function hasProperty($propertyName, $checkerMethodName)
320  {
321  if (!empty($this->{$propertyName})) {
322  return true;
323  }
324  foreach ($this->propertyResults as $subResult) {
325  if ($subResult->{$checkerMethodName}()) {
326  return true;
327  }
328  }
329  return false;
330  }
331 
338  public function hasErrors()
339  {
340  return $this->hasProperty('errors', 'hasErrors');
341  }
342 
349  public function hasWarnings()
350  {
351  return $this->hasProperty('warnings', 'hasWarnings');
352  }
353 
360  public function hasNotices()
361  {
362  return $this->hasProperty('notices', 'hasNotices');
363  }
364 
373  public function getFlattenedErrors()
374  {
375  $result = [];
376  $this->flattenTree('errors', $result, []);
377  return $result;
378  }
379 
388  public function getFlattenedWarnings()
389  {
390  $result = [];
391  $this->flattenTree('warnings', $result, []);
392  return $result;
393  }
394 
403  public function getFlattenedNotices()
404  {
405  $result = [];
406  $this->flattenTree('notices', $result, []);
407  return $result;
408  }
409 
419  public function flattenTree($propertyName, &$result, $level)
420  {
421  if (!empty($this->$propertyName)) {
422  $result[implode('.', $level)] = $this->$propertyName;
423  }
424  foreach ($this->propertyResults as $subPropertyName => $subResult) {
425  $level[] = $subPropertyName;
426  $subResult->flattenTree($propertyName, $result, $level);
427  array_pop($level);
428  }
429  }
430 
437  public function merge(Result $otherResult)
438  {
439  if ($otherResult->errorsExist) {
440  $this->mergeProperty($otherResult, 'getErrors', 'addError');
441  }
442  if ($otherResult->warningsExist) {
443  $this->mergeProperty($otherResult, 'getWarnings', 'addWarning');
444  }
445  if ($otherResult->noticesExist) {
446  $this->mergeProperty($otherResult, 'getNotices', 'addNotice');
447  }
448 
449  foreach ($otherResult->getSubResults() as $subPropertyName => $subResult) {
451  if (array_key_exists($subPropertyName, $this->propertyResults) && $this->propertyResults[$subPropertyName]->hasMessages()) {
452  $this->forProperty($subPropertyName)->merge($subResult);
453  } else {
454  $this->propertyResults[$subPropertyName] = $subResult;
455  $subResult->setParent($this);
456  }
457  }
458  }
459 
467  protected function mergeProperty(Result $otherResult, $getterName, $adderName)
468  {
469  foreach ($otherResult->$getterName() as $messageInOtherResult) {
470  $this->$adderName($messageInOtherResult);
471  }
472  }
473 
479  public function getSubResults()
480  {
481  return $this->propertyResults;
482  }
483 }
hasProperty($propertyName, $checkerMethodName)
Definition: Result.php:319
flattenTree($propertyName, &$result, $level)
Definition: Result.php:419
mergeProperty(Result $otherResult, $getterName, $adderName)
Definition: Result.php:467
forProperty($propertyPath)
Definition: Result.php:213
addWarning(Warning $warning)
Definition: Result.php:116
setParent(Result $parent)
Definition: Result.php:82
recurseThroughResult(array $pathSegments)
Definition: Result.php:234
addNotice(Notice $notice)
Definition: Result.php:128