8000 [Form] Made $name parameters optional in PropertyPathBuilder:replaceB… · symfony/symfony@0c09a0e · GitHub
[go: up one dir, main page]

Skip to content

Commit 0c09a0e

Browse files
committed
[Form] Made $name parameters optional in PropertyPathBuilder:replaceBy(Index|Property)
1 parent 081c643 commit 0c09a0e

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

src/Symfony/Component/Form/Tests/Util/PropertyPathBuilderTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ public function testReplaceByIndex()
100100
$this->assertEquals($path, $this->builder->getPropertyPath());
101101
}
102102

103+
public function testReplaceByIndexWithoutName()
104+
{
105+
$this->builder->replaceByIndex(0);
106+
107+
$path = new PropertyPath('[old1][old2].old3[old4][old5].old6');
108+
109+
$this->assertEquals($path, $this->builder->getPropertyPath());
110+
}
111+
103112
/**
104113
* @expectedException \OutOfBoundsException
105114
*/
@@ -125,6 +134,15 @@ public function testReplaceByProperty()
125134
$this->assertEquals($path, $this->builder->getPropertyPath());
126135
}
127136

137+
public function testReplaceByPropertyWithoutName()
138+
{
139+
$this->builder->replaceByProperty(1);
140+
141+
$path = new PropertyPath('old1.old2.old3[old4][old5].old6');
142+
143+
$this->assertEquals($path, $this->builder->getPropertyPath());
144+
}
145+
128146
/**
129147
* @expectedException \OutOfBoundsException
130148
*/

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,38 +131,44 @@ public function replace($offset, $length, PropertyPathInterface $path, $pathOffs
131131
}
132132

133133
/**
134-
* Replaces a sub-path by a single index element.
134+
* Replaces a property element by an index element.
135135
*
136136
* @param integer $offset The offset at which to replace.
137-
* @param string $name The inserted index name.
137+
* @param string $name The new name of the element. Optional.
138138
*
139139
* @throws \OutOfBoundsException If the offset is invalid.
140140
*/
141-
public function replaceByIndex($offset, $name)
141+
public function replaceByIndex($offset, $name = null)
142142
{
143143
if (!isset($this->elements[$offset])) {
144144
throw new \OutOfBoundsException('The offset ' . $offset . ' is not within the property path');
145145
}
146146

147-
$this->elements[$offset] = $name;
147+
if (null !== $name) {
148+
$this->elements[$offset] = $name;
149+
}
150+
148151
$this->isIndex[$offset] = true;
149152
}
150153

151154
/**
152-
* Replaces a sub-path by a single property element.
155+
* Replaces an index element by a property element.
153156
*
154157
* @param integer $offset The offset at which to replace.
155-
* @param string $name The inserted property name.
158+
* @param string $name The new name of the element. Optional.
156159
*
157160
* @throws \OutOfBoundsException If the offset is invalid.
158161
*/
159-
public function replaceByProperty($offset, $name)
162+
public function replaceByProperty($offset, $name = null)
160163
{
161164
if (!isset($this->elements[$offset])) {
162165
throw new \OutOfBoundsException('The offset ' . $offset . ' is not within the property path');
163166
}
164167

165-
$this->elements[$offset] = $name;
168+
if (null !== $name) {
169+
$this->elements[$offset] = $name;
170+
}
171+
166172
$this->isIndex[$offset] = false;
167173
}
168174

0 commit comments

Comments
 (0)
0