Completed
Push — master ( 1a3dd2...a893e4 )
by Julius
02:37
created

RstBuilder::addFieldList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Härtl <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 *  This program is free software: you can redistribute it and/or modify
10
 *  it under the terms of the GNU Affero General Public License as
11
 *  published by the Free Software Foundation, either version 3 of the
12
 *  License, or (at your option) any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU Affero General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU Affero General Public License
20
 *  along with this program. If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace JuliusHaertl\PHPDocToRst\Builder;
25
26
27
use phpDocumentor\Reflection\Fqsen;
28
29
/**
30
 * Helper class to build reStructuredText files
31
 *
32
 * @package JuliusHaertl\PHPDocToRst\Builder
33
 */
34
class RstBuilder {
35
36
    private $indentLevel = 0;
37
    /** @var string */
38
    protected $content = '.. rst-class:: phpdoctorst' . PHP_EOL . PHP_EOL ;
39
40
    public function getContent() {
41
        return $this->content;
42
    }
43
44
    public static function escape($text) {
45
        // escape all common reStructuredText control chars
46
        $text = preg_replace("/[\'\`\*\_\{\}\[\]\(\)\>\#\+\-\.\\\!]/", '\\\\$0', $text);
47
        return $text;
48
    }
49
50
    public function indent() {
51
        $this->indentLevel++;
52
        return $this;
53
    }
54
55
    public function unindent() {
56
        $this->indentLevel--;
57
        return $this;
58
    }
59
60
    public function addFieldList($key, $value) {
61
        $this->addLine(':'.self::escape($key).':');
62
        $this->indent()->addMultiline($value, true)->unindent();
63
        return $this;
64
    }
65
66
    public function addH1($text) {
67
        $this->addLine($text);
68
        $this->addLine(str_repeat('=', strlen((string)$text)))->addLine();
69
        return $this;
70
    }
71
72
    public function addH2($text) {
73
        $this->addLine($text);
74
        $this->addLine(str_repeat('-', strlen((string)$text)))->addLine();
75
        return $this;
76
    }
77
78
    public function addLine($text = '') {
79
        $this->add(str_repeat("\t", $this->indentLevel) . $text . PHP_EOL);
80
        return $this;
81
    }
82
83
    public function addMultiline($text = '', $blockIndent = false) {
84
        $lines = preg_split('/$\R?^/m', $text);
85
        $i = 0;
86
        foreach ($lines as $line) {
87
            if ($blockIndent && $i++ === 1) {
88
                $this->indent();
89
            }
90
            $this->addLine($line);
91
        }
92
        if ($blockIndent && sizeof($lines) > 1) {
93
            $this->unindent();
94
        }
95
        return $this;
96
    }
97
98
    public function add($text) {
99
        $this->content .= $text;
100
        return $this;
101
    }
102
103
104
}