| 1 |
<?php |
| 2 |
|
| 3 |
abstract class PHPParser_NodeAbstract implements PHPParser_Node, IteratorAggregate |
| 4 |
{ |
| 5 |
protected $subNodes; |
| 6 |
protected $attributes; |
| 7 |
|
| 8 |
/** |
| 9 |
* Creates a Node. |
| 10 |
* |
| 11 |
* @param array $subNodes Array of sub nodes |
| 12 |
* @param array $attributes Array of attributes |
| 13 |
*/ |
| 14 |
public function __construct(array $subNodes = array(), array $attributes = array()) { |
| 15 |
$this->subNodes = $subNodes; |
| 16 |
$this->attributes = $attributes; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Gets the type of the node. |
| 21 |
* |
| 22 |
* @return string Type of the node |
| 23 |
*/ |
| 24 |
public function getType() { |
| 25 |
return substr(get_class($this), 15); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Gets the names of the sub nodes. |
| 30 |
* |
| 31 |
* @return array Names of sub nodes |
| 32 |
*/ |
| 33 |
public function getSubNodeNames() { |
| 34 |
return array_keys($this->subNodes); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Gets line the node started in. |
| 39 |
* |
| 40 |
* @return int Line |
| 41 |
*/ |
| 42 |
public function getLine() { |
| 43 |
return $this->getAttribute('startLine', -1); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Sets line the node started in. |
| 48 |
* |
| 49 |
* @param int $line Line |
| 50 |
*/ |
| 51 |
public function setLine($line) { |
| 52 |
$this->setAttribute('startLine', (int) $line); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Gets the doc comment of the node. |
| 57 |
* |
| 58 |
* The doc comment has to be the last comment associated with the node. |
| 59 |
* |
| 60 |
* @return null|PHPParser_Comment_Doc Doc comment object or null |
| 61 |
*/ |
| 62 |
public function getDocComment() { |
| 63 |
$comments = $this->getAttribute('comments'); |
| 64 |
if (!$comments) { |
| 65 |
return null; |
| 66 |
} |
| 67 |
|
| 68 |
$lastComment = $comments[count($comments) - 1]; |
| 69 |
if (!$lastComment instanceof PHPParser_Comment_Doc) { |
| 70 |
return null; |
| 71 |
} |
| 72 |
|
| 73 |
return $lastComment; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* {@inheritDoc} |
| 78 |
*/ |
| 79 |
public function setAttribute($key, $value) { |
| 80 |
$this->attributes[$key] = $value; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* {@inheritDoc} |
| 85 |
*/ |
| 86 |
public function hasAttribute($key) { |
| 87 |
return array_key_exists($key, $this->attributes); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* {@inheritDoc} |
| 92 |
*/ |
| 93 |
public function &getAttribute($key, $default = null) { |
| 94 |
if (!array_key_exists($key, $this->attributes)) { |
| 95 |
return $default; |
| 96 |
} else { |
| 97 |
return $this->attributes[$key]; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* {@inheritDoc} |
| 103 |
*/ |
| 104 |
public function getAttributes() { |
| 105 |
return $this->attributes; |
| 106 |
} |
| 107 |
|
| 108 |
/* Magic interfaces */ |
| 109 |
|
| 110 |
public function &__get($name) { |
| 111 |
return $this->subNodes[$name]; |
| 112 |
} |
| 113 |
public function __set($name, $value) { |
| 114 |
$this->subNodes[$name] = $value; |
| 115 |
} |
| 116 |
public function __isset($name) { |
| 117 |
return isset($this->subNodes[$name]); |
| 118 |
} |
| 119 |
public function __unset($name) { |
| 120 |
unset($this->subNodes[$name]); |
| 121 |
} |
| 122 |
public function getIterator() { |
| 123 |
return new ArrayIterator($this->subNodes); |
| 124 |
} |
| 125 |
} |