From c06d893a0d53178ee0ef2455d4dd10a909b0c934 Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Mon, 24 Jun 2019 22:07:13 +0200 Subject: [PATCH] Add an AttributeNode node type. --- .../drupol/phptree/Node/AttributeNodeSpec.php | 34 ++++++++++ src/Node/AttributeNode.php | 68 +++++++++++++++++++ src/Node/AttributeNodeInterface.php | 30 ++++++++ 3 files changed, 132 insertions(+) create mode 100644 spec/drupol/phptree/Node/AttributeNodeSpec.php create mode 100644 src/Node/AttributeNode.php create mode 100644 src/Node/AttributeNodeInterface.php diff --git a/spec/drupol/phptree/Node/AttributeNodeSpec.php b/spec/drupol/phptree/Node/AttributeNodeSpec.php new file mode 100644 index 0000000..68c054a --- /dev/null +++ b/spec/drupol/phptree/Node/AttributeNodeSpec.php @@ -0,0 +1,34 @@ + 'bar', + ]; + + $attributes = new \ArrayObject($attributes); + + $this + ->setAttributes($attributes) + ->getAttributes() + ->shouldReturn($attributes); + + $this + ->setAttribute('bar', 'foo') + ->getAttribute('bar') + ->shouldReturn('foo'); + } + public function it_is_initializable() + { + $this->shouldHaveType(AttributeNode::class); + } +} 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..1f86a56 --- /dev/null +++ b/src/Node/AttributeNodeInterface.php @@ -0,0 +1,30 @@ +