Completed
Push — front-matter-extension ( d11a67...28a551 )
by Colin
02:09
created

MarkdownInput::getLineOffset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

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
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace League\CommonMark\Input;
15
16
use League\CommonMark\Exception\UnexpectedEncodingException;
17
18
final class MarkdownInput implements MarkdownInputInterface
19
{
20
    /**
21
     * @var iterable<int, string>|null
22
     *
23
     * @psalm-readonly-allow-private-mutation
24
     */
25
    private $lines;
26
27
    /**
28
     * @var string
29
     *
30
     * @psalm-readonly-allow-private-mutation
31
     */
32
    private $content;
33
34
    /**
35
     * @var int|null
36
     *
37
     * @psalm-readonly-allow-private-mutation
38
     */
39
    private $lineCount;
40
41
    /**
42
     * @var int
43
     *
44
     * @psalm-readonly
45
     */
46
    private $lineOffset = 0;
47
48 2838
    public function __construct(string $content, int $lineOffset = 0)
49
    {
50 2838
        if (! \mb_check_encoding($content, 'UTF-8')) {
51 9
            throw new UnexpectedEncodingException('Unexpected encoding - UTF-8 or ASCII was expected');
52
        }
53
54 2829
        $this->content    = $content;
55 2829
        $this->lineOffset = $lineOffset;
56 2829
    }
57
58 48
    public function getContent(): string
59
    {
60 48
        return $this->content;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 2781
    public function getLines(): iterable
67
    {
68 2781
        $this->splitLinesIfNeeded();
69
70
        /** @psalm-suppress PossiblyNullIterator */
71 2781
        foreach ($this->lines as $i => $line) {
72 2781
            yield $this->lineOffset + $i + 1 => $line;
73
        }
74 2781
    }
75
76 3
    public function getLineCount(): int
77
    {
78 3
        $this->splitLinesIfNeeded();
79
80
        \assert($this->lineCount !== null);
81
82 3
        return $this->lineCount;
83
    }
84
85 36
    public function getLineOffset(): int
86
    {
87 36
        return $this->lineOffset;
88
    }
89
90 2784
    private function splitLinesIfNeeded(): void
91
    {
92 2784
        if ($this->lines !== null) {
93
            return;
94
        }
95
96 2784
        $lines = \preg_split('/\r\n|\n|\r/', $this->content);
97 2784
        if ($lines === false) {
98
            throw new UnexpectedEncodingException('Failed to split Markdown content by line');
99
        }
100
101 2784
        $this->lines = $lines;
102
103
        // Remove any newline which appears at the very end of the string.
104
        // We've already split the document by newlines, so we can simply drop
105
        // any empty element which appears on the end.
106 2784
        if (\end($this->lines) === '') {
107 2295
            \array_pop($this->lines);
108
        }
109
110 2784
        $this->lineCount = \count($this->lines);
111 2784
    }
112
}
113