1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This is part of the league/commonmark package. |
7
|
|
|
* |
8
|
|
|
* (c) Martin Hasoň <[email protected]> |
9
|
|
|
* (c) Webuni s.r.o. <[email protected]> |
10
|
|
|
* (c) Colin O'Dell <[email protected]> |
11
|
|
|
* |
12
|
|
|
* For the full copyright and license information, please view the LICENSE |
13
|
|
|
* file that was distributed with this source code. |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace League\CommonMark\Extension\Table; |
17
|
|
|
|
18
|
|
|
use League\CommonMark\Node\Block\AbstractBlock; |
19
|
|
|
use League\CommonMark\Parser\Block\AbstractBlockContinueParser; |
20
|
|
|
use League\CommonMark\Parser\Block\BlockContinue; |
21
|
|
|
use League\CommonMark\Parser\Block\BlockContinueParserInterface; |
22
|
|
|
use League\CommonMark\Parser\Cursor; |
23
|
|
|
use League\CommonMark\Parser\InlineParserEngineInterface; |
24
|
|
|
use League\CommonMark\Util\ArrayCollection; |
25
|
|
|
|
26
|
|
|
final class TableParser extends AbstractBlockContinueParser |
27
|
|
|
{ |
28
|
|
|
/** @var Table */ |
29
|
|
|
private $block; |
30
|
|
|
|
31
|
|
|
/** @var ArrayCollection<int, string> */ |
32
|
|
|
private $bodyLines; |
33
|
|
|
|
34
|
|
|
/** @var array<int, string> */ |
35
|
|
|
private $columns; |
36
|
|
|
|
37
|
|
|
/** @var array<int, string> */ |
38
|
|
|
private $headerCells; |
39
|
|
|
|
40
|
|
|
/** @var bool */ |
41
|
|
|
private $nextIsSeparatorLine = true; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* TableParser constructor. |
45
|
|
|
* |
46
|
|
|
* @param array<int, string> $columns |
47
|
|
|
* @param array<int, string> $headerCells |
48
|
|
|
*/ |
49
|
69 |
|
public function __construct(array $columns, array $headerCells) |
50
|
|
|
{ |
51
|
69 |
|
$this->block = new Table(); |
52
|
69 |
|
$this->bodyLines = new ArrayCollection(); |
53
|
69 |
|
$this->columns = $columns; |
54
|
69 |
|
$this->headerCells = $headerCells; |
55
|
69 |
|
} |
56
|
|
|
|
57
|
12 |
|
public function canHaveLazyContinuationLines(): bool |
58
|
|
|
{ |
59
|
12 |
|
return true; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return Table |
64
|
|
|
*/ |
65
|
69 |
|
public function getBlock(): AbstractBlock |
66
|
|
|
{ |
67
|
69 |
|
return $this->block; |
68
|
|
|
} |
69
|
|
|
|
70
|
66 |
|
public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): ?BlockContinue |
71
|
|
|
{ |
72
|
66 |
|
if (\strpos($cursor->getLine(), '|') === false) { |
73
|
21 |
|
return BlockContinue::none(); |
74
|
|
|
} |
75
|
|
|
|
76
|
66 |
|
return BlockContinue::at($cursor); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
69 |
|
public function addLine(string $line): void |
80
|
|
|
{ |
81
|
69 |
|
if ($this->nextIsSeparatorLine) { |
82
|
69 |
|
$this->nextIsSeparatorLine = false; |
83
|
|
|
} else { |
84
|
66 |
|
$this->bodyLines[] = $line; |
85
|
|
|
} |
86
|
69 |
|
} |
87
|
|
|
|
88
|
69 |
|
public function parseInlines(InlineParserEngineInterface $inlineParser): void |
89
|
|
|
{ |
90
|
69 |
|
$headerColumns = \count($this->headerCells); |
91
|
|
|
|
92
|
69 |
|
$head = new TableSection(TableSection::TYPE_HEAD); |
93
|
69 |
|
$this->block->appendChild($head); |
94
|
|
|
|
95
|
69 |
|
$headerRow = new TableRow(); |
96
|
69 |
|
$head->appendChild($headerRow); |
97
|
69 |
|
for ($i = 0; $i < $headerColumns; $i++) { |
98
|
69 |
|
$cell = $this->headerCells[$i]; |
99
|
69 |
|
$tableCell = self::parseCell($cell, $i, $inlineParser); |
100
|
69 |
|
$tableCell->setType(TableCell::TYPE_HEAD); |
101
|
69 |
|
$headerRow->appendChild($tableCell); |
102
|
|
|
} |
103
|
|
|
|
104
|
69 |
|
$body = null; |
105
|
69 |
|
foreach ($this->bodyLines as $rowLine) { |
106
|
66 |
|
$cells = self::split($rowLine); |
107
|
66 |
|
$row = new TableRow(); |
108
|
|
|
|
109
|
|
|
// Body can not have more columns than head |
110
|
66 |
|
for ($i = 0; $i < $headerColumns; $i++) { |
111
|
66 |
|
$cell = $cells[$i] ?? ''; |
112
|
66 |
|
$tableCell = self::parseCell($cell, $i, $inlineParser); |
113
|
66 |
|
$row->appendChild($tableCell); |
114
|
|
|
} |
115
|
|
|
|
116
|
66 |
|
if ($body === null) { |
117
|
|
|
// It's valid to have a table without body. In that case, don't add an empty TableBody node. |
118
|
66 |
|
$body = new TableSection(); |
119
|
66 |
|
$this->block->appendChild($body); |
120
|
|
|
} |
121
|
66 |
|
$body->appendChild($row); |
122
|
|
|
} |
123
|
69 |
|
} |
124
|
|
|
|
125
|
69 |
|
private function parseCell(string $cell, int $column, InlineParserEngineInterface $inlineParser): TableCell |
126
|
|
|
{ |
127
|
69 |
|
$tableCell = new TableCell(); |
128
|
|
|
|
129
|
69 |
|
if ($column < \count($this->columns)) { |
130
|
69 |
|
$tableCell->setAlign($this->columns[$column]); |
131
|
|
|
} |
132
|
|
|
|
133
|
69 |
|
$inlineParser->parse(\trim($cell), $tableCell); |
134
|
|
|
|
135
|
69 |
|
return $tableCell; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string $line |
140
|
|
|
* |
141
|
|
|
* @return array<int, string> |
|
|
|
|
142
|
|
|
* |
143
|
|
|
* @internal |
144
|
|
|
*/ |
145
|
72 |
|
public static function split(string $line): array |
146
|
|
|
{ |
147
|
72 |
|
$cursor = new Cursor(\trim($line)); |
148
|
|
|
|
149
|
72 |
|
if ($cursor->getCharacter() === '|') { |
150
|
54 |
|
$cursor->advanceBy(1); |
151
|
|
|
} |
152
|
|
|
|
153
|
72 |
|
$cells = []; |
154
|
72 |
|
$sb = ''; |
155
|
|
|
|
156
|
72 |
|
while (!$cursor->isAtEnd()) { |
157
|
72 |
|
switch ($c = $cursor->getCharacter()) { |
158
|
72 |
|
case '\\': |
159
|
12 |
|
if ($cursor->peek() === '|') { |
160
|
|
|
// Pipe is special for table parsing. An escaped pipe doesn't result in a new cell, but is |
161
|
|
|
// passed down to inline parsing as an unescaped pipe. Note that that applies even for the `\|` |
162
|
|
|
// in an input like `\\|` - in other words, table parsing doesn't support escaping backslashes. |
163
|
12 |
|
$sb .= '|'; |
164
|
12 |
|
$cursor->advanceBy(1); |
165
|
|
|
} else { |
166
|
|
|
// Preserve backslash before other characters or at end of line. |
167
|
3 |
|
$sb .= '\\'; |
168
|
|
|
} |
169
|
12 |
|
break; |
170
|
72 |
|
case '|': |
171
|
72 |
|
$cells[] = $sb; |
172
|
72 |
|
$sb = ''; |
173
|
72 |
|
break; |
174
|
|
|
default: |
175
|
72 |
|
$sb .= $c; |
176
|
|
|
} |
177
|
72 |
|
$cursor->advanceBy(1); |
178
|
|
|
} |
179
|
|
|
|
180
|
72 |
|
if ($sb !== '') { |
181
|
33 |
|
$cells[] = $sb; |
182
|
|
|
} |
183
|
|
|
|
184
|
72 |
|
return $cells; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.