Skip to content

Commit

Permalink
add AttributePath::fromString()
Browse files Browse the repository at this point in the history
  • Loading branch information
tmilos committed Jan 19, 2017
1 parent cfd9ba1 commit ce49583
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
36 changes: 36 additions & 0 deletions src/Ast/AttributePath.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,44 @@ class AttributePath extends Node
/** @var string[] */
public $attributeNames = [];

/**
* @param string $string
*
* @return AttributePath
*/
public static function fromString($string)
{
$string = trim($string);
if (!$string) {
throw new \InvalidArgumentException('Empty attribute path');
}

$colonPos = strrpos($string, ':');
if ($colonPos !== false) {
$schema = substr($string, 0, $colonPos);
$path = substr($string, $colonPos + 1);
} else {
$schema = null;
$path = $string;
}

$parts = explode('.', $path);
$attributePath = new static();
$attributePath->schema = $schema;
foreach ($parts as $part) {
$attributePath->add($part);
}

return $attributePath;
}

public function add($attributeName)
{
$firstLetter = strtolower(substr($attributeName, 0, 1));
if ($firstLetter < 'a' || $firstLetter > 'z') {
throw new \InvalidArgumentException(sprintf('Invalid attribute name "%s"', $attributeName));
}

$this->attributeNames[] = $attributeName;
}

Expand Down
18 changes: 1 addition & 17 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,23 +265,7 @@ private function attributePath()
$this->syntaxError('attribute path');
}

$colonPos = strrpos($string, ':');
if ($colonPos !== false) {
$schema = substr($string, 0, $colonPos);
$path = substr($string, $colonPos + 1);
} else {
$schema = null;
$path = $string;
}

$parts = explode('.', $path);
$attributePath = new Ast\AttributePath();
$attributePath->schema = $schema;
foreach ($parts as $part) {
$attributePath->add($part);
}

return $attributePath;
return Ast\AttributePath::fromString($string);
}

/**
Expand Down

0 comments on commit ce49583

Please sign in to comment.