TYPO3 CMS  TYPO3_6-2
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 = array();
35 
40  protected $errorsExist = FALSE;
41 
45  protected $warnings = array();
46 
51  protected $warningsExist = FALSE;
52 
56  protected $notices = array();
57 
62  protected $noticesExist = FALSE;
63 
69  protected $propertyResults = array();
70 
74  protected $parent = NULL;
75 
83  public function setParent(Result $parent) {
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 
105  public function addError(\TYPO3\CMS\Extbase\Error\Error $error) {
106  $this->errors[] = $error;
107  $this->setErrorsExist();
108  }
109 
117  public function addWarning(\TYPO3\CMS\Extbase\Error\Warning $warning) {
118  $this->warnings[] = $warning;
119  $this->setWarningsExist();
120  }
121 
129  public function addNotice(\TYPO3\CMS\Extbase\Error\Notice $notice) {
130  $this->notices[] = $notice;
131  $this->setNoticesExist();
132  }
133 
140  public function getErrors() {
141  return $this->errors;
142  }
143 
150  public function getWarnings() {
151  return $this->warnings;
152  }
153 
160  public function getNotices() {
161  return $this->notices;
162  }
163 
170  public function getFirstError() {
171  reset($this->errors);
172  return current($this->errors);
173  }
174 
181  public function getFirstWarning() {
182  reset($this->warnings);
183  return current($this->warnings);
184  }
185 
192  public function getFirstNotice() {
193  reset($this->notices);
194  return current($this->notices);
195  }
196 
207  public function forProperty($propertyPath) {
208  if ($propertyPath === '' || $propertyPath === NULL) {
209  return $this;
210  }
211  if (strpos($propertyPath, '.') !== FALSE) {
212  return $this->recurseThroughResult(explode('.', $propertyPath));
213  }
214  if (!isset($this->propertyResults[$propertyPath])) {
215  $this->propertyResults[$propertyPath] = new Result();
216  $this->propertyResults[$propertyPath]->setParent($this);
217  }
218  return $this->propertyResults[$propertyPath];
219  }
220 
227  public function recurseThroughResult(array $pathSegments) {
228  if (count($pathSegments) === 0) {
229  return $this;
230  }
231 
232  $propertyName = array_shift($pathSegments);
233 
234  if (!isset($this->propertyResults[$propertyName])) {
235  $this->propertyResults[$propertyName] = new Result();
236  $this->propertyResults[$propertyName]->setParent($this);
237  }
238 
239  return $this->propertyResults[$propertyName]->recurseThroughResult($pathSegments);
240  }
241 
248  protected function setErrorsExist() {
249  $this->errorsExist = TRUE;
250  if ($this->parent !== NULL) {
251  $this->parent->setErrorsExist();
252  }
253  }
254 
261  protected function setWarningsExist() {
262  $this->warningsExist = TRUE;
263  if ($this->parent !== NULL) {
264  $this->parent->setWarningsExist();
265  }
266  }
267 
274  protected function setNoticesExist() {
275  $this->noticesExist = TRUE;
276  if ($this->parent !== NULL) {
277  $this->parent->setNoticesExist();
278  }
279  }
280 
286  public function hasMessages() {
287  return $this->errorsExist || $this->noticesExist || $this->warningsExist;
288  }
289 
295  public function clear() {
296  $this->errors = array();
297  $this->notices = array();
298  $this->warnings = array();
299 
300  $this->warningsExist = FALSE;
301  $this->noticesExist = FALSE;
302  $this->errorsExist = FALSE;
303 
304  $this->propertyResults = array();
305  }
306 
314  protected function hasProperty($propertyName, $checkerMethodName) {
315  if (count($this->{$propertyName}) > 0) {
316  return TRUE;
317  }
318  foreach ($this->propertyResults as $subResult) {
319  if ($subResult->{$checkerMethodName}()) {
320  return TRUE;
321  }
322  }
323  return FALSE;
324  }
325 
332  public function hasErrors() {
333  return $this->hasProperty('errors', 'hasErrors');
334  }
335 
342  public function hasWarnings() {
343  return $this->hasProperty('warnings', 'hasWarnings');
344  }
345 
352  public function hasNotices() {
353  return $this->hasProperty('notices', 'hasNotices');
354  }
355 
364  public function getFlattenedErrors() {
365  $result = array();
366  $this->flattenTree('errors', $result, array());
367  return $result;
368  }
369 
378  public function getFlattenedWarnings() {
379  $result = array();
380  $this->flattenTree('warnings', $result, array());
381  return $result;
382  }
383 
392  public function getFlattenedNotices() {
393  $result = array();
394  $this->flattenTree('notices', $result, array());
395  return $result;
396  }
397 
408  public function flattenTree($propertyName, &$result, $level) {
409  if (count($this->$propertyName) > 0) {
410  $result[implode('.', $level)] = $this->$propertyName;
411  }
412  foreach ($this->propertyResults as $subPropertyName => $subResult) {
413  array_push($level, $subPropertyName);
414  $subResult->flattenTree($propertyName, $result, $level);
415  array_pop($level);
416  }
417  }
418 
426  public function merge(Result $otherResult) {
427  if ($otherResult->errorsExist) {
428  $this->mergeProperty($otherResult, 'getErrors', 'addError');
429  }
430  if ($otherResult->warningsExist) {
431  $this->mergeProperty($otherResult, 'getWarnings', 'addWarning');
432  }
433  if ($otherResult->noticesExist) {
434  $this->mergeProperty($otherResult, 'getNotices', 'addNotice');
435  }
436 
437  foreach ($otherResult->getSubResults() as $subPropertyName => $subResult) {
439  if (array_key_exists($subPropertyName, $this->propertyResults) && $this->propertyResults[$subPropertyName]->hasMessages()) {
440  $this->forProperty($subPropertyName)->merge($subResult);
441  } else {
442  $this->propertyResults[$subPropertyName] = $subResult;
443  $subResult->setParent($this);
444  }
445  }
446  }
447 
456  protected function mergeProperty(Result $otherResult, $getterName, $adderName) {
457  foreach ($otherResult->$getterName() as $messageInOtherResult) {
458  $this->$adderName($messageInOtherResult);
459  }
460  }
461 
467  public function getSubResults() {
468  return $this->propertyResults;
469  }
470 }
hasProperty($propertyName, $checkerMethodName)
Definition: Result.php:314
flattenTree($propertyName, &$result, $level)
Definition: Result.php:408
mergeProperty(Result $otherResult, $getterName, $adderName)
Definition: Result.php:456
forProperty($propertyPath)
Definition: Result.php:207
if($list_of_literals) if(!empty($literals)) if(!empty($literals)) $result
Analyse literals to prepend the N char to them if their contents aren&#39;t numeric.
addWarning(\TYPO3\CMS\Extbase\Error\Warning $warning)
Definition: Result.php:117
setParent(Result $parent)
Definition: Result.php:83
recurseThroughResult(array $pathSegments)
Definition: Result.php:227
addNotice(\TYPO3\CMS\Extbase\Error\Notice $notice)
Definition: Result.php:129
addError(\TYPO3\CMS\Extbase\Error\Error $error)
Definition: Result.php:105