Completed
Push — develop ( bec80e...e58249 )
by
unknown
09:35
created

AbstractLeafs::getValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2016 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Core\Entity\Tree;
12
13
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
14
use Core\Entity\Collection\ArrayCollection;
15
use Core\Entity\EntityTrait;
16
use Doctrine\Common\Collections\Collection;
17
18
/**
19
 * Main base class for attached or embedded tree leafs.
20
 *
21
 * @ODM\MappedSuperclass
22
 * @ODM\HasLifecycleCallbacks
23
 * @author Mathias Gelhausen <[email protected]>
24
 * @since 0.29
25
 */
26
abstract class AbstractLeafs implements LeafsInterface
27
{
28
    use EntityTrait;
29
30
    /**
31
     * The leafs.
32
     *
33
     * @ODM\ReferenceMany(discriminatorField="_entity", storeAs="dbRef", strategy="set", sort={"priority"="asc"})
34
     * @var Collection
35
     */
36
    private $items;
37
38
    /**
39
     * The item values of all attached leafs.
40
     *
41
     * To make the attached leafs searchable via mongo queries.
42
     *
43
     * @ODM\Collection
44
     * @var array
45
     */
46
    private $values;
47
48
    public function getItems()
49
    {
50
        if (!$this->items) {
51
            $this->setItems(new ArrayCollection());
52
        }
53
54
        return $this->items;
55
    }
56
57
    public function setItems(Collection $items)
58
    {
59
        $this->items = $items;
60
61
        return $this;
62
    }
63
64
    /**
65
     * Get the item values of the attached leafs.
66
     *
67
     * @return array
68
     */
69
    public function getValues()
70
    {
71
        return $this->values;
72
    }
73
74
    /**
75
     * Updates the item values.
76
     *
77
     * @ODM\PrePersist
78
     * @ODM\PreUpdate
79
     */
80
    public function updateValues()
81
    {
82
        $values = [];
83
        /* @var NodeInterface $item */
84
        foreach ($this->getItems() as $item) {
85
            $values[] = $item->getValueWithParents();
86
        }
87
88
        $this->values = $values;
89
    }
90
91
    /**
92
     * Get a list of the names of all attached leafs.
93
     *
94
     * Will prefix the leaf name with its parents, unless the parent is
95
     * the root node.
96
     *
97
     * @return string
98
     */
99
    public function __toString()
100
    {
101
        $items = [];
102
103
        /* @var NodeInterface $item */
104
        foreach ($this->getItems() as $item) {
105
            $parent = $item->getParent(); $nameParts = [];
106
            while ($parent) {
107
                $nextParent = $parent->getParent();
108
                if ($nextParent) { $nameParts[] = $parent->getName(); }
109
                $parent = $nextParent;
110
            }
111
            $nameParts[] = $item->getName();
112
            $items[]     = join(' | ' , $nameParts);
113
114
        }
115
116
        $items = join(', ', $items);
117
118
        return $items;
119
    }
120
}