respect-validation/library/Respect/Validation/Exceptions/AbstractNestedException.php

82 lines
1.9 KiB
PHP
Raw Normal View History

2010-12-03 22:36:46 +01:00
<?php
namespace Respect\Validation\Exceptions;
use RecursiveIteratorIterator;
use RecursiveTreeIterator;
use Respect\Validation\ExceptionIterator;
2010-12-03 22:36:46 +01:00
class AbstractNestedException extends ValidationException
2010-12-03 22:36:46 +01:00
{
const ITERATE_TREE = 1;
const ITERATE_ALL = 2;
protected $related = array();
2010-12-03 22:36:46 +01:00
public function addRelated(ValidationException $related)
{
2011-02-20 19:03:09 +01:00
$this->related[spl_object_hash($related)] = $related;
return $this;
}
public function findRelated()
{
$target = $this;
$path = func_get_args();
while (!empty($path) && $target !== false)
$target = $this->getRelatedByName(array_shift($path));
return $target;
}
public function getIterator($full=false, $mode=self::ITERATE_ALL)
{
$exceptionIterator = new ExceptionIterator($this, $full);
if ($mode == self::ITERATE_ALL)
return new RecursiveIteratorIterator($exceptionIterator, 1);
else
return new RecursiveTreeIterator($exceptionIterator);
}
public function getFullMessage()
{
$message = array();
foreach ($this->getIterator(false, self::ITERATE_TREE) as $m)
$message[] = $m;
return implode(PHP_EOL, $message);
}
public function getRelated($full=false)
{
return $this->related;
}
public function getRelatedByName($name)
2010-12-03 22:36:46 +01:00
{
foreach ($this->getIterator(true) as $e)
if ($e->getId() === $name || $e->getName() === $name)
return $e;
return false;
2010-12-03 22:36:46 +01:00
}
public function setContext($context)
{
parent::setContext($context);
foreach ($this->related as $r)
$r->setContext($context);
2010-12-03 22:36:46 +01:00
}
public function setRelated(array $relatedExceptions)
{
foreach ($relatedExceptions as $related)
$this->addRelated($related);
2011-02-19 20:47:34 +01:00
return $this;
}
}