8000 merged branch Tobion/proppath (PR #5313) · meistro2k/symfony@1121ef0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1121ef0

Browse files
committed
merged branch Tobion/proppath (PR symfony#5313)
Commits ------- 79a1257 [Form] removed getPositions from PropertyPathInterface Discussion ---------- [Form] removed getPositions from PropertyPathInterface This method was just an implementation detail (that is not even needed as my implementation shows) and should not be part of the public API as it serves no purpose. --------------------------------------------------------------------------- by fabpot at 2012-08-22T06:19:35Z ping @bschussek --------------------------------------------------------------------------- by stof at 2012-08-22T09:11:51Z what is the performance impact of your implementation compared to the previous one ? the form binding is executing this code thousands times for big forms. --------------------------------------------------------------------------- by Tobion at 2012-08-22T14:08:39Z There is none of course. --------------------------------------------------------------------------- by bschussek at 2012-08-22T15:23:57Z Looks good to me.
2 parents 7a233bc + 79a1257 commit 1121ef0

File tree

3 files changed

+21
-63
lines changed

3 files changed

+21
-63
lines changed

src/Symfony/Component/Form/Extension/Validator/ViolationMapper/ViolationPath.php

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@ class ViolationPath implements \IteratorAggregate, PropertyPathInterface
2424
*/
2525
private $elements = array();
2626

27-
/**
28-
* @var array
29-
*/
30-
private $positions = array();
31-
3227
/**
3328
* @var array
3429
*/
@@ -59,7 +54,6 @@ public function __construct($violationPath)
5954
{
6055
$path = new PropertyPath($violationPath);
6156
$elements = $path->getElements();
62-
$positions = $path->getPositions();
6357
$data = false;
6458

6559
for ($i = 0, $l = count($elements); $i < $l; ++$i) {
@@ -76,7 +70,6 @@ public function __construct($violationPath)
7670
}
7771

7872
$this->elements[] = $elements[$i];
79-
$this->positions[] = $positions[$i];
8073
$this->isIndex[] = true;
8174
$this->mapsForm[] = true;
8275
} elseif ('data' === $elements[$i] && $path->isProperty($i)) {
@@ -89,7 +82,6 @@ public function __construct($violationPath)
8982
}
9083

9184
$this->elements[] = $elements[$i];
92-
$this->positions[] = $positions[$i];
9385
$this->isIndex[] = $path->isIndex($i);
9486
$this->mapsForm[] = false;
9587
$data = true;
@@ -102,16 +94,14 @@ public function __construct($violationPath)
10294
// Already after the "data" element
10395
// Pick everything as is
10496
$this->elements[] = $elements[$i];
105-
$this->positions[] = $positions[$i];
10697
$this->isIndex[] = $path->isIndex($i);
10798
$this->mapsForm[] = false;
10899
}
109100
}
110101

111102
$this->length = count($this->elements);
112-
$this->pathAsString = $violationPath;
113103

114-
$this->resizeString();
104+
$this->buildString();
115105
}
116106

117107
/**
@@ -122,14 +112,6 @@ public function __toString()
122112
return $this->pathAsString;
123113
}
124114

125-
/**
126-
* {@inheritdoc}
127-
*/
128-
public function getPositions()
129-
{
130-
return $this->positions;
131-
}
132-
133115
/**
134116
* {@inheritdoc}
135117
*/
@@ -153,9 +135,8 @@ public function getParent()
153135
array_pop($parent->elements);
154136
array_pop($parent->isIndex);
155137
array_pop($parent->mapsForm);
156-
array_pop($parent->positions);
157138

158-
$parent->resizeString();
139+
$parent->buildString();
159140

160141
return $parent;
161142
}
@@ -242,24 +223,27 @@ public function getIterator()
242223
}
243224

244225
/**
245-
* Resizes the string representation to match the number of elements.
226+
* Builds the string representation from the elements.
246227
*/
247-
private function resizeString()
228+
private function buildString()
248229
{
249-
$lastIndex = $this->length - 1;
250-
251-
if ($lastIndex < 0) {
252-
$this->pathAsString = '';
253-
} else {
254-
// +1 for the dot/opening bracket
255-
$length = $this->positions[$lastIndex] + strlen($this->elements[$lastIndex]) + 1;
230+
$this->pathAsString = '';
231+
$data = false;
256232

257-
if ($this->isIndex[$lastIndex]) {
258-
// +1 for the closing bracket
259-
++$length;
233+
foreach ($this->elements as $index => $element) {
234+
if ($this->mapsForm[$index]) {
235+
$this->pathAsString .= ".children[$element]";
236+
} elseif (!$data) {
237+
$this->pathAsString .= '.data' . ($this->isIndex[$index] ? "[$element]" : ".$element");
238+
$data = true;
239+
} else {
240+
$this->pathAsString .= $this->isIndex[$index] ? "[$element]" : ".$element";
260241
}
242+
}
261243

262-
$this->pathAsString = substr($this->pathAsString, 0, $length);
244+
if ('' !== $this->pathAsString) {
245+
// remove leading dot
246+
$this->pathAsString = substr($this->pathAsString, 1);
263247
}
264248
}
265249
}

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

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,6 @@ class PropertyPath implements \IteratorAggregate, PropertyPathInterface
6565
*/
6666
private $pathAsString;
6767

68-
/**
69-
* Positions where the individual elements start in the string representation
70-
* @var array
71-
*/
72-
private $positions;
73-
7468
/**
7569
* Constructs a property path from a string.
7670
*
@@ -89,7 +83,6 @@ public function __construct($propertyPath)
8983
$this->length = $propertyPath->length;
9084
$this->isIndex = $propertyPath->isIndex;
9185
$this->pathAsString = $propertyPath->pathAsString;
92-
$this->positions = $propertyPath->positions;
9386

9487
return;
9588
}
@@ -109,8 +102,6 @@ public function __construct($propertyPath)
109102
$pattern = '/^(([^\.\[]+)|\[([^\]]+)\])(.*)/';
110103

111104
while (preg_match($pattern, $remaining, $matches)) {
112-
$this->positions[] = $position;
113-
114105
if ('' !== $matches[2]) {
115106
$element = $matches[2];
116107
$this->isIndex[] = false;
@@ -136,7 +127,7 @@ public function __construct($propertyPath)
136127
$pattern = '/^(\.(\w+)|\[([^\]]+)\])(.*)/';
137128
}
138129

139-
if (!empty($remaining)) {
130+
if ('' !== $remaining) {
140131
throw new InvalidPropertyPathException(sprintf(
141132
'Could not parse property path "%s". Unexpected token "%s" at position %d',
142133
$propertyPath,
@@ -156,14 +147,6 @@ public function __toString()
156147
return $this->pathAsString;
157148
}
158149

159-
/**
160-
* {@inheritdoc}
161-
*/
162-
public function getPositions()
163-
{
164-
return $this->positions;
165-
}
166-
167150
/**
168151
* {@inheritdoc}
169152
*/
@@ -184,11 +167,10 @@ public function getParent()
184167
$parent = clone $this;
185168

186169
--$parent->length;
187-
$parent->pathAsString = substr($parent->pathAsString, 0, $parent->positions[$parent->length]);
170+
$parent->pathAsString = substr($parent->pathAsString, 0, max(strrpos($parent->pathAsString, '.'), strrpos($parent->pathAsString, '[')));
188171
array_pop($parent->elements);
189172
array_pop($parent->singulars);
190173
array_pop($parent->isIndex);
191-
array_pop($parent->positions);
192174

193175
return $parent;
194176
}

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,7 @@ interface PropertyPathInterface extends \Traversable
2424
public function __toString();
2525

2626
/**
27-
* Returns the positions at which the elements of the path
28-
* start in the string.
29-
*
30-
* @return array The string offsets of the elements.
31-
*/
32-
public function getPositions();
33-
34-
/**
35-
* Returns the length of the property path.
27+
* Returns the length of the property path, i.e. the number of elements.
3628
*
3729
* @return integer The path length.
3830
*/

0 commit comments

Comments
 (0)
0