1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the league/commonmark package. |
5
|
|
|
* |
6
|
|
|
* (c) Colin O'Dell <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace League\CommonMark\Input; |
13
|
|
|
|
14
|
|
|
use League\CommonMark\Exception\UnexpectedEncodingException; |
15
|
|
|
|
16
|
|
|
final class MarkdownInput implements MarkdownInputInterface |
17
|
|
|
{ |
18
|
|
|
/** @var iterable<int, string>|null */ |
19
|
|
|
private $lines; |
20
|
|
|
|
21
|
|
|
/** @var string */ |
22
|
|
|
private $content; |
23
|
|
|
|
24
|
|
|
/** @var int|null */ |
25
|
|
|
private $lineCount; |
26
|
|
|
|
27
|
2514 |
|
public function __construct(string $content) |
28
|
|
|
{ |
29
|
2514 |
|
if (!\mb_check_encoding($content, 'UTF-8')) { |
30
|
9 |
|
throw new UnexpectedEncodingException('Unexpected encoding - UTF-8 or ASCII was expected'); |
31
|
|
|
} |
32
|
|
|
|
33
|
2505 |
|
$this->content = $content; |
34
|
2505 |
|
} |
35
|
|
|
|
36
|
3 |
|
public function getContent(): string |
37
|
|
|
{ |
38
|
3 |
|
return $this->content; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return \Traversable<int, string> |
|
|
|
|
43
|
|
|
*/ |
44
|
2496 |
|
public function getLines(): \Traversable |
45
|
|
|
{ |
46
|
2496 |
|
$this->splitLinesIfNeeded(); |
47
|
|
|
|
48
|
2496 |
|
foreach ($this->lines as $lineNumber => $line) { |
49
|
2496 |
|
yield $lineNumber => $line; |
50
|
|
|
} |
51
|
2496 |
|
} |
52
|
|
|
|
53
|
3 |
|
public function getLineCount(): int |
54
|
|
|
{ |
55
|
3 |
|
$this->splitLinesIfNeeded(); |
56
|
|
|
|
57
|
3 |
|
return $this->lineCount; |
58
|
|
|
} |
59
|
|
|
|
60
|
2499 |
|
private function splitLinesIfNeeded(): void |
61
|
|
|
{ |
62
|
2499 |
|
if ($this->lines !== null) { |
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
|
66
|
2499 |
|
$lines = \preg_split('/\r\n|\n|\r/', $this->content); |
67
|
2499 |
|
if ($lines === false) { |
68
|
|
|
throw new UnexpectedEncodingException('Failed to split Markdown content by line'); |
69
|
|
|
} |
70
|
|
|
|
71
|
2499 |
|
$this->lines = $lines; |
72
|
|
|
|
73
|
|
|
// Remove any newline which appears at the very end of the string. |
74
|
|
|
// We've already split the document by newlines, so we can simply drop |
75
|
|
|
// any empty element which appears on the end. |
76
|
2499 |
|
if (\end($this->lines) === '') { |
77
|
2226 |
|
\array_pop($this->lines); |
78
|
|
|
} |
79
|
|
|
|
80
|
2499 |
|
$this->lineCount = \count($this->lines); |
81
|
2499 |
|
} |
82
|
|
|
} |
83
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.