|
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\HtmlBlock; |
|
15
|
|
|
use League\CommonMark\Node\Block\AbstractBlock; |
|
16
|
|
|
use League\CommonMark\Parser\Block\AbstractBlockContinueParser; |
|
17
|
|
|
use League\CommonMark\Parser\Block\BlockContinue; |
|
18
|
|
|
use League\CommonMark\Parser\Block\BlockContinueParserInterface; |
|
19
|
|
|
use League\CommonMark\Parser\Cursor; |
|
20
|
|
|
use League\CommonMark\Util\RegexHelper; |
|
21
|
|
|
|
|
22
|
|
|
final class HtmlBlockParser extends AbstractBlockContinueParser |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var HtmlBlock */ |
|
25
|
|
|
private $block; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
private $content = ''; |
|
31
|
|
|
|
|
32
|
|
|
/** @var bool */ |
|
33
|
|
|
private $finished = false; |
|
34
|
|
|
|
|
35
|
168 |
|
public function __construct(int $blockType) |
|
36
|
|
|
{ |
|
37
|
168 |
|
$this->block = new HtmlBlock($blockType); |
|
38
|
168 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return HtmlBlock |
|
42
|
|
|
*/ |
|
43
|
168 |
|
public function getBlock(): AbstractBlock |
|
44
|
|
|
{ |
|
45
|
168 |
|
return $this->block; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
153 |
|
public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): ?BlockContinue |
|
49
|
|
|
{ |
|
50
|
153 |
|
if ($this->finished) { |
|
51
|
48 |
|
return BlockContinue::none(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
138 |
|
if ($cursor->isBlank() && \in_array($this->block->getType(), [HtmlBlock::TYPE_6_BLOCK_ELEMENT, HtmlBlock::TYPE_7_MISC_ELEMENT], true)) { |
|
55
|
48 |
|
return BlockContinue::none(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
117 |
|
return BlockContinue::at($cursor); |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
168 |
|
public function addLine(string $line): void |
|
62
|
|
|
{ |
|
63
|
168 |
|
if ($this->content !== '') { |
|
64
|
117 |
|
$this->content .= "\n"; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
168 |
|
$this->content .= $line; |
|
68
|
|
|
|
|
69
|
|
|
// Check for end condition |
|
70
|
168 |
|
if ($this->block->getType() >= HtmlBlock::TYPE_1_CODE_CONTAINER && $this->block->getType() <= HtmlBlock::TYPE_5_CDATA) { |
|
71
|
57 |
|
$cursor = new Cursor($line); |
|
72
|
57 |
|
if ($cursor->match(RegexHelper::getHtmlBlockCloseRegex($this->block->getType())) !== null) { |
|
73
|
54 |
|
$this->finished = true; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
168 |
|
} |
|
77
|
|
|
|
|
78
|
168 |
|
public function closeBlock(): void |
|
79
|
|
|
{ |
|
80
|
168 |
|
$this->block->setLiteral($this->content); |
|
81
|
168 |
|
$this->content = ''; |
|
82
|
168 |
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
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.