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

94 lines
2.5 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 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;
protected $related = array();
2010-12-03 22:36:46 +01:00
public static $defaultTemplates = array(
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
);
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
{
foreach ($this->getIterator(true) as $e)
if ($e->getId() === $name)
return $e;
return false;
2010-12-03 22:36:46 +01:00
}
public function addRelated(ValidationException $related)
{
$this->related[] = $related;
return $this;
}
public function getMainMessage()
2010-12-03 22:36:46 +01:00
{
if (1 === count($this->related))
return $this->related[0]
->setName($this->getName())
->getMainMessage();
else
return parent::getMainMessage();
2010-12-03 22:36:46 +01:00
}
public function setRelated(array $relatedExceptions)
{
foreach ($relatedExceptions as $related)
$this->addRelated($related);
return $this;
}
2010-12-03 22:36:46 +01:00
public function getRelated($full=false)
{
if (!$full && 1 === count($this->related))
if ($this->related[0] instanceof AbstractCompositeException)
return $this->related[0]->getRelated(false);
else
return array();
else
return $this->related;
}
}