Test Failed
Push — master ( a1e735...534e7d )
by Bálint
13:42 queued 13s
created

IndentedTextWriter::getResult()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace POData\Writers\Json;
4
5
/**
6
 * Class IndentedTextWriter
7
 * @package POData\Writers\Json
8
 */
9
class IndentedTextWriter
10
{
11
    /**
12
     * writer to which Json text needs to be written
13
     * @var string
14
     */
15
    private $result;
16
17
    /**
18
     * keeps track of the indentLevel
19
     * @var int
20
     */
21
    private $indentLevel;
22
23
    /**
24
     * keeps track of pending tabs
25
     * @var bool
26
     */
27
    private $tabsPending;
28
29
    /**
30
     * string representation of tab
31
     * @var string
32
     */
33
    private $tabString;
34
35
    /**
36
     * Creates a new instance of IndentedTextWriter
37
     *
38
     * @param string $writer writer which IndentedTextWriter wraps
39
     */
40
    public function __construct($writer)
41
    {
42
        $this->_writer = $writer;
0 ignored issues
show
Bug Best Practice introduced by
The property _writer does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
43
        $this->tabString = "\t";
44
        $this->clear();
45
    }
46
47
    /**
48
     * Writes the given string value to the underlying writer
49
     *
50
     * @param string $value string, char, text value to be written
51
     *
52
     * @return IndentedTextWriter
53
     */
54
    public function writeValue($value)
55
    {
56
        $this->outputTabs();
57
        $this->_write($value);
58
        return $this;
59
    }
60
61
    /**
62
     * Writes a new line character to the text stream
63
     *
64
     * @return IndentedTextWriter
65
     */
66
    public function writeLine()
67
    {
68
        $this->_write("\n");
69
        $this->tabsPending = true;
70
        return $this;
71
    }
72
73
    /**
74
     * Writes the given text trimmed with no indentation
75
     *
76
     * @param string $value text to be written
77
     *
78
     * @return IndentedTextWriter
79
     */
80
    public function writeTrimmed($value)
81
    {
82
        $this->_write(trim($value));
83
        return $this;
84
    }
85
86
    /**
87
     * Increases the current indent setting by 1
88
     * @return IndentedTextWriter
89
     */
90
    public function increaseIndent()
91
    {
92
        $this->indentLevel++;
93
        return $this;
94
    }
95
96
    /**
97
     * Decreases the current indent setting by 1, never going below 0
98
     * @return IndentedTextWriter
99
     */
100
    public function decreaseIndent()
101
    {
102
        if ($this->indentLevel > 0) {
103
            $this->indentLevel--;
104
        }
105
        return $this;
106
    }
107
108
109
    /**
110
     * @return string the current written text
111
     */
112
    public function getResult()
113
    {
114
        return $this->result;
115
    }
116
117
    public function clear()
118
    {
119
        $this->result = $this->_writer;
120
    }
121
122
    /**
123
     * Writes the tabs depending on the indent level
124
     *
125
     * @return void
126
     */
127
    private function outputTabs()
128
    {
129
        if ($this->tabsPending) {
130
            $this->_write(str_repeat($this->tabString, $this->indentLevel));
131
            $this->tabsPending = false;
132
        }
133
    }
134
135
    /**
136
     * Writes the value to the text stream
137
     *
138
     * @param string $value value to be written
139
     *
140
     * @return void
141
     */
142
    private function _write($value)
143
    {
144
        $this->result .= $value;
145
    }
146
147
148
}
149