public function __construct($propertyPath)
{
if ($propertyPath === '' || $propertyPath === null) {
throw new InvalidPropertyPathException('The property path must not be empty');
}
$this->string = $propertyPath;
$position = 0;
$remaining = $propertyPath;
// first element is evaluated differently - no leading dot for properties
$pattern = '/^((\w+)|\[(\w+)\])(.*)/';
while (preg_match($pattern, $remaining, $matches)) {
if ($matches[2] !== '') {
$this->elements[] = $matches[2];
$this->isIndex[] = false;
} else {
$this->elements[] = $matches[3];
$this->isIndex[] = true;
}
$position += strlen($matches[1]);
$remaining = $matches[4];
$pattern = '/^(\.(\w+)|\[(\w+)\])(.*)/';
}
if (!empty($remaining)) {
throw new InvalidPropertyPathException(sprintf(
'Could not parse property path "%s". Unexpected token "%s" at position %d',
$propertyPath,
$remaining{0},
$position
));
}
$this->length = count($this->elements);
}