2011-04-18 19:02:30 +02:00
|
|
|
<?php
|
|
|
|
|
2011-05-27 18:20:44 +02:00
|
|
|
/**
|
|
|
|
* @property array $parts Parts of the name
|
2011-08-04 13:59:56 +02:00
|
|
|
* @property int $type Resolve type (self::NORMAL, self::FULLY_QUALIFIED or self::RELATIVE)
|
2011-05-27 18:20:44 +02:00
|
|
|
*/
|
2011-06-05 18:40:04 +02:00
|
|
|
class PHPParser_Node_Name extends PHPParser_NodeAbstract
|
2011-04-18 19:02:30 +02:00
|
|
|
{
|
2011-08-04 13:23:35 +02:00
|
|
|
const NORMAL = 0;
|
|
|
|
const FULLY_QUALIFIED = 1;
|
|
|
|
const RELATIVE = 2;
|
2011-04-18 19:02:30 +02:00
|
|
|
|
2011-08-04 13:23:35 +02:00
|
|
|
/**
|
|
|
|
* Gets the first part of the name, i.e. everything before the first namespace separator.
|
|
|
|
*
|
|
|
|
* @return string First part of the name
|
|
|
|
*/
|
|
|
|
public function getFirst() {
|
|
|
|
return $this->parts[0];
|
|
|
|
}
|
|
|
|
|
2011-08-04 12:02:14 +02:00
|
|
|
/**
|
|
|
|
* Gets the last part of the name, i.e. everything after the last namespace separator.
|
|
|
|
*
|
|
|
|
* @return string Last part of the name
|
|
|
|
*/
|
|
|
|
public function getLast() {
|
|
|
|
return $this->parts[count($this->parts) - 1];
|
|
|
|
}
|
|
|
|
|
2011-08-04 13:23:35 +02:00
|
|
|
/**
|
|
|
|
* Checks whether the name is unqualified. (E.g. Name)
|
|
|
|
*
|
|
|
|
* @return bool Whether the name is unqualified
|
|
|
|
*/
|
|
|
|
public function isUnqualified() {
|
2011-08-04 13:59:56 +02:00
|
|
|
return self::NORMAL == $this->type && 1 == count($this->parts);
|
2011-08-04 13:23:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether the name is qualified. (E.g. Name\Name)
|
|
|
|
*
|
|
|
|
* @return bool Whether the name is qualified
|
|
|
|
*/
|
|
|
|
public function isQualified() {
|
2011-08-04 13:59:56 +02:00
|
|
|
return self::NORMAL == $this->type && 1 < count($this->parts);
|
2011-08-04 13:23:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether the name is fully qualified. (E.g. \Name)
|
|
|
|
*
|
|
|
|
* @return bool Whether the name is fully qualified
|
|
|
|
*/
|
|
|
|
public function isFullyQualified() {
|
2011-08-04 13:59:56 +02:00
|
|
|
return self::FULLY_QUALIFIED == $this->type;
|
2011-08-04 13:23:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether the name is explicitly relative to the current namespace. (E.g. namespace\Name)
|
|
|
|
*
|
|
|
|
* @return bool Whether the name is fully qualified
|
|
|
|
*/
|
|
|
|
public function isRelative() {
|
2011-08-04 13:59:56 +02:00
|
|
|
return self::RELATIVE == $this->type;
|
2011-08-04 13:23:35 +02:00
|
|
|
}
|
|
|
|
|
2011-08-04 12:02:14 +02:00
|
|
|
/**
|
|
|
|
* Returns a string representation of the name by imploding the namespace parts with a separator.
|
|
|
|
*
|
|
|
|
* @param string $separator The separator to use (defaults to the namespace separator \)
|
|
|
|
*
|
|
|
|
* @return string String representation
|
|
|
|
*/
|
|
|
|
public function toString($separator = '\\') {
|
|
|
|
return implode($separator, $this->parts);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a string representation of the name by imploding the namespace parts with the
|
2011-08-04 16:30:04 +02:00
|
|
|
* namespace separator.
|
2011-08-04 12:02:14 +02:00
|
|
|
*
|
|
|
|
* @return string String representation
|
|
|
|
*/
|
|
|
|
public function __toString() {
|
|
|
|
return $this->toString('\\');
|
|
|
|
}
|
2011-08-04 16:30:04 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the whole name.
|
|
|
|
*
|
|
|
|
* @param string|array|self $name The name to set the whole name to
|
|
|
|
*/
|
|
|
|
public function set($name) {
|
|
|
|
$this->parts = $this->prepareName($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepends a name to this name.
|
|
|
|
*
|
|
|
|
* @param string|array|self $name Name to prepend
|
|
|
|
*/
|
|
|
|
public function prepend($name) {
|
|
|
|
$this->parts = array_merge($this->prepareName($name), $this->parts);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Appends a name to this name.
|
|
|
|
*
|
|
|
|
* @param string|array|self $name Name to append
|
|
|
|
*/
|
|
|
|
public function append($name) {
|
|
|
|
$this->parts = array_merge($this->prepareName($name), $name->parts);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the first part of the name.
|
|
|
|
*
|
|
|
|
* @param string|array|self $name The name to set the first part to
|
|
|
|
*/
|
|
|
|
public function setFirst($name) {
|
|
|
|
$this->parts = array_merge($this->prepareName($name), array_slice($this->parts, 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the last part of the name.
|
|
|
|
*
|
|
|
|
* @param string|array|self $name The name to set the last part to
|
|
|
|
*/
|
|
|
|
public function setLast($name) {
|
|
|
|
$this->parts = array_merge($this->prepareName($name), array_slice($this->parts, 0, -1));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepares a (string, array or Name node) name for use in name changing methods by converting
|
|
|
|
* it to an array.
|
|
|
|
*
|
|
|
|
* @param string|array|self $name Name to prepare
|
|
|
|
*
|
|
|
|
* @return array Prepared name
|
|
|
|
*/
|
|
|
|
protected function prepareName($name) {
|
|
|
|
if (is_string($name)) {
|
|
|
|
return explode('\\', $name);
|
|
|
|
} elseif ($name instanceof self) {
|
|
|
|
return $name->parts;
|
|
|
|
} elseif (!is_array($name)) {
|
|
|
|
throw new InvalidArgumentException(
|
|
|
|
'When changing a name you need to pass either a string and array or a Name node'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2011-04-18 19:02:30 +02:00
|
|
|
}
|