5 abstract class NodeAbstract implements Node, \JsonSerializable
12 * @param array $attributes Array of attributes
14 public function __construct(array $attributes = array()) {
15 $this->attributes = $attributes;
19 * Gets the type of the node.
21 * @return string Type of the node
23 public function getType() {
24 return strtr(substr(rtrim(get_class($this), '_'), 15), '\\', '_');
28 * Gets line the node started in.
32 public function getLine() {
33 return $this->getAttribute('startLine', -1);
37 * Sets line the node started in.
39 * @param int $line Line
41 public function setLine($line) {
42 $this->setAttribute('startLine', (int) $line);
46 * Gets the doc comment of the node.
48 * The doc comment has to be the last comment associated with the node.
50 * @return null|Comment\Doc Doc comment object or null
52 public function getDocComment() {
53 $comments = $this->getAttribute('comments');
58 $lastComment = $comments[count($comments) - 1];
59 if (!$lastComment instanceof Comment\Doc) {
67 * Sets the doc comment of the node.
69 * This will either replace an existing doc comment or add it to the comments array.
71 * @param Comment\Doc $docComment Doc comment to set
73 public function setDocComment(Comment\Doc $docComment) {
74 $comments = $this->getAttribute('comments', []);
76 $numComments = count($comments);
77 if ($numComments > 0 && $comments[$numComments - 1] instanceof Comment\Doc) {
78 // Replace existing doc comment
79 $comments[$numComments - 1] = $docComment;
82 $comments[] = $docComment;
85 $this->setAttribute('comments', $comments);
88 public function setAttribute($key, $value) {
89 $this->attributes[$key] = $value;
92 public function hasAttribute($key) {
93 return array_key_exists($key, $this->attributes);
96 public function &getAttribute($key, $default = null) {
97 if (!array_key_exists($key, $this->attributes)) {
100 return $this->attributes[$key];
104 public function getAttributes() {
105 return $this->attributes;
108 public function jsonSerialize() {
109 return ['nodeType' => $this->getType()] + get_object_vars($this);