ConstantCollection::toString()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4286
cc 2
eloc 6
nc 2
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of the ClassGeneration package.
5
 *
6
 * (c) Antonio Spinelli <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ClassGeneration;
13
14
use ClassGeneration\Collection\ArrayCollection;
15
16
/**
17
 * Constant Collection ClassGeneration
18
 * @author Antonio Spinelli <[email protected]>
19
 */
20
class ConstantCollection extends ArrayCollection
21
{
22
23
    /**
24
     * Adds a new Constant at ConstantCollection.
25
     *
26
     * @param ConstantInterface $constant
27
     *
28
     * @throws \InvalidArgumentException
29
     * @return bool
30
     */
31 4
    public function add($constant)
32
    {
33 4
        if (!$constant instanceof ConstantInterface) {
34 1
            throw new \InvalidArgumentException(
35
                'This Constant must be a instance of \ClassGeneration\ConstantInterface'
36 1
            );
37
        }
38
39 4
        if ($constant->getName() === null) {
40 2
            $constant->setName('constant' . ($this->count() + 1));
41 2
        }
42
43 4
        return parent::add($constant);
44
    }
45
46
    /**
47
     * Gets the element of the collection at the current internal iterator position.
48
     * @return ConstantInterface
49
     */
50 2
    public function current()
51
    {
52 2
        return parent::current();
53
    }
54
55
    /**
56
     * Gets the Constant Iterator.
57
     * @return ConstantIterator|Constant[]
58
     */
59 11
    public function getIterator()
60
    {
61 11
        return new ConstantIterator($this);
62
    }
63
64
    /**
65
     * Parse the Constant Collection to string.
66
     * @return string
67
     */
68 10
    public function toString()
69
    {
70 10
        $string = '';
71 10
        $constants = $this->getIterator();
72 10
        foreach ($constants as $constant) {
73 1
            $string .= $constant->toString();
74 10
        }
75
76 10
        return $string;
77
    }
78
79
    /**
80
     * Removes tags by name.
81
     *
82
     * @param $constantName
83
     *
84
     * @return ConstantCollection
85
     */
86 1
    public function removeByName($constantName)
87
    {
88 1
        $removedList = new self();
89 1
        $list = $this->getIterator();
90 1
        foreach ($list as $index => $constant) {
91 1
            $currentName = $constant->getName();
92 1
            if ((is_array($constantName) && in_array($currentName, $constantName))
93 1
                || ($constant->getName() === $constantName)
94 1
            ) {
95 1
                $removedList->add(clone $constant);
96 1
                $this->remove($index);
97 1
            }
98 1
        }
99
100 1
        return $removedList;
101
    }
102
}
103