Skip to content

Commit

Permalink
Merge pull request #13 from drupol/AttributeNode
Browse files Browse the repository at this point in the history
Add an AttributeNode node type.
  • Loading branch information
drupol authored Jun 24, 2019
2 parents e543e7b + c06d893 commit 81f4632
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
34 changes: 34 additions & 0 deletions spec/drupol/phptree/Node/AttributeNodeSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types = 1);

namespace spec\drupol\phptree\Node;

use drupol\phptree\Node\AttributeNode;
use PhpSpec\ObjectBehavior;

class AttributeNodeSpec extends ObjectBehavior
{
public 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');
}
public function it_is_initializable()
{
$this->shouldHaveType(AttributeNode::class);
}
}
68 changes: 68 additions & 0 deletions src/Node/AttributeNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types = 1);

namespace drupol\phptree\Node;

use drupol\phptree\Traverser\TraverserInterface;

/**
* Class AttributeNode.
*/
class AttributeNode extends NaryNode implements AttributeNodeInterface
{
/**
* ValueNode constructor.
*
* @param array $attributes
* @param null|int $capacity
* @param null|\drupol\phptree\Traverser\TraverserInterface $traverser
* @param null|\drupol\phptree\Node\NodeInterface $parent
*/
public function __construct(
array $attributes = [],
?int $capacity = 0,
?TraverserInterface $traverser = null,
?NodeInterface $parent = null
) {
parent::__construct($capacity, $traverser, $parent);

$this->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;
}
}
30 changes: 30 additions & 0 deletions src/Node/AttributeNodeInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types = 1);

namespace drupol\phptree\Node;

/**
* Interface AttributeNodeInterface.
*/
interface AttributeNodeInterface extends NaryNodeInterface
{
/**
* {@inheritdoc}
*/
public function getAttribute($key);
/**
* {@inheritdoc}
*/
public function getAttributes(): \Traversable;

/**
* {@inheritdoc}
*/
public function setAttribute(string $key, $value): AttributeNodeInterface;

/**
* {@inheritdoc}
*/
public function setAttributes(\Traversable $attributes): AttributeNodeInterface;
}

0 comments on commit 81f4632

Please sign in to comment.