1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file was originally 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 UnicornFail\Emoji\Input; |
15
|
|
|
|
16
|
|
|
use UnicornFail\Emoji\Exception\UnexpectedEncodingException; |
17
|
|
|
|
18
|
|
|
class Input implements InputInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var ?iterable<int, string> |
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 |
36
|
|
|
* |
37
|
|
|
* @psalm-readonly-allow-private-mutation |
38
|
|
|
*/ |
39
|
|
|
private $lineCount; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var int |
43
|
|
|
* |
44
|
|
|
* @psalm-readonly |
45
|
|
|
*/ |
46
|
|
|
private $lineOffset; |
47
|
|
|
|
48
|
|
|
public function __construct(string $content, int $lineOffset = 0) |
49
|
|
|
{ |
50
|
|
|
if (! \mb_check_encoding($content, 'UTF-8')) { |
51
|
|
|
throw new UnexpectedEncodingException('Unexpected encoding - UTF-8 or ASCII was expected'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->content = $content; |
55
|
|
|
$this->lineOffset = $lineOffset; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getContent(): string |
59
|
|
|
{ |
60
|
|
|
return $this->content; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function getLines(): iterable |
67
|
|
|
{ |
68
|
|
|
$this->splitLinesIfNeeded(); |
69
|
|
|
|
70
|
|
|
foreach ($this->lines ?? [] as $i => $line) { |
71
|
|
|
yield $this->lineOffset + $i + 1 => $line; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function getLineCount(): int |
76
|
|
|
{ |
77
|
|
|
$this->splitLinesIfNeeded(); |
78
|
|
|
|
79
|
|
|
\assert($this->lineCount !== null); |
80
|
|
|
|
81
|
|
|
return $this->lineCount; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function splitLinesIfNeeded(): void |
85
|
|
|
{ |
86
|
|
|
if ($this->lines !== null) { |
87
|
|
|
return; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$lines = \preg_split('/\r\n|\n|\r/', $this->content); |
91
|
|
|
if ($lines === false) { |
92
|
|
|
throw new UnexpectedEncodingException('Failed to split content by line'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// Remove any newline which appears at the very end of the string. |
96
|
|
|
// We've already split the document by newlines, so we can simply drop |
97
|
|
|
// any empty element which appears on the end. |
98
|
|
|
if (\end($lines) === '') { |
99
|
|
|
\array_pop($lines); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$this->lines = $lines; |
103
|
|
|
|
104
|
|
|
$this->lineCount = \count($this->lines); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|