Skip to content

Commit 81f4632

Browse files
authored
Merge pull request #13 from drupol/AttributeNode
Add an AttributeNode node type.
2 parents e543e7b + c06d893 commit 81f4632

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace spec\drupol\phptree\Node;
6+
7+
use drupol\phptree\Node\AttributeNode;
8+
use PhpSpec\ObjectBehavior;
9+
10+
class AttributeNodeSpec extends ObjectBehavior
11+
{
12+
public function it_can_set_and_get_the_attributes()
13+
{
14+
$attributes = [
15+
'foo' => 'bar',
16+
];
17+
18+
$attributes = new \ArrayObject($attributes);
19+
20+
$this
21+
->setAttributes($attributes)
22+
->getAttributes()
23+
->shouldReturn($attributes);
24+
25+
$this
26+
->setAttribute('bar', 'foo')
27+
->getAttribute('bar')
28+
->shouldReturn('foo');
29+
}
30+
public function it_is_initializable()
31+
{
32+
$this->shouldHaveType(AttributeNode::class);
33+
}
34+
}

src/Node/AttributeNode.php

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace drupol\phptree\Node;
6+
7+
use drupol\phptree\Traverser\TraverserInterface;
8+
9+
/**
10+
* Class AttributeNode.
11+
*/
12+
class AttributeNode extends NaryNode implements AttributeNodeInterface
13+
{
14+
/**
15+
* ValueNode constructor.
16+
*
17+
* @param array $attributes
18+
* @param null|int $capacity
19+
* @param null|\drupol\phptree\Traverser\TraverserInterface $traverser
20+
* @param null|\drupol\phptree\Node\NodeInterface $parent
21+
*/
22+
public function __construct(
23+
array $attributes = [],
24+
?int $capacity = 0,
25+
?TraverserInterface $traverser = null,
26+
?NodeInterface $parent = null
27+
) {
28+
parent::__construct($capacity, $traverser, $parent);
29+
30+
$this->storage()->set('attributes', new \ArrayObject($attributes));
31+
}
32+
33+
/**
34+
* {@inheritdoc}
35+
*/
36+
public function getAttribute($key)
37+
{
38+
return $this->getAttributes()[$key] ?? null;
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
public function getAttributes(): \Traversable
45+
{
46+
return $this->storage()->get('attributes');
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
public function setAttribute(string $key, $value): AttributeNodeInterface
53+
{
54+
$this->getAttributes()[$key] = $value;
55+
56+
return $this;
57+
}
58+
59+
/**
60+
* {@inheritdoc}
61+
*/
62+
public function setAttributes(\Traversable $attributes): AttributeNodeInterface
63+
{
64+
$this->storage()->set('attributes', $attributes);
65+
66+
return $this;
67+
}
68+
}

src/Node/AttributeNodeInterface.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace drupol\phptree\Node;
6+
7+
/**
8+
* Interface AttributeNodeInterface.
9+
*/
10+
interface AttributeNodeInterface extends NaryNodeInterface
11+
{
12+
/**
13+
* {@inheritdoc}
14+
*/
15+
public function getAttribute($key);
16+
/**
17+
* {@inheritdoc}
18+
*/
19+
public function getAttributes(): \Traversable;
20+
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function setAttribute(string $key, $value): AttributeNodeInterface;
25+
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public function setAttributes(\Traversable $attributes): AttributeNodeInterface;
30+
}

0 commit comments

Comments
 (0)