This commit is contained in:
Paolo A
2024-08-13 13:44:16 +00:00
parent 1bbb23088d
commit e796d76612
4001 changed files with 30101 additions and 40075 deletions

View File

@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
/*
* This file is part of the league/commonmark package.
*
@@ -13,24 +15,37 @@ namespace League\CommonMark\Input;
use League\CommonMark\Exception\UnexpectedEncodingException;
final class MarkdownInput implements MarkdownInputInterface
class MarkdownInput implements MarkdownInputInterface
{
/** @var array<int, string>|null */
private $lines;
/**
* @var array<int, string>|null
*
* @psalm-readonly-allow-private-mutation
*/
private ?array $lines = null;
/** @var string */
private $content;
/** @psalm-readonly-allow-private-mutation */
private string $content;
/** @var int|null */
private $lineCount;
/** @psalm-readonly-allow-private-mutation */
private ?int $lineCount = null;
public function __construct(string $content)
/** @psalm-readonly */
private int $lineOffset;
public function __construct(string $content, int $lineOffset = 0)
{
if (!\mb_check_encoding($content, 'UTF-8')) {
if (! \mb_check_encoding($content, 'UTF-8')) {
throw new UnexpectedEncodingException('Unexpected encoding - UTF-8 or ASCII was expected');
}
$this->content = $content;
// Strip any leading UTF-8 BOM
if (\substr($content, 0, 3) === "\xEF\xBB\xBF") {
$content = \substr($content, 3);
}
$this->content = $content;
$this->lineOffset = $lineOffset;
}
public function getContent(): string
@@ -39,14 +54,17 @@ final class MarkdownInput implements MarkdownInputInterface
}
/**
* @return \Traversable<int, string>
* {@inheritDoc}
*/
public function getLines(): \Traversable
public function getLines(): iterable
{
$this->splitLinesIfNeeded();
foreach ($this->lines as $lineNumber => $line) {
yield $lineNumber => $line;
\assert($this->lines !== null);
/** @psalm-suppress PossiblyNullIterator */
foreach ($this->lines as $i => $line) {
yield $this->lineOffset + $i + 1 => $line;
}
}
@@ -54,6 +72,8 @@ final class MarkdownInput implements MarkdownInputInterface
{
$this->splitLinesIfNeeded();
\assert($this->lineCount !== null);
return $this->lineCount;
}