Browse Source

Basic DOM

master
Adam Pippin 3 years ago
parent
commit
f6992724ee
  1. 4
      app/Dom/Document.php
  2. 95
      app/Dom/Node.php
  3. 18
      app/Dom/NodeFunction.php
  4. 32
      app/Dom/NodeFunctionValue.php
  5. 32
      app/Dom/NodeValue.php

4
app/Dom/Document.php

@ -11,4 +11,8 @@ namespace App\Dom;
*/
class Document extends Node
{
public function __construct()
{
$this->children = [];
}
}

95
app/Dom/Node.php

@ -9,7 +9,98 @@ namespace App\Dom;
*
* @author Adam Pippin <hello@adampippin.ca>
*/
class Node
class Node implements \ArrayAccess, \Iterator
{
// parent, children, type
/** @var int */
private $_idx;
/** @var string */
protected $name;
/** @var Node */
protected $parent;
/** @var Node[] */
protected $children;
public function __construct(Node $parent, ?string $name)
{
$this->parent = $parent;
$this->children = [];
$this->name = $name;
}
public function setName(string $name)
{
$this->name = $name;
}
public function getName(): string
{
return $this->name;
}
public function getParent(): Node
{
return $this->parent;
}
public function getChildren(): array
{
return $this->children;
}
public function addChild(Node $child)
{
$this->children[] = $child;
}
// ArrayAccess
public function offsetExists($offset): bool
{
return isset($this->children[$offset]);
}
public function offsetGet($offset)
{
return $this->children[$offset];
}
public function offsetSet($offset, $value)
{
$this->children[$offset] = $value;
}
public function offsetUnset($offset)
{
unset($this->children[$offset]);
}
// Iterator
public function current()
{
return $this->children[$this->_idx];
}
public function key()
{
return $this->_idx;
}
public function next()
{
$this->_idx++;
}
public function rewind()
{
$this->_idx = 0;
}
public function valid()
{
return isset($this->children[$this->_idx]);
}
}

18
app/Dom/NodeFunction.php

@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace App\Dom;
/**
* Node in a document that represents a function call.
*
* @author Adam Pippin <hello@adampippin.ca>
*/
class NodeFunction extends Node
{
public function __construct(Node $parent, ?string $name)
{
parent::__construct($parent, $name);
}
}

32
app/Dom/NodeFunctionValue.php

@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace App\Dom;
/**
* Node in a document that represents a function call with a value.
*
* @author Adam Pippin <hello@adampippin.ca>
*/
class NodeFunctionValue extends NodeFunction
{
/** @var mixed */
protected $value;
public function __construct(Node $parent, ?string $name, $value)
{
parent::__construct($parent, $name);
$this->value = $value;
}
public function setValue($value)
{
$this->value = $value;
}
public function getValue()
{
return $this->value;
}
}

32
app/Dom/NodeValue.php

@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace App\Dom;
/**
* Node in a document that contains a value.
*
* @author Adam Pippin <hello@adampippin.ca>
*/
class NodeValue extends Node
{
/** @var mixed */
protected $value;
public function __construct(Node $parent, ?string $name, $value)
{
parent::__construct($parent, $name);
$this->value = $value;
}
public function setValue($value)
{
$this->value = $value;
}
public function getValue()
{
return $this->value;
}
}
Loading…
Cancel
Save