|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nayjest\Tree; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use Nayjest\Collection\Extended\ObjectCollection; |
|
7
|
|
|
use Nayjest\Tree\Exception\LockedNodeException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class NodeCollection. |
|
11
|
|
|
* |
|
12
|
|
|
* NodeCollection in addition to basic collection facilities |
|
13
|
|
|
* manages parent-child relationships and guarantees tree structure integrity. |
|
14
|
|
|
*/ |
|
15
|
|
|
class NodeCollection extends ObjectCollection |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var ParentNodeInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $parentNode; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct( |
|
23
|
|
|
ParentNodeInterface $parentNode, |
|
24
|
|
|
array $nodes = null |
|
25
|
|
|
) { |
|
26
|
|
|
$this->parentNode = $parentNode; |
|
27
|
|
|
parent::__construct($nodes); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Adds component to collection. |
|
32
|
|
|
* |
|
33
|
|
|
* If component is already in collection, it will not be added twice. |
|
34
|
|
|
* |
|
35
|
|
|
* @param ChildNodeInterface $item |
|
36
|
|
|
* @param bool $prepend Pass true to add component to the beginning of an array. |
|
37
|
|
|
* |
|
38
|
|
|
* @return $this |
|
39
|
|
|
*/ |
|
40
|
|
|
public function add($item, $prepend = false) |
|
41
|
|
|
{ |
|
42
|
|
|
if (!$item instanceof ChildNodeInterface) { |
|
43
|
|
|
$details = is_object($item) ? get_class($item) : gettype($item); |
|
44
|
|
|
throw new InvalidArgumentException( |
|
45
|
|
|
"NodeCollection accepts only objects implementing ChildNodeInterface, $details given." |
|
46
|
|
|
); |
|
47
|
|
|
} |
|
48
|
|
|
$old = $item->parent(); |
|
49
|
|
|
if ($old === $this->parentNode) { |
|
50
|
|
|
return $this; |
|
|
|
|
|
|
51
|
|
|
} elseif ($old !== null) { |
|
52
|
|
|
$item->detach(); |
|
53
|
|
|
} |
|
54
|
|
|
$this->checkUnlocked($item); |
|
55
|
|
|
parent::add($item, $prepend); |
|
56
|
|
|
$item->internalSetParent($this->parentNode); |
|
57
|
|
|
|
|
58
|
|
|
return $this; |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param ChildNodeInterface $item |
|
63
|
|
|
* |
|
64
|
|
|
* @return $this |
|
65
|
|
|
*/ |
|
66
|
|
|
public function remove($item) |
|
67
|
|
|
{ |
|
68
|
|
|
if ($item->parent() === $this->parentNode) { |
|
69
|
|
|
$this->checkUnlocked($item); |
|
70
|
|
|
$item->internalUnsetParent(); |
|
71
|
|
|
parent::remove($item); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $this; |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function clear() |
|
78
|
|
|
{ |
|
79
|
|
|
/** @var ChildNodeInterface $item */ |
|
80
|
|
|
foreach ($this->items() as $item) { |
|
81
|
|
|
$this->checkUnlocked($item); |
|
82
|
|
|
$item->internalUnsetParent(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return parent::clear(); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
protected function createCollection(array $items) |
|
89
|
|
|
{ |
|
90
|
|
|
return new ObjectCollection($items); |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
private function checkUnlocked(ChildNodeInterface $child) |
|
94
|
|
|
{ |
|
95
|
|
|
if ($child->isLocked()) { |
|
96
|
|
|
throw new LockedNodeException(); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.