2010-12-03 22:36:46 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Respect\Validation\Exceptions;
|
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-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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setContext($context)
|
|
|
|
{
|
|
|
|
parent::setContext($context);
|
|
|
|
foreach ($this->related as $r)
|
|
|
|
$r->setContext($context);
|
|
|
|
}
|
|
|
|
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-10 21:49:48 +01:00
|
|
|
foreach ($this->getIterator(true) as $e)
|
|
|
|
if ($e->getId() === $name)
|
|
|
|
return $e;
|
|
|
|
return false;
|
2010-12-03 22:36:46 +01:00
|
|
|
}
|
|
|
|
|
2011-02-10 21:49:48 +01:00
|
|
|
public function addRelated(ValidationException $related)
|
|
|
|
{
|
|
|
|
$this->related[] = $related;
|
|
|
|
return $this;
|
|
|
|
}
|
2011-01-31 01:17:06 +01:00
|
|
|
public function getMainMessage()
|
2010-12-03 22:36:46 +01:00
|
|
|
{
|
2011-01-31 01:17:06 +01:00
|
|
|
if (1 === count($this->related))
|
2011-02-08 23:10:58 +01:00
|
|
|
return $this->related[0]
|
|
|
|
->setName($this->getName())
|
|
|
|
->getMainMessage();
|
2011-01-31 01:17:06 +01:00
|
|
|
else
|
|
|
|
return parent::getMainMessage();
|
2010-12-03 22:36: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)
|
|
|
|
{
|
|
|
|
if (!$full && 1 === count($this->related))
|
2011-02-10 21:49:48 +01:00
|
|
|
if ($this->related[0] instanceof AbstractCompositeException)
|
|
|
|
return $this->related[0]->getRelated(false);
|
|
|
|
else
|
|
|
|
return array();
|
2011-02-02 03:28:02 +01:00
|
|
|
else
|
2011-02-10 21:49:48 +01:00
|
|
|
return $this->related;
|
2011-02-02 03:28:02 +01:00
|
|
|
}
|
|
|
|
|
2011-02-10 21:49:48 +01:00
|
|
|
}
|