[Form] Extracted ReferencingArrayIterator out of VirtualFormAwareIterator

This commit is contained in:
Bernhard Schussek 2013-08-23 13:16:42 +02:00
parent ccaaedfcbb
commit cd27e1facb
2 changed files with 81 additions and 58 deletions

View File

@ -0,0 +1,78 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Form\Util;
/**
* Iterator that traverses an array.
*
* Contrary to {@link \ArrayIterator}, this iterator recognizes changes in the
* original array during iteration.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class ReferencingArrayIterator implements \Iterator
{
/**
* @var array
*/
private $array;
/**
* Creates a new iterator.
*
* @param array $array An array
*/
public function __construct(array &$array)
{
$this->array = &$array;
}
/**
*{@inheritdoc}
*/
public function current()
{
return current($this->array);
}
/**
*{@inheritdoc}
*/
public function next()
{
next($this->array);
}
/**
*{@inheritdoc}
*/
public function key()
{
return key($this->array);
}
/**
*{@inheritdoc}
*/
public function valid()
{
return null !== key($this->array);
}
/**
*{@inheritdoc}
*/
public function rewind()
{
reset($this->array);
}
}

View File

@ -14,71 +14,16 @@ namespace Symfony\Component\Form\Util;
/**
* Iterator that traverses an array of forms.
*
* Contrary to \ArrayIterator, this iterator recognizes changes in the original
* array during iteration.
* Contrary to {@link \ArrayIterator}, this iterator recognizes changes in the
* original array during iteration.
*
* You can wrap the iterator into a {@link \RecursiveIterator} in order to
* enter any virtual child form and iterate the children of that virtual form.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class VirtualFormAwareIterator implements \RecursiveIterator
class VirtualFormAwareIterator extends ReferencingArrayIterator implements \RecursiveIterator
{
/**
* @var \Symfony\Component\Form\FormInterface[]
*/
private $forms;
/**
* Creates a new iterator.
*
* @param \Symfony\Component\Form\FormInterface[] $forms An array of forms
*/
public function __construct(array &$forms)
{
$this->forms = &$forms;
}
/**
*{@inheritdoc}
*/
public function current()
{
return current($this->forms);
}
/**
*{@inheritdoc}
*/
public function next()
{
next($this->forms);
}
/**
*{@inheritdoc}
*/
public function key()
{
return key($this->forms);
}
/**
*{@inheritdoc}
*/
public function valid()
{
return null !== key($this->forms);
}
/**
*{@inheritdoc}
*/
public function rewind()
{
reset($this->forms);
}
/**
*{@inheritdoc}
*/