|
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\Extension\CommonMark\Parser\Block; |
|
13
|
|
|
|
|
14
|
|
|
use League\CommonMark\Extension\CommonMark\Node\Block\ListBlock; |
|
15
|
|
|
use League\CommonMark\Extension\CommonMark\Node\Block\ListData; |
|
16
|
|
|
use League\CommonMark\Extension\CommonMark\Node\Block\ListItem; |
|
17
|
|
|
use League\CommonMark\Node\Block\AbstractBlock; |
|
18
|
|
|
use League\CommonMark\Node\Block\Paragraph; |
|
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
|
|
|
|
|
24
|
|
|
final class ListItemParser extends AbstractBlockContinueParser |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var ListItem */ |
|
27
|
|
|
private $block; |
|
28
|
|
|
|
|
29
|
|
|
/** @var bool */ |
|
30
|
|
|
private $hadBlankLine = false; |
|
31
|
|
|
|
|
32
|
282 |
|
public function __construct(ListData $listData) |
|
33
|
|
|
{ |
|
34
|
282 |
|
$this->block = new ListItem($listData); |
|
35
|
282 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return ListItem |
|
39
|
|
|
*/ |
|
40
|
282 |
|
public function getBlock(): AbstractBlock |
|
41
|
|
|
{ |
|
42
|
282 |
|
return $this->block; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
264 |
|
public function isContainer(): bool |
|
46
|
|
|
{ |
|
47
|
264 |
|
return true; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
258 |
|
public function canContain(AbstractBlock $childBlock): bool |
|
51
|
|
|
{ |
|
52
|
258 |
|
if ($this->hadBlankLine) { |
|
53
|
|
|
// We saw a blank line in this list item, that means the list block is loose. |
|
54
|
|
|
// |
|
55
|
|
|
// spec: if any of its constituent list items directly contain two block-level elements with a blank line |
|
56
|
|
|
// between them |
|
57
|
84 |
|
$parent = $this->block->parent(); |
|
58
|
84 |
|
if ($parent instanceof ListBlock) { |
|
59
|
84 |
|
$parent->setTight(false); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
258 |
|
return true; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
222 |
|
public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): ?BlockContinue |
|
67
|
|
|
{ |
|
68
|
222 |
|
if ($cursor->isBlank()) { |
|
69
|
129 |
|
if ($this->block->firstChild() === null) { |
|
70
|
|
|
// Blank line after empty list item |
|
71
|
6 |
|
return BlockContinue::none(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
123 |
|
$activeBlock = $activeBlockParser->getBlock(); |
|
75
|
|
|
// If the active block is a code block, blank lines in it should not affect if the list is tight. |
|
76
|
123 |
|
$this->hadBlankLine = $activeBlock instanceof Paragraph || $activeBlock instanceof ListItem; |
|
77
|
123 |
|
$cursor->advanceToNextNonSpaceOrTab(); |
|
78
|
|
|
|
|
79
|
123 |
|
return BlockContinue::at($cursor); |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
219 |
|
$contentIndent = $this->block->getListData()->markerOffset + $this->getBlock()->getListData()->padding; |
|
83
|
219 |
|
if ($cursor->getIndent() >= $contentIndent) { |
|
84
|
126 |
|
$cursor->advanceBy($contentIndent, true); |
|
85
|
|
|
|
|
86
|
126 |
|
return BlockContinue::at($cursor); |
|
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
138 |
|
return BlockContinue::none(); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
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_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.