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 = array();
|
|
|
|
|
2011-05-31 16:33:11 +02:00
|
|
|
/**
|
|
|
|
* Creates a Node.
|
|
|
|
*
|
|
|
|
* @param array $subNodes Array of sub nodes
|
|
|
|
*/
|
2011-04-18 19:02:30 +02:00
|
|
|
public function __construct(array $subNodes) {
|
|
|
|
$this->subNodes = $subNodes;
|
|
|
|
}
|
|
|
|
|
2011-05-31 16:33:11 +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) {
|
2011-05-27 18:20:44 +02:00
|
|
|
if (!array_key_exists($name, $this->subNodes)) {
|
2011-05-31 16:33:11 +02:00
|
|
|
throw new InvalidArgumentException(
|
2011-05-27 18:20:44 +02:00
|
|
|
sprintf('"%s" has no subnode "%s"', $this->getType(), $name)
|
|
|
|
);
|
2011-04-18 19:02:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->subNodes[$name];
|
|
|
|
}
|
|
|
|
|
2011-06-02 16:29:28 +02:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2011-05-31 16:33:11 +02:00
|
|
|
/**
|
|
|
|
* 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]);
|
|
|
|
}
|
|
|
|
|
2011-05-31 16:33:11 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
2011-05-31 16:33:11 +02:00
|
|
|
*
|
|
|
|
* @return string Type of this node
|
|
|
|
*/
|
2011-05-27 18:20:44 +02:00
|
|
|
public function getType() {
|
2011-06-05 18:40:04 +02:00
|
|
|
return substr(get_class($this), 15);
|
2011-05-27 18:20:44 +02:00
|
|
|
}
|
|
|
|
|
2011-05-31 16:33:11 +02:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
}
|