|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Copyright 2016 Johannes M. Schmitt <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace JMS\Serializer\Util; |
|
20
|
|
|
|
|
21
|
|
|
use JMS\Serializer\Exception\RuntimeException; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* A writer implementation. |
|
25
|
|
|
* |
|
26
|
|
|
* This may be used to simplify writing well-formatted code. |
|
27
|
|
|
* |
|
28
|
|
|
* @author Johannes M. Schmitt <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
class Writer |
|
31
|
|
|
{ |
|
32
|
|
|
public $indentationSpaces = 4; |
|
33
|
|
|
public $indentationLevel = 0; |
|
34
|
|
|
public $content = ''; |
|
35
|
|
|
public $changeCount = 0; |
|
36
|
|
|
|
|
37
|
|
|
private $changes = array(); |
|
38
|
|
|
|
|
39
|
72 |
|
public function indent() |
|
40
|
|
|
{ |
|
41
|
72 |
|
$this->indentationLevel += 1; |
|
42
|
|
|
|
|
43
|
72 |
|
return $this; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
72 |
|
public function outdent() |
|
47
|
|
|
{ |
|
48
|
72 |
|
$this->indentationLevel -= 1; |
|
49
|
|
|
|
|
50
|
72 |
|
if ($this->indentationLevel < 0) { |
|
51
|
|
|
throw new RuntimeException('The identation level cannot be less than zero.'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
72 |
|
return $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param string $content |
|
59
|
|
|
* |
|
60
|
|
|
* @return Writer |
|
61
|
|
|
*/ |
|
62
|
95 |
|
public function writeln($content) |
|
63
|
|
|
{ |
|
64
|
95 |
|
$this->write($content . "\n"); |
|
65
|
|
|
|
|
66
|
95 |
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
2 |
|
public function revert() |
|
70
|
|
|
{ |
|
71
|
2 |
|
$change = array_pop($this->changes); |
|
72
|
2 |
|
$this->changeCount -= 1; |
|
73
|
2 |
|
$this->content = substr($this->content, 0, -1 * strlen($change)); |
|
74
|
2 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param string $content |
|
78
|
|
|
* |
|
79
|
|
|
* @return Writer |
|
80
|
|
|
*/ |
|
81
|
95 |
|
public function write($content) |
|
82
|
|
|
{ |
|
83
|
95 |
|
$addition = ''; |
|
84
|
|
|
|
|
85
|
95 |
|
$lines = explode("\n", $content); |
|
86
|
95 |
|
for ($i = 0, $c = count($lines); $i < $c; $i++) { |
|
87
|
95 |
|
if ($this->indentationLevel > 0 |
|
88
|
95 |
|
&& !empty($lines[$i]) |
|
89
|
95 |
|
&& ((empty($addition) && "\n" === substr($this->content, -1)) || "\n" === substr($addition, -1)) |
|
90
|
95 |
|
) { |
|
91
|
20 |
|
$addition .= str_repeat(' ', $this->indentationLevel * $this->indentationSpaces); |
|
92
|
20 |
|
} |
|
93
|
|
|
|
|
94
|
95 |
|
$addition .= $lines[$i]; |
|
95
|
|
|
|
|
96
|
95 |
|
if ($i + 1 < $c) { |
|
97
|
95 |
|
$addition .= "\n"; |
|
98
|
95 |
|
} |
|
99
|
95 |
|
} |
|
100
|
|
|
|
|
101
|
95 |
|
$this->content .= $addition; |
|
102
|
95 |
|
$this->changes[] = $addition; |
|
103
|
95 |
|
$this->changeCount += 1; |
|
104
|
|
|
|
|
105
|
95 |
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
76 |
|
public function rtrim($preserveNewLines = true) |
|
109
|
|
|
{ |
|
110
|
76 |
|
if (!$preserveNewLines) { |
|
111
|
76 |
|
$this->content = rtrim($this->content); |
|
112
|
|
|
|
|
113
|
76 |
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$addNl = "\n" === substr($this->content, -1); |
|
117
|
|
|
$this->content = rtrim($this->content); |
|
118
|
|
|
|
|
119
|
|
|
if ($addNl) { |
|
120
|
|
|
$this->content .= "\n"; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return $this; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
98 |
|
public function reset() |
|
127
|
|
|
{ |
|
128
|
98 |
|
$this->content = ''; |
|
129
|
98 |
|
$this->indentationLevel = 0; |
|
130
|
|
|
|
|
131
|
98 |
|
return $this; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
95 |
|
public function getContent() |
|
135
|
|
|
{ |
|
136
|
95 |
|
return $this->content; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|