Chris@0: attributes = $attributes; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the type of the node. Chris@0: * Chris@0: * @return string Type of the node Chris@0: */ Chris@0: public function getType() { Chris@0: return strtr(substr(rtrim(get_class($this), '_'), 15), '\\', '_'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets line the node started in. Chris@0: * Chris@0: * @return int Line Chris@0: */ Chris@0: public function getLine() { Chris@0: return $this->getAttribute('startLine', -1); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets line the node started in. Chris@0: * Chris@0: * @param int $line Line Chris@0: * Chris@0: * @deprecated Chris@0: */ Chris@0: public function setLine($line) { Chris@0: $this->setAttribute('startLine', (int) $line); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the doc comment of the node. Chris@0: * Chris@0: * The doc comment has to be the last comment associated with the node. Chris@0: * Chris@0: * @return null|Comment\Doc Doc comment object or null Chris@0: */ Chris@0: public function getDocComment() { Chris@0: $comments = $this->getAttribute('comments'); Chris@0: if (!$comments) { Chris@0: return null; Chris@0: } Chris@0: Chris@0: $lastComment = $comments[count($comments) - 1]; Chris@0: if (!$lastComment instanceof Comment\Doc) { Chris@0: return null; Chris@0: } Chris@0: Chris@0: return $lastComment; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the doc comment of the node. Chris@0: * Chris@0: * This will either replace an existing doc comment or add it to the comments array. Chris@0: * Chris@0: * @param Comment\Doc $docComment Doc comment to set Chris@0: */ Chris@0: public function setDocComment(Comment\Doc $docComment) { Chris@0: $comments = $this->getAttribute('comments', []); Chris@0: Chris@0: $numComments = count($comments); Chris@0: if ($numComments > 0 && $comments[$numComments - 1] instanceof Comment\Doc) { Chris@0: // Replace existing doc comment Chris@0: $comments[$numComments - 1] = $docComment; Chris@0: } else { Chris@0: // Append new comment Chris@0: $comments[] = $docComment; Chris@0: } Chris@0: Chris@0: $this->setAttribute('comments', $comments); Chris@0: } Chris@0: Chris@0: public function setAttribute($key, $value) { Chris@0: $this->attributes[$key] = $value; Chris@0: } Chris@0: Chris@0: public function hasAttribute($key) { Chris@0: return array_key_exists($key, $this->attributes); Chris@0: } Chris@0: Chris@0: public function &getAttribute($key, $default = null) { Chris@0: if (!array_key_exists($key, $this->attributes)) { Chris@0: return $default; Chris@0: } else { Chris@0: return $this->attributes[$key]; Chris@0: } Chris@0: } Chris@0: Chris@0: public function getAttributes() { Chris@0: return $this->attributes; Chris@0: } Chris@0: Chris@0: public function jsonSerialize() { Chris@0: return ['nodeType' => $this->getType()] + get_object_vars($this); Chris@0: } Chris@0: }