Completed
Push — master ( 5184b6...ce2e43 )
by Colin
11s
created

Context::getEncoding()   A

Complexity

Conditions 1
Paths 1

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 4
Code Lines 2

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 *
8
 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
9
 *  - (c) John MacFarlane
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace League\CommonMark;
16
17
use League\CommonMark\Block\Element\AbstractBlock;
18
use League\CommonMark\Block\Element\Document;
19
20
/**
21
 * Parses Markdown into an AST
22
 */
23
class Context implements ContextInterface
24
{
25
    /**
26
     * @var Environment
27
     */
28
    protected $environment;
29
30
    /**
31
     * @var AbstractBlock
32
     */
33
    protected $doc;
34
35
    /**
36
     * @var AbstractBlock|null
37
     */
38
    protected $tip;
39
40
    /**
41
     * @var AbstractBlock
42
     */
43
    protected $container;
44
45
    /**
46
     * @var int
47
     */
48
    protected $lineNumber;
49
50
    /**
51
     * @var string
52
     */
53
    protected $line;
54
55
    /**
56
     * @var UnmatchedBlockCloser
57
     */
58
    protected $blockCloser;
59
60
    /**
61
     * @var bool
62
     */
63
    protected $blocksParsed = false;
64
65
    protected $encoding = 'UTF-8';
66
67
    protected $referenceParser;
68
69 1950
    public function __construct(Document $document, Environment $environment)
70
    {
71 1950
        $this->doc = $document;
72 1950
        $this->tip = $this->doc;
73 1950
        $this->container = $this->doc;
74
75 1950
        $this->environment = $environment;
76
77 1950
        $this->referenceParser = new ReferenceParser($document->getReferenceMap());
78
79 1950
        $this->blockCloser = new UnmatchedBlockCloser($this);
80 1950
    }
81
82
    /**
83
     * @param string $line
84
     */
85 1938
    public function setNextLine($line)
86
    {
87 1938
        ++$this->lineNumber;
88 1938
        $this->line = $line;
89 1938
    }
90
91
    /**
92
     * @return Document
93
     */
94 1941
    public function getDocument()
95
    {
96 1941
        return $this->doc;
97
    }
98
99
    /**
100
     * @return AbstractBlock|null
101
     */
102 1950
    public function getTip()
103
    {
104 1950
        return $this->tip;
105
    }
106
107
    /**
108
     * @param AbstractBlock|null $block
109
     *
110
     * @return $this
111
     */
112 1950
    public function setTip(AbstractBlock $block = null)
113
    {
114 1950
        $this->tip = $block;
115
116 1950
        return $this;
117
    }
118
119
    /**
120
     * @return int
121
     */
122 1938
    public function getLineNumber()
123
    {
124 1938
        return $this->lineNumber;
125
    }
126
127
    /**
128
     * @return string
129
     */
130 1938
    public function getLine()
131
    {
132 1938
        return $this->line;
133
    }
134
135
    /**
136
     * Finalize and close any unmatched blocks
137
     *
138
     * @return UnmatchedBlockCloser
139
     */
140 1938
    public function getBlockCloser()
141
    {
142 1938
        return $this->blockCloser;
143
    }
144
145
    /**
146
     * @return AbstractBlock
147
     */
148 1938
    public function getContainer()
149
    {
150 1938
        return $this->container;
151
    }
152
153
    /**
154
     * @param AbstractBlock $container
155
     *
156
     * @return $this
157
     */
158 1938
    public function setContainer($container)
159
    {
160 1938
        $this->container = $container;
161
162 1938
        return $this;
163
    }
164
165
    /**
166
     * @param AbstractBlock $block
167
     *
168
     * @return AbstractBlock
169
     */
170 1938
    public function addBlock(AbstractBlock $block)
171
    {
172 1938
        $this->getBlockCloser()->closeUnmatchedBlocks();
173 1938
        $block->setStartLine($this->lineNumber);
174 1938
        while (!$this->tip->canContain($block)) {
175 111
            $this->tip->finalize($this, $this->lineNumber);
176 37
        }
177
178 1938
        $this->tip->appendChild($block);
179 1938
        $this->tip = $block;
180 1938
        $this->container = $block;
181
182 1938
        return $block;
183
    }
184
185
    /**
186
     * @param AbstractBlock $replacement
187
     */
188 51
    public function replaceContainerBlock(AbstractBlock $replacement)
189
    {
190 51
        $this->getBlockCloser()->closeUnmatchedBlocks();
191 51
        $this->getContainer()->replaceWith($replacement);
192
193 51
        if ($this->getTip() === $this->getContainer()) {
194 51
            $this->setTip($replacement);
195 17
        }
196
197 51
        $this->setContainer($replacement);
198 51
    }
199
200
    /**
201
     * @return bool
202
     */
203 1938
    public function getBlocksParsed()
204
    {
205 1938
        return $this->blocksParsed;
206
    }
207
208
    /**
209
     * @param bool $bool
210
     *
211
     * @return $this
212
     */
213 1938
    public function setBlocksParsed($bool)
214
    {
215 1938
        $this->blocksParsed = $bool;
216
217 1938
        return $this;
218
    }
219
220
    /**
221
     * @return ReferenceParser
222
     */
223 390
    public function getReferenceParser()
224
    {
225 390
        return $this->referenceParser;
226
    }
227
228
    /**
229
     * @return string
230
     */
231 1938
    public function getEncoding()
232
    {
233 1938
        return $this->encoding;
234
    }
235
236
    /**
237
     * @param string $encoding
238
     *
239
     * @return $this
240
     */
241 1941
    public function setEncoding($encoding)
242
    {
243 1941
        $this->encoding = $encoding;
244
245 1941
        return $this;
246
    }
247
}
248