Skip to content

Commit cd27e1f

Browse files
committed
[Form] Extracted ReferencingArrayIterator out of VirtualFormAwareIterator
1 parent ccaaedf commit cd27e1f

File tree

2 files changed

+81
-58
lines changed

2 files changed

+81
-58
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Form\Util;
13+
14+
/**
15+
* Iterator that traverses an array.
16+
*
17+
* Contrary to {@link \ArrayIterator}, this iterator recognizes changes in the
18+
* original array during iteration.
19+
*
20+
* @author Bernhard Schussek <bschussek@gmail.com>
21+
*/
22+
class ReferencingArrayIterator implements \Iterator
23+
{
24+
/**
25+
* @var array
26+
*/
27+
private $array;
28+
29+
/**
30+
* Creates a new iterator.
31+
*
32+
* @param array $array An array
33+
*/
34+
public function __construct(array &$array)
35+
{
36+
$this->array = &$array;
37+
}
38+
39+
/**
40+
*{@inheritdoc}
41+
*/
42+
public function current()
43+
{
44+
return current($this->array);
45+
}
46+
47+
/**
48+
*{@inheritdoc}
49+
*/
50+
public function next()
51+
{
52+
next($this->array);
53+
}
54+
55+
/**
56+
*{@inheritdoc}
57+
*/
58+
public function key()
59+
{
60+
return key($this->array);
61+
}
62+
63+
/**
64+
*{@inheritdoc}
65+
*/
66+
public function valid()
67+
{
68+
return null !== key($this->array);
69+
}
70+
71+
/**
72+
*{@inheritdoc}
73+
*/
74+
public function rewind()
75+
{
76+
reset($this->array);
77+
}
78+
}

src/Symfony/Component/Form/Util/VirtualFormAwareIterator.php

Lines changed: 3 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -14,71 +14,16 @@
1414
/**
1515
* Iterator that traverses an array of forms.
1616
*
17-
* Contrary to \ArrayIterator, this iterator recognizes changes in the original
18-
* array during iteration.
17+
* Contrary to {@link \ArrayIterator}, this iterator recognizes changes in the
18+
* original array during iteration.
1919
*
2020
* You can wrap the iterator into a {@link \RecursiveIterator} in order to
2121
* enter any virtual child form and iterate the children of that virtual form.
2222
*
2323
* @author Bernhard Schussek <bschussek@gmail.com>
2424
*/
25-
class VirtualFormAwareIterator implements \RecursiveIterator
25+
class VirtualFormAwareIterator extends ReferencingArrayIterator implements \RecursiveIterator
2626
{
27-
/**
28-
* @var \Symfony\Component\Form\FormInterface[]
29-
*/
30-
private $forms;
31-
32-
/**
33-
* Creates a new iterator.
34-
*
35-
* @param \Symfony\Component\Form\FormInterface[] $forms An array of forms
36-
*/
37-
public function __construct(array &$forms)
38-
{
39-
$this->forms = &$forms;
40-
}
41-
42-
/**
43-
*{@inheritdoc}
44-
*/
45-
public function current()
46-
{
47-
return current($this->forms);
48-
}
49-
50-
/**
51-
*{@inheritdoc}
52-
*/
53-
public function next()
54-
{
55-
next($this->forms);
56-
}
57-
58-
/**
59-
*{@inheritdoc}
60-
*/
61-
public function key()
62-
{
63-
return key($this->forms);
64-
}
65-
66-
/**
67-
*{@inheritdoc}
68-
*/
69-
public function valid()
70-
{
71-
return null !== key($this->forms);
72-
}
73-
74-
/**
75-
*{@inheritdoc}
76-
*/
77-
public function rewind()
78-
{
79-
reset($this->forms);
80-
}
81-
8227
/**
8328
*{@inheritdoc}
8429
*/

0 commit comments

Comments
 (0)