This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Validator/ConstraintViolationList.php

147 lines
2.8 KiB
PHP
Raw Normal View History

<?php
2010-10-02 11:42:31 +01:00
/*
* This file is part of the Symfony package.
2010-10-02 11:42:31 +01:00
*
* (c) Fabien Potencier <fabien@symfony.com>
2010-10-02 11:42:31 +01: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
*/
namespace Symfony\Component\Validator;
/**
2012-01-31 06:45:20 +00:00
* A list of ConstrainViolation objects.
*
* @author Bernhard Schussek <bschussek@gmail.com>
2011-07-20 09:37:57 +01:00
*
* @api
*/
2011-03-28 13:54:37 +01:00
class ConstraintViolationList implements \IteratorAggregate, \Countable, \ArrayAccess
{
/**
* The constraint violations
*
* @var array
*/
protected $violations = array();
/**
2012-01-31 06:45:20 +00:00
* Creates a new constraint violation list.
*
* @param array $violations The constraint violations to add to the list
*/
public function __construct(array $violations = array())
{
foreach ($violations as $violation) {
$this->add($violation);
}
}
/**
* @return string
*/
public function __toString()
{
$string = '';
foreach ($this->violations as $violation) {
$string .= $violation . "\n";
}
return $string;
}
/**
* Add a ConstraintViolation to this list.
*
* @param ConstraintViolation $violation
2011-07-20 09:37:57 +01:00
*
* @api
*/
public function add(ConstraintViolation $violation)
{
$this->violations[] = $violation;
}
/**
* Merge an existing ConstraintViolationList into this list.
*
* @param ConstraintViolationList $otherList
2011-07-20 09:37:57 +01:00
*
* @api
*/
public function addAll(ConstraintViolationList $otherList)
{
foreach ($otherList->violations as $violation) {
$this->violations[] = $violation;
}
}
/**
* @see IteratorAggregate
2011-07-20 09:37:57 +01:00
*
* @api
*/
public function getIterator()
{
return new \ArrayIterator($this->violations);
}
/**
* @see Countable
2011-07-20 09:37:57 +01:00
*
* @api
*/
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)
{
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
}