Completed
Push — master ( 55b21a...aec22b )
by Thomas
06:20
created

Writer::indent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * Copyright 2011 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
namespace gossi\codegen\generator\utils;
19
20
/**
21
 * A writer implementation.
22
 *
23
 * This may be used to simplify writing well-formatted code.
24
 *
25
 * @author Johannes M. Schmitt <[email protected]>
26
 */
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 40
	public function __construct($options = []) {
39 40
		$this->options = array_merge($this->options, $options);
40
41 40
		$this->indentation = str_repeat($this->options['indentation_character'], 
42 40
			$this->options['indentation_size']);
43 40
	}
44
45 26
	public function indent() {
46 26
		$this->indentationLevel += 1;
47
48 26
		return $this;
49
	}
50
51 25
	public function outdent() {
52 25
		$this->indentationLevel = max($this->indentationLevel - 1, 0);
53
54 25
		return $this;
55
	}
56
57
	/**
58
	 *
59
	 * @param string $content        	
60
	 */
61 33
	public function writeln($content = '') {
62 33
		$this->write($content . "\n");
63
64 33
		return $this;
65
	}
66
67
	/**
68
	 *
69
	 * @param string $content        	
70
	 */
71 38
	public function write($content) {
72 38
		$lines = explode("\n", $content);
73 38
		for ($i = 0, $c = count($lines); $i < $c; $i++) {
74 38
			if ($this->indentationLevel > 0
75 38
					&& !empty($lines[$i])
76 38
					&& (empty($this->content) || "\n" === substr($this->content, -1))) {
77 5
				$this->content .= str_repeat($this->indentation, $this->indentationLevel);
78
			}
79
80 38
			$this->content .= $lines[$i];
81
82 38
			if ($i + 1 < $c) {
83 33
				$this->content .= "\n";
84
			}
85
		}
86
87 38
		return $this;
88
	}
89
90 32
	public function rtrim() {
91 32
		$addNl = "\n" === substr($this->content, -1);
92 32
		$this->content = rtrim($this->content);
93
94 32
		if ($addNl) {
95 25
			$this->content .= "\n";
96
		}
97
98 32
		return $this;
99
	}
100
101 32
	public function endsWith($search) {
102 32
		return substr($this->content, -strlen($search)) === $search;
103
	}
104
105 37
	public function reset() {
106 37
		$this->content = '';
107 37
		$this->indentationLevel = 0;
108
109 37
		return $this;
110
	}
111
112 38
	public function getContent() {
113 38
		return $this->content;
114
	}
115
}
116