respect-validation/library/Exceptions/AbstractNestedException.php

144 lines
3.4 KiB
PHP
Raw Normal View History

2010-12-03 22:36:46 +01:00
<?php
2015-06-08 16:47:14 +02:00
/*
* This file is part of Respect/Validation.
*
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
*
* For the full copyright and license information, please view the "LICENSE.md"
* file that was distributed with this source code.
*/
2010-12-03 22:36:46 +01:00
namespace Respect\Validation\Exceptions;
use RecursiveIteratorIterator;
use RecursiveTreeIterator;
use Respect\Validation\ExceptionIterator;
2010-12-03 22:36:46 +01:00
class AbstractNestedException extends ValidationException implements NestedValidationExceptionInterface
2010-12-03 22:36:46 +01:00
{
const ITERATE_TREE = 1;
const ITERATE_ALL = 2;
2015-10-18 03:44:47 +02:00
protected $related = [];
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 findMessages(array $paths)
{
2015-10-18 03:44:47 +02:00
$messages = [];
foreach ($paths as $key => $value) {
$numericKey = is_numeric($key);
$path = $numericKey ? $value : $key;
$e = $this->findRelated($path);
if (is_object($e) && !$numericKey) {
$e->setTemplate($value);
}
$path = str_replace('.', '_', $path);
$messages[$path] = $e ? $e->getMainMessage() : '';
}
return $messages;
}
public function findRelated($path)
{
$target = $this;
$path = explode('.', $path);
while (!empty($path) && $target !== false) {
$target = $target->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 getMessages()
{
2015-10-18 03:44:47 +02:00
$messages = [];
foreach ($this->getIterator() as $key => $exception) {
if ($key === 0) {
continue;
}
$messages[] = $exception->getMessage();
}
return $messages;
}
public function getFullMessage()
{
2015-10-18 03:44:47 +02:00
$message = [];
$iterator = $this->getIterator(false, self::ITERATE_TREE);
foreach ($iterator as $m) {
$message[] = $m;
}
return implode(PHP_EOL, $message);
}
public function getRelated($full = false)
{
if (!$full && 1 === count($this->related)
2015-06-08 16:47:14 +02:00
&& current($this->related) instanceof self) {
return current($this->related)->getRelated();
} else {
return $this->related;
}
}
public function setParam($name, $value)
{
if ('translator' === $name) {
foreach ($this->getRelated(true) as $related) {
$related->setParam($name, $value);
}
}
parent::setParam($name, $value);
return $this;
}
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 setRelated(array $relatedExceptions)
{
foreach ($relatedExceptions as $related) {
$this->addRelated($related);
}
2011-02-19 20:47:34 +01:00
return $this;
}
}