Skip to content

Commit

Permalink
[Form] removed getPositions from PropertyPathInterface
Browse files Browse the repository at this point in the history
this method was just an implementation detail (that is not even needed) and should not be part of the public API as it serves no purpose
  • Loading branch information
Tobion committed Aug 22, 2012
1 parent 74583bb commit dd50752
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 63 deletions.
52 changes: 18 additions & 34 deletions Extension/Validator/ViolationMapper/ViolationPath.php
Expand Up @@ -24,11 +24,6 @@ class ViolationPath implements \IteratorAggregate, PropertyPathInterface
*/
private $elements = array();

/**
* @var array
*/
private $positions = array();

/**
* @var array
*/
Expand Down Expand Up @@ -59,7 +54,6 @@ public function __construct($violationPath)
{
$path = new PropertyPath($violationPath);
$elements = $path->getElements();
$positions = $path->getPositions();
$data = false;

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

$this->elements[] = $elements[$i];
$this->positions[] = $positions[$i];
$this->isIndex[] = true;
$this->mapsForm[] = true;
} elseif ('data' === $elements[$i] && $path->isProperty($i)) {
Expand All @@ -89,7 +82,6 @@ public function __construct($violationPath)
}

$this->elements[] = $elements[$i];
$this->positions[] = $positions[$i];
$this->isIndex[] = $path->isIndex($i);
$this->mapsForm[] = false;
$data = true;
Expand All @@ -102,16 +94,14 @@ public function __construct($violationPath)
// Already after the "data" element
// Pick everything as is
$this->elements[] = $elements[$i];
$this->positions[] = $positions[$i];
$this->isIndex[] = $path->isIndex($i);
$this->mapsForm[] = false;
}
}

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

$this->resizeString();
$this->buildString();
}

/**
Expand All @@ -122,14 +112,6 @@ public function __toString()
return $this->pathAsString;
}

/**
* {@inheritdoc}
*/
public function getPositions()
{
return $this->positions;
}

/**
* {@inheritdoc}
*/
Expand All @@ -153,9 +135,8 @@ public function getParent()
array_pop($parent->elements);
array_pop($parent->isIndex);
array_pop($parent->mapsForm);
array_pop($parent->positions);

$parent->resizeString();
$parent->buildString();

return $parent;
}
Expand Down Expand Up @@ -242,24 +223,27 @@ public function getIterator()
}

/**
* Resizes the string representation to match the number of elements.
* Builds the string representation from the elements.
*/
private function resizeString()
private function buildString()
{
$lastIndex = $this->length - 1;

if ($lastIndex < 0) {
$this->pathAsString = '';
} else {
// +1 for the dot/opening bracket
$length = $this->positions[$lastIndex] + strlen($this->elements[$lastIndex]) + 1;
$this->pathAsString = '';
$data = false;

if ($this->isIndex[$lastIndex]) {
// +1 for the closing bracket
++$length;
foreach ($this->elements as $index => $element) {
if ($this->mapsForm[$index]) {
$this->pathAsString .= ".children[$element]";
} elseif (!$data) {
$this->pathAsString .= '.data' . ($this->isIndex[$index] ? "[$element]" : ".$element");
$data = true;
} else {
$this->pathAsString .= $this->isIndex[$index] ? "[$element]" : ".$element";
}
}

$this->pathAsString = substr($this->pathAsString, 0, $length);
if ('' !== $this->pathAsString) {
// remove leading dot
$this->pathAsString = substr($this->pathAsString, 1);
}
}
}
22 changes: 2 additions & 20 deletions Util/PropertyPath.php
Expand Up @@ -65,12 +65,6 @@ class PropertyPath implements \IteratorAggregate, PropertyPathInterface
*/
private $pathAsString;

/**
* Positions where the individual elements start in the string representation
* @var array
*/
private $positions;

/**
* Constructs a property path from a string.
*
Expand All @@ -89,7 +83,6 @@ public function __construct($propertyPath)
$this->length = $propertyPath->length;
$this->isIndex = $propertyPath->isIndex;
$this->pathAsString = $propertyPath->pathAsString;
$this->positions = $propertyPath->positions;

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

while (preg_match($pattern, $remaining, $matches)) {
$this->positions[] = $position;

if ('' !== $matches[2]) {
$element = $matches[2];
$this->isIndex[] = false;
Expand All @@ -136,7 +127,7 @@ public function __construct($propertyPath)
$pattern = '/^(\.(\w+)|\[([^\]]+)\])(.*)/';
}

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

/**
* {@inheritdoc}
*/
public function getPositions()
{
return $this->positions;
}

/**
* {@inheritdoc}
*/
Expand All @@ -184,11 +167,10 @@ public function getParent()
$parent = clone $this;

--$parent->length;
$parent->pathAsString = substr($parent->pathAsString, 0, $parent->positions[$parent->length]);
$parent->pathAsString = substr($parent->pathAsString, 0, max(strrpos($parent->pathAsString, '.'), strrpos($parent->pathAsString, '[')));
array_pop($parent->elements);
array_pop($parent->singulars);
array_pop($parent->isIndex);
array_pop($parent->positions);

return $parent;
}
Expand Down
10 changes: 1 addition & 9 deletions Util/PropertyPathInterface.php
Expand Up @@ -24,15 +24,7 @@ interface PropertyPathInterface extends \Traversable
public function __toString();

/**
* Returns the positions at which the elements of the path
* start in the string.
*
* @return array The string offsets of the elements.
*/
public function getPositions();

/**
* Returns the length of the property path.
* Returns the length of the property path, i.e. the number of elements.
*
* @return integer The path length.
*/
Expand Down

0 comments on commit dd50752

Please sign in to comment.