|
1
|
|
|
<?php |
|
2
|
|
|
namespace gossi\formatter\utils; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* A writer implementation. |
|
6
|
|
|
* |
|
7
|
|
|
* This may be used to simplify writing well-formatted code. |
|
8
|
|
|
* |
|
9
|
|
|
*/ |
|
10
|
|
|
class Writer { |
|
11
|
|
|
|
|
12
|
|
|
private $content = ''; |
|
13
|
|
|
private $indentationLevel = 0; |
|
14
|
|
|
private $indentation; |
|
15
|
|
|
|
|
16
|
|
|
private $options = [ |
|
17
|
|
|
'indentation_character' => "\t", |
|
18
|
|
|
'indentation_size' => 1 |
|
19
|
|
|
]; |
|
20
|
|
|
|
|
21
|
2 |
|
public function __construct($options = []) { |
|
22
|
2 |
|
$this->options = array_merge($this->options, $options); |
|
23
|
|
|
|
|
24
|
2 |
|
$this->indentation = str_repeat($this->options['indentation_character'], |
|
25
|
2 |
|
$this->options['indentation_size']); |
|
26
|
2 |
|
} |
|
27
|
|
|
|
|
28
|
2 |
|
public function indent() { |
|
29
|
2 |
|
$this->indentationLevel += 1; |
|
30
|
|
|
|
|
31
|
2 |
|
return $this; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
2 |
|
public function outdent() { |
|
35
|
2 |
|
$this->indentationLevel -= 1; |
|
36
|
|
|
|
|
37
|
2 |
|
if ($this->indentationLevel < 0) { |
|
38
|
|
|
throw new \RuntimeException('The identation level cannot be less than zero.'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
2 |
|
return $this; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $content |
|
47
|
|
|
*/ |
|
48
|
2 |
|
public function writeln($content = '') { |
|
49
|
2 |
|
$this->write($content . "\n"); |
|
50
|
|
|
|
|
51
|
2 |
|
return $this; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $content |
|
57
|
|
|
*/ |
|
58
|
2 |
|
public function write($content) { |
|
59
|
2 |
|
$lines = explode("\n", $content); |
|
60
|
2 |
|
for ($i = 0, $c = count($lines); $i < $c; $i ++) { |
|
61
|
2 |
|
if ($this->indentationLevel > 0 |
|
62
|
2 |
|
&& !empty($lines[$i]) |
|
63
|
2 |
|
&& (empty($this->content) || "\n" === substr($this->content, -1))) { |
|
64
|
2 |
|
$this->content .= str_repeat($this->indentation, $this->indentationLevel); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
2 |
|
$this->content .= $lines[$i]; |
|
68
|
|
|
|
|
69
|
2 |
|
if ($i + 1 < $c) { |
|
70
|
2 |
|
$this->content .= "\n"; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
2 |
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function rtrim() { |
|
78
|
|
|
$addNl = "\n" === substr($this->content, -1); |
|
79
|
|
|
$this->content = rtrim($this->content); |
|
80
|
|
|
|
|
81
|
|
|
if ($addNl) { |
|
82
|
|
|
$this->content .= "\n"; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function endsWith($search) { |
|
89
|
|
|
return substr($this->content, -strlen($search)) === $search; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function reset() { |
|
93
|
|
|
$this->content = ''; |
|
94
|
|
|
$this->indentationLevel = 0; |
|
95
|
|
|
|
|
96
|
|
|
return $this; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
2 |
|
public function getContent() { |
|
100
|
2 |
|
return $this->content; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|