1 | <?php |
||
27 | class Writer { |
||
28 | |||
29 | private $content = ''; |
||
30 | private $indentationLevel = 0; |
||
31 | private $indentation; |
||
32 | |||
33 | private $options = [ |
||
34 | 'indentation_character' => "\t", |
||
35 | 'indentation_size' => 1 |
||
36 | ]; |
||
37 | |||
38 | 23 | public function __construct($options = []) { |
|
44 | |||
45 | 22 | public function indent() { |
|
50 | |||
51 | 21 | public function outdent() { |
|
52 | 21 | $this->indentationLevel -= 1; |
|
53 | |||
54 | 21 | if ($this->indentationLevel < 0) { |
|
55 | throw new \RuntimeException('The identation level cannot be less than zero.'); |
||
56 | } |
||
57 | |||
58 | 21 | return $this; |
|
59 | } |
||
60 | |||
61 | /** |
||
62 | * |
||
63 | * @param string $content |
||
64 | */ |
||
65 | 23 | public function writeln($content = '') { |
|
70 | |||
71 | /** |
||
72 | * |
||
73 | * @param string $content |
||
74 | */ |
||
75 | 23 | public function write($content) { |
|
76 | |||
77 | 23 | if (is_string($content)) { |
|
78 | 23 | $lines = explode("\n", $content); |
|
79 | 23 | for ($i = 0, $c = count($lines); $i < $c; $i++) { |
|
80 | 23 | if ($this->indentationLevel > 0 |
|
81 | 23 | && !empty($lines[$i]) |
|
82 | 23 | && (empty($this->content) || "\n" === substr($this->content, -1))) { |
|
83 | 10 | $this->content .= str_repeat($this->indentation, $this->indentationLevel); |
|
84 | } |
||
85 | |||
86 | 23 | $this->content .= $lines[$i]; |
|
87 | |||
88 | 23 | if ($i + 1 < $c) { |
|
89 | 23 | $this->content .= "\n"; |
|
90 | } |
||
91 | } |
||
92 | } elseif(is_null($content)) { |
||
93 | $this->content .= 'null'; |
||
94 | } |
||
95 | |||
96 | 23 | return $this; |
|
97 | } |
||
98 | |||
99 | 21 | public function rtrim() { |
|
100 | 21 | $addNl = "\n" === substr($this->content, -1); |
|
101 | 21 | $this->content = rtrim($this->content); |
|
102 | |||
103 | 21 | if ($addNl) { |
|
104 | 21 | $this->content .= "\n"; |
|
105 | } |
||
106 | |||
107 | 21 | return $this; |
|
108 | } |
||
109 | |||
110 | 5 | public function endsWith($search) { |
|
113 | |||
114 | 18 | public function reset() { |
|
120 | |||
121 | 23 | public function getContent() { |
|
124 | } |
||
125 |