BlockGenerator::stopIndention()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * @author sleibelt
4
 * @since 2014-04-25
5
 */
6
7
namespace Net\Bazzline\Component\CodeGenerator;
8
9
/**
10
 * Class BlockGenerator
11
 *
12
 * @package Net\Bazzline\Component\Locator\LocatorGenerator
13
 * @todo add helpers like "startWhile", "startIf" and so one
14
 */
15
class BlockGenerator extends AbstractContentGenerator implements LineGeneratorDependentInterface
16
{
17
    /**
18
     * @var boolean
19
     */
20
    private $addIndented;
21
22
    /**
23
     * @var array|BlockGenerator[]|LineGenerator[]
24
     */
25
    private $content = [];
26
27
    /**
28
     * @var LineGenerator
29
     */
30
    private $lineGenerator;
31
32
    /**
33
     * @param LineGenerator $lineGenerator
34
     * @param Indention $indention
35
     * @param null|string|array|GeneratorInterface $content
36
     */
37
    public function __construct(LineGenerator $lineGenerator, Indention $indention, $content = null)
38
    {
39
        $this->addIndented = false;
40
        $this->lineGenerator = $lineGenerator;
41
        parent::__construct($indention, $content);
42
    }
43
44
    /**
45
     * @param LineGenerator $generator
46
     * @return $this
47
     */
48
    public function setLineGenerator(LineGenerator $generator)
49
    {
50
        $this->lineGenerator = $generator;
51
52
        return $this;
53
    }
54
55
    /**
56
     * @param Indention $indention
57
     * @return $this
58
     */
59
    public function setIndention(Indention $indention)
60
    {
61
        parent::setIndention($indention);
62
        $this->lineGenerator->setIndention($indention);
63
64
        return $this;
65
    }
66
67
    /**
68
     * @param array|GeneratorInterface|string $content
69
     * @return $this
70
     * @throws InvalidArgumentException
71
     * @todo rename to addContent, easy up this method by working on todo in generate method
72
     */
73
    public function add($content)
74
    {
75
        $indention = ($this->addIndented) ? $this->getIndention()->toString() : '';
76
77
        if (is_string($content)) {
78
            $lineOfContent = $this->getLineGenerator($indention . $content);
79
            $this->content[] = $lineOfContent;
80 View Code Duplication
        } else if (is_array($content)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
            foreach ($content as $part) {
82
                $this->add($part);
83
            }
84
        } else if ($content instanceof AbstractContentGenerator) {
85
            $content->setIndention($this->getIndention());
86
            $this->content[] = $content;
87
        } else {
88
            throw new InvalidArgumentException('content has to be string, an array or instance of AbstractContentGenerator');
89
        }
90
91
        return $this;
92
    }
93
94
    /**
95
     * @return $this
96
     */
97
    public function clear()
98
    {
99
        $this->content = [];
100
101
        return $this;
102
    }
103
104
    /**
105
     * @return bool
106
     */
107
    public function hasContent()
108
    {
109
        return (!empty($this->content));
110
    }
111
112
    /**
113
     * @throws InvalidArgumentException|RuntimeException
114
     * @return string
115
     * @todo implement support for string|array beside AbstractContentGenerator in $this->content
116
     */
117
    public function generate()
118
    {
119
        $string = '';
120
        $arrayKeys = array_keys($this->content);
121
        $lastKey = array_pop($arrayKeys);
122
123
        foreach ($this->content as $key => $content) {
124
            if ($content->hasContent()) {
125
                if ($content instanceof BlockGenerator) {
126
                    $this->getIndention()->increaseLevel();
127
                    $string .= $content->generate();
128
                    $this->getIndention()->decreaseLevel();
129
                } else {
130
                    $string .= $content->generate();
131
                }
132
                if ($key !== $lastKey) {
133
                    $string .= PHP_EOL;
134
                }
135
            }
136
        }
137
138
        return $string;
139
    }
140
141
    /**
142
     * @return int
143
     */
144
    public function count()
145
    {
146
        return (count($this->content));
147
    }
148
149
    /**
150
     * @todo find better name
151
     * @return $this
152
     */
153
    public function startIndention()
154
    {
155
        $this->getIndention()->increaseLevel();
156
        $this->addIndented = true;
157
158
        return $this;
159
    }
160
161
    /**
162
     * @todo find better name
163
     * @return $this
164
     */
165
    public function stopIndention()
166
    {
167
        $this->getIndention()->decreaseLevel();
168
        if ($this->getIndention()->isSetToInitialLevel()) {
169
            $this->addIndented = false;
170
        }
171
172
        return $this;
173
    }
174
175
    /**
176
     * @param null|string $content
177
     * @return LineGenerator
178
     */
179
    final protected function getLineGenerator($content = null)
180
    {
181
        $line = clone $this->lineGenerator;
182
183
        if (!is_null($content)) {
184
            $line->add($content);
185
        }
186
187
        return $line;
188
    }
189
}
190