diff --git a/spec/drupol/phptree/Node/AttributeNodeSpec.php b/spec/drupol/phptree/Node/AttributeNodeSpec.php new file mode 100644 index 0000000..14d0269 --- /dev/null +++ b/spec/drupol/phptree/Node/AttributeNodeSpec.php @@ -0,0 +1,34 @@ +shouldHaveType(AttributeNode::class); + } + + function it_can_set_and_get_the_attributes() + { + $attributes = [ + 'foo' => 'bar', + ]; + + $attributes = new \ArrayObject($attributes); + + $this + ->setAttributes($attributes) + ->getAttributes() + ->shouldReturn($attributes); + + $this + ->setAttribute('bar', 'foo') + ->getAttribute('bar') + ->shouldReturn('foo'); + } +} diff --git a/src/Node/AttributeNode.php b/src/Node/AttributeNode.php new file mode 100644 index 0000000..2f8fa4a --- /dev/null +++ b/src/Node/AttributeNode.php @@ -0,0 +1,68 @@ +storage()->set('attributes', new \ArrayObject($attributes)); + } + + /** + * {@inheritdoc} + */ + public function getAttribute($key) + { + return $this->getAttributes()[$key] ?? null; + } + + /** + * {@inheritdoc} + */ + public function getAttributes(): \Traversable + { + return $this->storage()->get('attributes'); + } + + /** + * {@inheritdoc} + */ + public function setAttribute(string $key, $value): AttributeNodeInterface + { + $this->getAttributes()[$key] = $value; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function setAttributes(\Traversable $attributes): AttributeNodeInterface + { + $this->storage()->set('attributes', $attributes); + + return $this; + } +} diff --git a/src/Node/AttributeNodeInterface.php b/src/Node/AttributeNodeInterface.php new file mode 100644 index 0000000..61fe187 --- /dev/null +++ b/src/Node/AttributeNodeInterface.php @@ -0,0 +1,30 @@ +