|
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\Parser\Block\AbstractBlockContinueParser; |
|
19
|
|
|
use League\CommonMark\Parser\Block\BlockContinue; |
|
20
|
|
|
use League\CommonMark\Parser\Block\BlockContinueParserInterface; |
|
21
|
|
|
use League\CommonMark\Parser\Cursor; |
|
22
|
|
|
|
|
23
|
|
|
final class ListBlockParser extends AbstractBlockContinueParser |
|
24
|
|
|
{ |
|
25
|
|
|
/** @var ListBlock */ |
|
26
|
|
|
private $block; |
|
27
|
|
|
|
|
28
|
|
|
/** @var bool */ |
|
29
|
|
|
private $hadBlankLine = false; |
|
30
|
|
|
|
|
31
|
|
|
/** @var int */ |
|
32
|
|
|
private $linesAfterBlank = 0; |
|
33
|
|
|
|
|
34
|
282 |
|
public function __construct(ListData $listData) |
|
35
|
|
|
{ |
|
36
|
282 |
|
$this->block = new ListBlock($listData); |
|
37
|
282 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return ListBlock |
|
41
|
|
|
*/ |
|
42
|
282 |
|
public function getBlock(): AbstractBlock |
|
43
|
|
|
{ |
|
44
|
282 |
|
return $this->block; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
264 |
|
public function isContainer(): bool |
|
48
|
|
|
{ |
|
49
|
264 |
|
return true; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
264 |
|
public function canContain(AbstractBlock $childBlock): bool |
|
53
|
|
|
{ |
|
54
|
264 |
|
if (!$childBlock instanceof ListItem) { |
|
55
|
57 |
|
return false; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
// Another list item is being added to this list block. |
|
59
|
|
|
// If the previous line was blank, that means this list |
|
60
|
|
|
// block is "loose" (not tight). |
|
61
|
264 |
|
if ($this->hadBlankLine && $this->linesAfterBlank === 1) { |
|
62
|
21 |
|
$this->block->setTight(false); |
|
63
|
21 |
|
$this->hadBlankLine = false; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
264 |
|
return true; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
222 |
|
public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): ?BlockContinue |
|
70
|
|
|
{ |
|
71
|
222 |
|
if ($cursor->isBlank()) { |
|
72
|
129 |
|
$this->hadBlankLine = true; |
|
73
|
129 |
|
$this->linesAfterBlank = 0; |
|
74
|
222 |
|
} elseif ($this->hadBlankLine) { |
|
75
|
129 |
|
$this->linesAfterBlank++; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
// List blocks themselves don't have any markers, only list items. So try to stay in the list. |
|
79
|
|
|
// If there is a block start other than list item, canContain makes sure that this list is closed. |
|
80
|
222 |
|
return BlockContinue::at($cursor); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
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.