Completed
Pull Request — develop (#323)
by Mathias
15:36
created

AbstractLeafs::__toString()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 12
nc 4
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
 * @author Mathias Gelhausen <[email protected]>
23
 * @since 0.29
24
 */
25
abstract class AbstractLeafs implements LeafsInterface
26
{
27
    use EntityTrait;
28
29
    /**
30
     * The leafs.
31
     *
32
     * @ODM\ReferenceMany(discriminatorField="_entity", storeAs="dbRef", strategy="set", sort={"priority"="asc"})
33
     * @var Collection
34
     */
35
    private $items;
36
37
    public function getItems()
38
    {
39
        if (!$this->items) {
40
            $this->setItems(new ArrayCollection());
41
        }
42
43
        return $this->items;
44
    }
45
46
    public function setItems(Collection $items)
47
    {
48
        $this->items = $items;
49
50
        return $this;
51
    }
52
53
    /**
54
     * Get a list of the names of all attached leafs.
55
     *
56
     * Will prefix the leaf name with its parents, unless the parent is
57
     * the root node.
58
     *
59
     * @return string
60
     */
61
    public function __toString()
62
    {
63
        $items = [];
64
65
        /* @var NodeInterface $item */
66
        foreach ($this->getItems() as $item) {
67
            $parent = $item->getParent(); $nameParts = [];
68
            while ($parent) {
69
                $nextParent = $parent->getParent();
70
                if ($nextParent) { $nameParts[] = $parent->getName(); }
71
                $parent = $nextParent;
72
            }
73
            $nameParts[] = $item->getName();
74
            $items[]     = join(' / ' , $nameParts);
75
76
        }
77
78
        $items = join(', ', $items);
79
80
        return $items;
81
    }
82
}