Completed
Push — master ( ee7457...ea6079 )
by Vitaliy
05:13
created

ParentNodeTrait::__wakeup()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.2
cc 4
eloc 9
nc 4
nop 0
1
<?php
2
3
namespace Nayjest\Tree;
4
5
use Nayjest\Collection\Extended\ObjectCollection;
6
use Traversable;
7
8
/**
9
 * Trait ParentNodeTrait.
10
 *
11
 * @implements ParentNodeInterface
12
 * @see ParentNodeInterface
13
 */
14
trait ParentNodeTrait
15
{
16
    /**
17
     * @var NodeCollection|ChildNodeInterface[]
18
     */
19
    protected $collection;
20
21
    /**
22
     * Returns default child components.
23
     *
24
     * Override this method if you need.
25
     *
26
     * @return ChildNodeInterface[]
27
     */
28
    protected function defaultChildren()
29
    {
30
        return [];
31
    }
32
33
    /**
34
     * @param array $items
35
     */
36
    protected function initializeCollection(array $items)
37
    {
38
        /* @var ParentNodeInterface|ParentNodeTrait $this */
39
        $this->collection = new NodeCollection(
40
            $this,
41
            $items
42
        );
43
    }
44
45
    /**
46
     * Returns child components.
47
     *
48
     * @return \Nayjest\Collection\CollectionInterface
49
     */
50
    public function children()
51
    {
52
        if ($this->collection === null) {
53
            $this->initializeCollection($this->defaultChildren());
54
        }
55
56
        return $this->collection;
57
    }
58
59
    /**
60
     * @return bool
61
     */
62
    final public function isWritable()
63
    {
64
        return $this->children()->isWritable();
65
    }
66
67
    /**
68
     * @return ObjectCollection
69
     */
70
    public function getChildrenRecursive()
71
    {
72
        $res = new ObjectCollection();
73
        foreach ($this->children() as $child) {
74
            $res->add($child);
75
            if ($child instanceof ParentNodeInterface) {
76
                $res->addMany($child->getChildrenRecursive());
77
            }
78
        }
79
80
        return $res;
81
    }
82
83
    /**
84
     * @param array|Traversable $children
85
     *
86
     * @return $this
87
     */
88
    public function setChildren($children)
89
    {
90
        $this->children()->set($children);
0 ignored issues
show
Bug introduced by
The method set() does not seem to exist on object<Nayjest\Collectio...adonlyObjectCollection>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
91
92
        return $this;
93
    }
94
95
    /**
96
     * @param $item
97
     *
98
     * @return $this
99
     */
100
    public function addChild(ChildNodeInterface $item)
101
    {
102
        $this->children()->add($item);
0 ignored issues
show
Bug introduced by
The method add() does not seem to exist on object<Nayjest\Collectio...adonlyObjectCollection>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
103
104
        return $this;
105
    }
106
107
    /**
108
     * @param array|Traversable $children
109
     *
110
     * @return $this
111
     */
112
    public function addChildren($children)
113
    {
114
        $this->children()->addMany($children);
0 ignored issues
show
Bug introduced by
The method addMany() does not seem to exist on object<Nayjest\Collectio...adonlyObjectCollection>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
115
116
        return $this;
117
    }
118
119
    public function __wakeup()
120
    {
121
        if ($this->collection === null) {
122
            return;
123
        }
124
        foreach($this->collection as $child)
125
        {
126
            $listeners = $child->listeners('parent.change');
127
            $child->removeAllListeners('parent.change');
128
            $child->internalSetParent($this);
129
            foreach($listeners as $listener) {
130
                $child->on('parent.change', $listener);
131
            }
132
        }
133
    }
134
}
135