2010-06-24 09:40:05 +01:00
|
|
|
<?php
|
|
|
|
|
2010-10-02 11:42:31 +01:00
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2010-10-02 11:42:31 +01:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-10-02 11:42:31 +01:00
|
|
|
*
|
2011-01-15 13:29:43 +00:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2010-10-02 11:42:31 +01:00
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\Validator;
|
|
|
|
|
2011-01-16 04:41:23 +00:00
|
|
|
/**
|
2012-01-31 06:45:20 +00:00
|
|
|
* A list of ConstrainViolation objects.
|
2012-01-30 19:56:10 +00:00
|
|
|
*
|
|
|
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
2011-07-20 09:37:57 +01:00
|
|
|
*
|
|
|
|
* @api
|
2011-01-16 04:41:23 +00:00
|
|
|
*/
|
2011-03-28 13:54:37 +01:00
|
|
|
class ConstraintViolationList implements \IteratorAggregate, \Countable, \ArrayAccess
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2012-01-30 19:56:10 +00:00
|
|
|
/**
|
|
|
|
* The constraint violations
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-06-24 10:24:08 +01:00
|
|
|
protected $violations = array();
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2012-01-30 19:56:10 +00:00
|
|
|
/**
|
2012-01-31 06:45:20 +00:00
|
|
|
* Creates a new constraint violation list.
|
2012-01-30 19:56:10 +00:00
|
|
|
*
|
|
|
|
* @param array $violations The constraint violations to add to the list
|
|
|
|
*/
|
|
|
|
public function __construct(array $violations = array())
|
|
|
|
{
|
|
|
|
foreach ($violations as $violation) {
|
|
|
|
$this->add($violation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-16 04:41:23 +00:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2010-06-24 10:24:08 +01:00
|
|
|
public function __toString()
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
$string = '';
|
|
|
|
|
|
|
|
foreach ($this->violations as $violation) {
|
2010-07-01 01:10:28 +01:00
|
|
|
$root = $violation->getRoot();
|
2012-01-28 14:32:24 +00:00
|
|
|
$class = (string) (is_object($root) ? get_class($root) : $root);
|
|
|
|
$propertyPath = (string) $violation->getPropertyPath();
|
2012-01-31 06:39:54 +00:00
|
|
|
if ('' !== $propertyPath && '[' !== $propertyPath[0] && '' !== $class) {
|
2012-01-28 14:32:24 +00:00
|
|
|
$class .= '.';
|
|
|
|
}
|
2010-06-24 10:24:08 +01:00
|
|
|
$string .= <<<EOF
|
2012-01-28 14:32:24 +00:00
|
|
|
{$class}{$propertyPath}:
|
2010-07-01 01:10:28 +01:00
|
|
|
{$violation->getMessage()}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
|
|
|
EOF;
|
2010-06-24 10:24:08 +01:00
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
return $string;
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2011-01-16 04:41:23 +00:00
|
|
|
/**
|
|
|
|
* Add a ConstraintViolation to this list.
|
|
|
|
*
|
|
|
|
* @param ConstraintViolation $violation
|
2011-07-20 09:37:57 +01:00
|
|
|
*
|
|
|
|
* @api
|
2011-01-16 04:41:23 +00:00
|
|
|
*/
|
2010-06-24 10:24:08 +01:00
|
|
|
public function add(ConstraintViolation $violation)
|
|
|
|
{
|
|
|
|
$this->violations[] = $violation;
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2011-01-16 04:41:23 +00:00
|
|
|
/**
|
|
|
|
* Merge an existing ConstraintViolationList into this list.
|
|
|
|
*
|
2012-01-31 20:35:48 +00:00
|
|
|
* @param ConstraintViolationList $otherList
|
2011-07-20 09:37:57 +01:00
|
|
|
*
|
|
|
|
* @api
|
2011-01-16 04:41:23 +00:00
|
|
|
*/
|
2012-01-31 20:35:48 +00:00
|
|
|
public function addAll(ConstraintViolationList $otherList)
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2012-01-31 20:35:48 +00:00
|
|
|
foreach ($otherList->violations as $violation) {
|
2010-06-24 10:24:08 +01:00
|
|
|
$this->violations[] = $violation;
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
|
|
|
|
2011-01-16 04:41:23 +00:00
|
|
|
/**
|
|
|
|
* @see IteratorAggregate
|
2011-07-20 09:37:57 +01:00
|
|
|
*
|
|
|
|
* @api
|
2011-01-16 04:41:23 +00:00
|
|
|
*/
|
2010-06-24 10:24:08 +01:00
|
|
|
public function getIterator()
|
|
|
|
{
|
|
|
|
return new \ArrayIterator($this->violations);
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2011-01-16 04:41:23 +00:00
|
|
|
/**
|
|
|
|
* @see Countable
|
2011-07-20 09:37:57 +01:00
|
|
|
*
|
|
|
|
* @api
|
2011-01-16 04:41:23 +00:00
|
|
|
*/
|
2010-06-24 10:24:08 +01:00
|
|
|
public function count()
|
|
|
|
{
|
|
|
|
return count($this->violations);
|
|
|
|
}
|
2011-03-28 13:54:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @see ArrayAccess
|
2011-07-20 09:37:57 +01:00
|
|
|
*
|
|
|
|
* @api
|
2011-03-28 13:54:37 +01:00
|
|
|
*/
|
|
|
|
public function offsetExists($offset)
|
|
|
|
{
|
|
|
|
return isset($this->violations[$offset]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see ArrayAccess
|
2011-07-20 09:37:57 +01:00
|
|
|
*
|
|
|
|
* @api
|
2011-03-28 13:54:37 +01:00
|
|
|
*/
|
|
|
|
public function offsetGet($offset)
|
|
|
|
{
|
|
|
|
return isset($this->violations[$offset]) ? $this->violations[$offset] : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see ArrayAccess
|
2011-07-20 09:37:57 +01:00
|
|
|
*
|
|
|
|
* @api
|
2011-03-28 13:54:37 +01:00
|
|
|
*/
|
|
|
|
public function offsetSet($offset, $value)
|
|
|
|
{
|
2011-03-30 08:43:05 +01:00
|
|
|
if (null === $offset) {
|
2011-03-28 13:54:37 +01:00
|
|
|
$this->violations[] = $value;
|
|
|
|
} else {
|
|
|
|
$this->violations[$offset] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see ArrayAccess
|
2011-07-20 09:37:57 +01:00
|
|
|
*
|
|
|
|
* @api
|
2011-03-28 13:54:37 +01:00
|
|
|
*/
|
|
|
|
public function offsetUnset($offset)
|
|
|
|
{
|
|
|
|
unset($this->violations[$offset]);
|
|
|
|
}
|
2011-06-08 11:12:55 +01:00
|
|
|
|
2011-06-08 11:16:48 +01:00
|
|
|
}
|