php-parser/lib/NodeAbstract.php
nikic eeb63065be a) changes node structure (Stmt_, Expr_, ...) b) fixes parsing of x::$y[z]
Sorry for that one large commit. Won't happen again.
2011-05-27 18:42:36 +02:00

63 lines
1.7 KiB
PHP

<?php
abstract class NodeAbstract implements IteratorAggregate
{
protected $subNodes = array();
public function __construct(array $subNodes) {
$this->subNodes = $subNodes;
}
public function __get($name) {
if (!array_key_exists($name, $this->subNodes)) {
throw new OutOfBoundsException(
sprintf('"%s" has no subnode "%s"', $this->getType(), $name)
);
}
return $this->subNodes[$name];
}
public function getType() {
return substr(get_class($this), 5);
}
public function getIterator() {
return new ArrayIterator($this->subNodes);
}
public function __toString() {
$r = '[' . $this->getType() . ']';
foreach ($this->subNodes as $key => $value) {
$r .= "\n" . ' ' . $key . ': ';
if (null === $value) {
$r .= 'null';
} elseif (false === $value) {
$r .= 'false';
} elseif (true === $value) {
$r .= 'true';
} elseif (is_scalar($value)) {
$r .= $value;
} elseif ($value instanceof NodeAbstract) {
$lines = explode("\n", $value);
$r .= array_shift($lines);
foreach ($lines as $line) {
$r .= "\n" . ' ' . $line;
}
} elseif (is_array($value)) {
foreach ($value as $v) {
$lines = explode("\n", $v);
foreach ($lines as $line) {
$r .= "\n" . ' ' . $line;
}
}
} else {
die('UNEXPECTED!!!');
}
}
return $r;
}
}