PlainTextPrinter::clearTextBuffer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * Copyright (c) 2011-2015, Celestino Diaz <[email protected]>
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
25
namespace Brickoo\Component\IO\Printing;
26
27
use Brickoo\Component\Common\Assert;
28
29
/**
30
 * PlainTextPrinter
31
 *
32
 * Implementation of a plain text printer.
33
 * @author Celestino Diaz <[email protected]>
34
 */
35
class PlainTextPrinter implements Printer {
36
37
    /** @const integer */
38
    const INDENT_TABS = "\t";
39
    const INDENT_SPACES = " ";
40
41
    /** @var \Brickoo\Component\IO\Printing\OutputPrinter */
42
    private $outputRenderer;
43
44
    /** @var string */
45
    private $eolSeparator;
46
47
    /** @var string */
48
    private $indentMode;
49
50
    /** @var integer */
51
    private $indentationAmount;
52
53
    /** @var string */
54
    private $bufferedTextLine;
55
56
    /**
57
     * Class constructor.
58
     * @param \Brickoo\Component\IO\Printing\OutputPrinter $outputRenderer
59
     * @param string $indentMode
60
     * @param string $eolSeparator
61
     * @throws \InvalidArgumentException
62
     */
63 2
    public function __construct(OutputPrinter $outputRenderer, $indentMode = self::INDENT_TABS, $eolSeparator = PHP_EOL) {
64 2
        Assert::isString($indentMode);
65 2
        Assert::isString($eolSeparator);
66 2
        $this->indentationAmount = 0;
67 2
        $this->bufferedTextLine = "";
68 2
        $this->outputRenderer = $outputRenderer;
69 2
        $this->eolSeparator = $eolSeparator;
70 2
        $this->indentMode = $indentMode;
71 2
    }
72
73
    /** {@inheritdoc} */
74 2
    public function nextLine() {
75 2
        $this->doPrint();
76 2
        $this->getOutputPrinter()->doPrint($this->eolSeparator);
77 2
        return $this;
78
    }
79
80
    /** {@inheritdoc} */
81 2
    public function indent($amount = 1) {
82 2
        Assert::isInteger($amount);
83
84 2
        if ($this->hasBufferedText()) {
85 1
            $this->addText($this->getIndentation($amount));
86 1
            return $this;
87
        }
88 2
        $this->indentationAmount += $amount;
89 2
        return $this;
90
    }
91
92
    /** {@inheritdoc} */
93 1
    public function outdent($amount = 1) {
94 1
        Assert::isInteger($amount);
95 1
        $this->indentationAmount -= $amount;
96 1
        $this->indentationAmount = $this->indentationAmount < 0
97 1
            ? 0 : $this->indentationAmount;
98 1
        return $this;
99
    }
100
101
    /** {@inheritdoc} */
102 3
    public function addText($text) {
103 3
        Assert::isString($text);
104
105 3
        if ((!$this->hasBufferedText()) && $this->indentationAmount > 0) {
106 2
            $this->bufferedTextLine .= $this->getIndentation($this->indentationAmount);
107 2
        }
108
109 3
        $this->bufferedTextLine .= $text;
110 3
        return $this;
111
    }
112
113
    /** {@inheritdoc} */
114 1
    public function doPrint() {
115 1
        if ($this->hasBufferedText()) {
116 1
            $this->getOutputPrinter()->doPrint($this->bufferedTextLine);
117 1
            $this->clearTextBuffer();
118 1
        }
119 1
        return $this;
120
    }
121
122
    /**
123
     * Return the output renderer.
124
     * @return \Brickoo\Component\IO\Printing\OutputPrinter
125
     */
126 1
    private function getOutputPrinter() {
127 1
        return $this->outputRenderer;
128
    }
129
130
    /**
131
     * Check if the buffer contains text.
132
     * @return boolean check result
133
     */
134 1
    private function hasBufferedText() {
135 1
        return (!empty($this->bufferedTextLine));
136
    }
137
138
    /**
139
     * Clear the text buffer.
140
     * @return \Brickoo\Component\IO\Printing\PlainTextPrinter
141
     */
142 1
    private function clearTextBuffer() {
143 1
        $this->bufferedTextLine = "";
144 1
        return $this;
145
    }
146
147
    /**
148
     * Return the indentation characters.
149
     * @param integer $amount
150
     * @return string the indentation characters
151
     */
152 1
    private function getIndentation($amount) {
153 1
        if ($this->indentMode == self::INDENT_SPACES) {
154 1
            $amount = $amount * 4;
155 1
        }
156 1
        return str_repeat($this->indentMode, $amount);
157
    }
158
159
}
160