2011-04-18 19:02:30 +02:00
|
|
|
<?php
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser\Node;
|
|
|
|
|
|
|
|
use PhpParser\NodeAbstract;
|
|
|
|
|
|
|
|
class Name extends NodeAbstract
|
2011-04-18 19:02:30 +02:00
|
|
|
{
|
2015-02-28 18:44:28 +01:00
|
|
|
/** @var string[] Parts of the name */
|
|
|
|
public $parts;
|
|
|
|
|
2011-08-09 14:55:45 +02:00
|
|
|
/**
|
|
|
|
* Constructs a name node.
|
|
|
|
*
|
2011-08-09 15:23:18 +02:00
|
|
|
* @param string|array $parts Parts of the name (or name as string)
|
2012-04-29 23:32:09 +02:00
|
|
|
* @param array $attributes Additional attributes
|
2011-08-09 14:55:45 +02:00
|
|
|
*/
|
2012-04-29 23:32:09 +02:00
|
|
|
public function __construct($parts, array $attributes = array()) {
|
2011-08-09 15:23:18 +02:00
|
|
|
if (!is_array($parts)) {
|
|
|
|
$parts = explode('\\', $parts);
|
|
|
|
}
|
|
|
|
|
2015-05-02 22:17:34 +02:00
|
|
|
parent::__construct($attributes);
|
2015-02-28 18:44:28 +01:00
|
|
|
$this->parts = $parts;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSubNodeNames() {
|
|
|
|
return array('parts');
|
2011-08-09 14:55:45 +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-09-24 18:05:14 +02:00
|
|
|
/**
|
|
|
|
* Checks whether the name is unqualified. (E.g. Name)
|
|
|
|
*
|
|
|
|
* @return bool Whether the name is unqualified
|
|
|
|
*/
|
|
|
|
public function isUnqualified() {
|
|
|
|
return 1 == count($this->parts);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether the name is qualified. (E.g. Name\Name)
|
|
|
|
*
|
|
|
|
* @return bool Whether the name is qualified
|
|
|
|
*/
|
|
|
|
public function isQualified() {
|
|
|
|
return 1 < count($this->parts);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether the name is fully qualified. (E.g. \Name)
|
|
|
|
*
|
|
|
|
* @return bool Whether the name is fully qualified
|
|
|
|
*/
|
|
|
|
public function isFullyQualified() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether the name is explicitly relative to the current namespace. (E.g. namespace\Name)
|
|
|
|
*
|
|
|
|
* @return bool Whether the name is relative
|
|
|
|
*/
|
|
|
|
public function isRelative() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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() {
|
2011-09-24 16:53:40 +02:00
|
|
|
return implode('\\', $this->parts);
|
2011-08-04 12:02:14 +02:00
|
|
|
}
|
2011-08-04 16:30:04 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the whole name.
|
|
|
|
*
|
2015-07-12 23:55:57 +02:00
|
|
|
* @deprecated Create a new Name instead, or manually modify the $parts property
|
|
|
|
*
|
2011-08-04 16:30:04 +02:00
|
|
|
* @param string|array|self $name The name to set the whole name to
|
|
|
|
*/
|
|
|
|
public function set($name) {
|
2015-06-12 23:05:28 +02:00
|
|
|
$this->parts = self::prepareName($name);
|
2011-08-04 16:30:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepends a name to this name.
|
|
|
|
*
|
2015-07-12 23:55:57 +02:00
|
|
|
* @deprecated Use Name::concat($name1, $name2) instead
|
|
|
|
*
|
2011-08-04 16:30:04 +02:00
|
|
|
* @param string|array|self $name Name to prepend
|
|
|
|
*/
|
|
|
|
public function prepend($name) {
|
2015-06-12 23:05:28 +02:00
|
|
|
$this->parts = array_merge(self::prepareName($name), $this->parts);
|
2011-08-04 16:30:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Appends a name to this name.
|
|
|
|
*
|
2015-07-12 23:55:57 +02:00
|
|
|
* @deprecated Use Name::concat($name1, $name2) instead
|
|
|
|
*
|
2011-08-04 16:30:04 +02:00
|
|
|
* @param string|array|self $name Name to append
|
|
|
|
*/
|
|
|
|
public function append($name) {
|
2015-06-12 23:05:28 +02:00
|
|
|
$this->parts = array_merge($this->parts, self::prepareName($name));
|
2011-08-04 16:30:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the first part of the name.
|
|
|
|
*
|
2015-07-12 23:55:57 +02:00
|
|
|
* @deprecated Use concat($first, $name->slice(1)) instead
|
|
|
|
*
|
2011-08-04 16:30:04 +02:00
|
|
|
* @param string|array|self $name The name to set the first part to
|
|
|
|
*/
|
|
|
|
public function setFirst($name) {
|
2015-06-12 23:05:28 +02:00
|
|
|
array_splice($this->parts, 0, 1, self::prepareName($name));
|
2011-08-04 16:30:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the last part of the name.
|
|
|
|
*
|
|
|
|
* @param string|array|self $name The name to set the last part to
|
|
|
|
*/
|
|
|
|
public function setLast($name) {
|
2015-06-12 23:05:28 +02:00
|
|
|
array_splice($this->parts, -1, 1, self::prepareName($name));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-07-12 23:55:57 +02:00
|
|
|
* Gets a slice of a name (similar to array_slice).
|
|
|
|
*
|
|
|
|
* This method returns a new instance of the same type as the original and with the same
|
|
|
|
* attributes.
|
2015-06-12 23:05:28 +02:00
|
|
|
*
|
2015-07-12 23:55:57 +02:00
|
|
|
* If the slice is empty, a Name with an empty parts array is returned. While this is
|
|
|
|
* meaningless in itself, it works correctly in conjunction with concat().
|
|
|
|
*
|
|
|
|
* @param int $offset Offset to start the slice at
|
|
|
|
*
|
|
|
|
* @return static Sliced name
|
2015-06-12 23:05:28 +02:00
|
|
|
*/
|
2015-07-12 23:55:57 +02:00
|
|
|
public function slice($offset) {
|
|
|
|
// TODO negative offset and length
|
|
|
|
if ($offset < 0 || $offset > count($this->parts)) {
|
|
|
|
throw new \OutOfBoundsException(sprintf('Offset %d is out of bounds', $offset));
|
|
|
|
}
|
|
|
|
|
|
|
|
return new static(array_slice($this->parts, $offset), $this->attributes);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Concatenate two names, yielding a new Name instance.
|
|
|
|
*
|
|
|
|
* The type of the generated instance depends on which class this method is called on, for
|
|
|
|
* example Name\FullyQualified::concat() will yield a Name\FullyQualified instance.
|
|
|
|
*
|
|
|
|
* @param string|array|self $name1 The first name
|
|
|
|
* @param string|array|self $name2 The second name
|
|
|
|
* @param array $attributes Attributes to assign to concatenated name
|
|
|
|
*
|
|
|
|
* @return static Concatenated name
|
|
|
|
*/
|
|
|
|
public static function concat($name1, $name2, array $attributes = []) {
|
|
|
|
return new static(
|
|
|
|
array_merge(self::prepareName($name1), self::prepareName($name2)), $attributes
|
|
|
|
);
|
2011-08-04 16:30:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2015-06-12 23:05:28 +02:00
|
|
|
private static function prepareName($name) {
|
2011-08-04 16:30:04 +02:00
|
|
|
if (is_string($name)) {
|
|
|
|
return explode('\\', $name);
|
2011-09-28 18:11:28 +02:00
|
|
|
} elseif (is_array($name)) {
|
|
|
|
return $name;
|
2011-08-04 16:30:04 +02:00
|
|
|
} elseif ($name instanceof self) {
|
|
|
|
return $name->parts;
|
|
|
|
}
|
2011-09-28 18:11:28 +02:00
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
throw new \InvalidArgumentException(
|
2011-09-28 18:11:28 +02:00
|
|
|
'When changing a name you need to pass either a string, an array or a Name node'
|
|
|
|
);
|
2011-08-04 16:30:04 +02:00
|
|
|
}
|
2015-02-28 18:44:28 +01:00
|
|
|
}
|