1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2025-01-22 22:01:18 +01:00
PHP-Parser/lib/PHPParser/NodeAbstract.php

88 lines
2.0 KiB
PHP
Raw Normal View History

2011-04-18 19:02:30 +02:00
<?php
2011-06-05 18:40:04 +02:00
abstract class PHPParser_NodeAbstract implements IteratorAggregate
2011-04-18 19:02:30 +02:00
{
protected $subNodes;
protected $line;
2011-04-18 19:02:30 +02:00
/**
* Creates a Node.
*
* @param array $subNodes Array of sub nodes
* @param int $line Line
*/
public function __construct(array $subNodes, $line = -1) {
2011-04-18 19:02:30 +02:00
$this->subNodes = $subNodes;
$this->line = $line;
2011-04-18 19:02:30 +02:00
}
/**
* Gets a sub node.
*
* @param string $name Name of sub node
*
* @return mixed Sub node
*/
2011-04-18 19:02:30 +02:00
public function __get($name) {
if (!array_key_exists($name, $this->subNodes)) {
throw new InvalidArgumentException(
sprintf('"%s" has no subnode "%s"', $this->getType(), $name)
);
2011-04-18 19:02:30 +02:00
}
return $this->subNodes[$name];
}
/**
* Sets a sub node.
*
* @param string $name Name of sub node
* @param mixed $value Value to set sub node to
*/
public function __set($name, $value) {
$this->subNodes[$name] = $value;
}
/**
* Checks whether a subnode exists.
*
* @param string $name Name of sub node
*
* @return bool Whether the sub node exists
*/
2011-05-30 22:11:11 +02:00
public function __isset($name) {
return isset($this->subNodes[$name]);
}
/**
* Gets the type of this node.
*
* The type of a node is the node's class name without the
2011-06-05 18:40:04 +02:00
* PHPParser_Node_ prefix.
*
* @return string Type of this node
*/
public function getType() {
2011-06-05 18:40:04 +02:00
return substr(get_class($this), 15);
}
/**
* Gets line the node *ended* in.
*
* TODO: We probably want the line it started in...
*
* @return int Line
*/
public function getLine() {
return $this->line;
}
/**
* Gets an Iterator for the sub nodes.
*
* @return ArrayIterator Iterator for sub nodes
*/
2011-04-18 19:02:30 +02:00
public function getIterator() {
return new ArrayIterator($this->subNodes);
}
}