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
|
|
|
|
2015-01-30 02:53:20 +01:00
|
|
|
class AbstractNestedException extends ValidationException implements NestedValidationExceptionInterface
|
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-20 17:59:53 +01:00
|
|
|
|
2011-02-10 21:49:48 +01:00
|
|
|
protected $related = array();
|
2010-12-03 22:36:46 +01:00
|
|
|
|
2011-02-20 17:59:53 +01:00
|
|
|
public function addRelated(ValidationException $related)
|
2011-02-10 21:49:48 +01:00
|
|
|
{
|
2011-02-20 19:03:09 +01:00
|
|
|
$this->related[spl_object_hash($related)] = $related;
|
2013-01-14 22:55:03 +01:00
|
|
|
|
2011-02-20 17:59:53 +01:00
|
|
|
return $this;
|
2011-02-10 21:49:48 +01:00
|
|
|
}
|
|
|
|
|
2011-04-29 21:00:39 +02:00
|
|
|
public function findMessages(array $paths)
|
2011-03-31 00:06:59 +02:00
|
|
|
{
|
|
|
|
$messages = array();
|
|
|
|
|
2011-04-29 21:00:39 +02:00
|
|
|
foreach ($paths as $key => $value) {
|
|
|
|
$numericKey = is_numeric($key);
|
|
|
|
$path = $numericKey ? $value : $key;
|
|
|
|
|
|
|
|
$e = $this->findRelated($path);
|
|
|
|
|
2013-01-14 22:55:03 +01:00
|
|
|
if (is_object($e) && !$numericKey) {
|
2011-04-29 21:00:39 +02:00
|
|
|
$e->setTemplate($value);
|
2013-01-14 22:55:03 +01:00
|
|
|
}
|
2011-04-29 21:00:39 +02:00
|
|
|
|
|
|
|
$path = str_replace('.', '_', $path);
|
|
|
|
$messages[$path] = $e ? $e->getMainMessage() : '';
|
2011-03-31 00:06:59 +02:00
|
|
|
}
|
2013-01-14 22:55:03 +01:00
|
|
|
|
2011-03-31 00:06:59 +02:00
|
|
|
return $messages;
|
|
|
|
}
|
|
|
|
|
2011-04-29 21:00:39 +02:00
|
|
|
public function findRelated($path)
|
2011-02-18 04:08:46 +01:00
|
|
|
{
|
2011-02-20 17:59:53 +01:00
|
|
|
$target = $this;
|
2011-04-29 21:00:39 +02:00
|
|
|
$path = explode('.', $path);
|
2011-02-18 04:08:46 +01:00
|
|
|
|
2013-01-14 22:55:03 +01:00
|
|
|
while (!empty($path) && $target !== false) {
|
2011-04-27 19:35:12 +02:00
|
|
|
$target = $target->getRelatedByName(array_shift($path));
|
2013-01-14 22:55:03 +01:00
|
|
|
}
|
|
|
|
|
2011-02-20 17:59:53 +01:00
|
|
|
return $target;
|
2011-02-10 21:49:48 +01:00
|
|
|
}
|
2011-02-18 04:08:46 +01:00
|
|
|
|
2015-01-08 03:15:35 +01:00
|
|
|
public function getIterator($full = false, $mode = self::ITERATE_ALL)
|
2011-02-10 21:49:48 +01:00
|
|
|
{
|
|
|
|
$exceptionIterator = new ExceptionIterator($this, $full);
|
2011-02-20 17:59:53 +01:00
|
|
|
|
2013-01-14 22:55:03 +01:00
|
|
|
if ($mode == self::ITERATE_ALL) {
|
2011-02-10 21:49:48 +01:00
|
|
|
return new RecursiveIteratorIterator($exceptionIterator, 1);
|
2013-01-14 22:55:03 +01:00
|
|
|
} else {
|
2011-02-10 21:49:48 +01:00
|
|
|
return new RecursiveTreeIterator($exceptionIterator);
|
2013-01-14 22:55:03 +01:00
|
|
|
}
|
2011-02-10 21:49:48 +01:00
|
|
|
}
|
|
|
|
|
2011-02-20 17:59:53 +01:00
|
|
|
public function getFullMessage()
|
2011-02-10 21:49:48 +01:00
|
|
|
{
|
2011-02-20 17:59:53 +01:00
|
|
|
$message = array();
|
2012-07-25 00:46:49 +02:00
|
|
|
$iterator = $this->getIterator(false, self::ITERATE_TREE);
|
2013-01-14 22:55:03 +01:00
|
|
|
foreach ($iterator as $m) {
|
2011-02-20 17:59:53 +01:00
|
|
|
$message[] = $m;
|
2013-01-14 22:55:03 +01:00
|
|
|
}
|
|
|
|
|
2011-02-20 17:59:53 +01:00
|
|
|
return implode(PHP_EOL, $message);
|
|
|
|
}
|
|
|
|
|
2015-01-08 03:15:35 +01:00
|
|
|
public function getRelated($full = false)
|
2011-02-20 17:59:53 +01:00
|
|
|
{
|
2013-01-14 22:55:03 +01:00
|
|
|
if (!$full && 1 === count($this->related)
|
|
|
|
&& current($this->related) instanceof AbstractNestedException) {
|
2012-12-04 17:28:55 +01:00
|
|
|
return current($this->related)->getRelated();
|
2013-01-14 22:55:03 +01:00
|
|
|
} else {
|
2012-12-04 17:28:55 +01:00
|
|
|
return $this->related;
|
2013-01-14 22:55:03 +01:00
|
|
|
}
|
2011-02-10 21:49:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getRelatedByName($name)
|
2010-12-03 22:36:46 +01:00
|
|
|
{
|
2013-01-14 22:55:03 +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;
|
2013-01-14 22:55:03 +01:00
|
|
|
}
|
|
|
|
}
|
2011-02-20 17:59:53 +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 setRelated(array $relatedExceptions)
|
|
|
|
{
|
2013-01-14 22:55:03 +01:00
|
|
|
foreach ($relatedExceptions as $related) {
|
2011-02-10 21:49:48 +01:00
|
|
|
$this->addRelated($related);
|
2013-01-14 22:55:03 +01:00
|
|
|
}
|
2011-02-19 20:47:34 +01:00
|
|
|
|
2011-02-20 17:59:53 +01:00
|
|
|
return $this;
|
2011-02-02 03:28:02 +01:00
|
|
|
}
|
2011-02-10 21:49:48 +01:00
|
|
|
}
|