2010-12-03 22:36:46 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Respect\Validation\Exceptions;
|
2011-02-18 04:08:46 +01:00
|
|
|
|
2011-02-10 21:49:48 +01:00
|
|
|
use RecursiveIteratorIterator;
|
|
|
|
use RecursiveTreeIterator;
|
|
|
|
use Respect\Validation\ExceptionIterator;
|
2010-12-03 22:36:46 +01:00
|
|
|
|
2011-02-10 21:49:48 +01:00
|
|
|
class AbstractCompositeException extends ValidationException
|
2010-12-03 22:36:46 +01:00
|
|
|
{
|
2011-02-18 04:08:46 +01:00
|
|
|
const ITERATE_TREE = 1;
|
|
|
|
const ITERATE_ALL = 2;
|
2011-02-07 02:12:41 +01:00
|
|
|
const NONE = 0;
|
|
|
|
const SOME = 1;
|
2011-02-10 21:49:48 +01:00
|
|
|
protected $related = array();
|
2010-12-03 22:36:46 +01:00
|
|
|
public static $defaultTemplates = array(
|
2011-02-10 21:49:48 +01:00
|
|
|
self::NONE => 'All of the required rules must pass for {{name}}',
|
|
|
|
self::SOME => 'These rules must pass for {{name}}',
|
2010-12-03 22:36:46 +01:00
|
|
|
);
|
|
|
|
|
2011-02-10 21:49:48 +01:00
|
|
|
public function chooseTemplate()
|
|
|
|
{
|
|
|
|
$numRules = $this->getParam('passed');
|
|
|
|
$numFailed = count($this->getRelated());
|
|
|
|
return $numRules === $numFailed ? static::NONE : static::SOME;
|
|
|
|
}
|
|
|
|
|
2011-02-18 04:08:46 +01:00
|
|
|
public function getFullMessage()
|
|
|
|
{
|
|
|
|
$message = array();
|
|
|
|
foreach ($this->getIterator(false, self::ITERATE_TREE) as $m)
|
|
|
|
$message[] = $m;
|
|
|
|
return implode(PHP_EOL, $message);
|
|
|
|
}
|
|
|
|
|
2011-02-10 21:49:48 +01:00
|
|
|
public function setContext($context)
|
|
|
|
{
|
|
|
|
parent::setContext($context);
|
|
|
|
foreach ($this->related as $r)
|
|
|
|
$r->setContext($context);
|
|
|
|
}
|
2011-02-18 04:08:46 +01:00
|
|
|
|
2011-02-10 21:49:48 +01:00
|
|
|
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 findRelated()
|
|
|
|
{
|
|
|
|
$target = $this;
|
|
|
|
$path = func_get_args();
|
|
|
|
while (!empty($path) && $target !== false)
|
|
|
|
$target = $this->getRelatedByName(array_shift($path));
|
|
|
|
return $target;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRelatedByName($name)
|
2010-12-03 22:36:46 +01:00
|
|
|
{
|
2011-02-18 04:08:46 +01:00
|
|
|
foreach ($this->getIterator(true) as $e) {
|
|
|
|
if ($e->getId() === $name || $e->getName() === $name)
|
2011-02-10 21:49:48 +01:00
|
|
|
return $e;
|
2011-02-18 04:08:46 +01:00
|
|
|
}
|
2011-02-10 21:49:48 +01:00
|
|
|
return false;
|
2010-12-03 22:36:46 +01:00
|
|
|
}
|
|
|
|
|
2011-02-10 21:49:48 +01:00
|
|
|
public function addRelated(ValidationException $related)
|
|
|
|
{
|
2011-02-18 04:08:46 +01:00
|
|
|
if ($related instanceof static)
|
|
|
|
$related->setName($this->getName());
|
2011-02-10 21:49:48 +01:00
|
|
|
$this->related[] = $related;
|
|
|
|
return $this;
|
|
|
|
}
|
2011-02-18 04:08:46 +01:00
|
|
|
|
|
|
|
public function setName($name)
|
2010-12-03 22:36:46 +01:00
|
|
|
{
|
2011-02-18 04:08:46 +01:00
|
|
|
foreach ($this->related as $r)
|
|
|
|
if ($r instanceof static)
|
|
|
|
$r->setName($name);
|
|
|
|
return parent::setName($name);
|
2010-12-03 22:36:46 +01:00
|
|
|
}
|
2011-02-18 04:08:46 +01:00
|
|
|
|
2011-02-10 21:49:48 +01:00
|
|
|
public function setRelated(array $relatedExceptions)
|
|
|
|
{
|
|
|
|
foreach ($relatedExceptions as $related)
|
|
|
|
$this->addRelated($related);
|
|
|
|
return $this;
|
|
|
|
}
|
2010-12-03 22:36:46 +01:00
|
|
|
|
2011-02-02 03:28:02 +01:00
|
|
|
public function getRelated($full=false)
|
|
|
|
{
|
2011-02-19 02:07:14 +01:00
|
|
|
if (!$full
|
|
|
|
&& 1 === count($this->related)
|
|
|
|
&& $this->related[0] instanceof self)
|
2011-02-18 04:08:46 +01:00
|
|
|
return $this->related[0]->getRelated();
|
|
|
|
else
|
2011-02-19 02:07:14 +01:00
|
|
|
return $this->related;
|
2011-02-18 04:08:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getMainMessage()
|
|
|
|
{
|
2011-02-19 02:07:14 +01:00
|
|
|
if (1 === count($this->related))
|
2011-02-18 04:08:46 +01:00
|
|
|
return $this->related[0]->getMainMessage();
|
|
|
|
else
|
|
|
|
return parent::getMainMessage();
|
2011-02-02 03:28:02 +01:00
|
|
|
}
|
|
|
|
|
2011-02-10 21:49:48 +01:00
|
|
|
}
|