1 | <?php |
||
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() { |
|
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 = '') { |
|
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 | 2 | } |
|
66 | |||
67 | 2 | $this->content .= $lines[$i]; |
|
68 | |||
69 | 2 | if ($i + 1 < $c) { |
|
70 | 2 | $this->content .= "\n"; |
|
71 | 2 | } |
|
72 | 2 | } |
|
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) { |
||
91 | |||
92 | public function reset() { |
||
98 | |||
99 | 2 | public function getContent() { |
|
102 | } |
||
103 |