|
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\FencedCode; |
|
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\ArrayCollection; |
|
21
|
|
|
use League\CommonMark\Util\RegexHelper; |
|
22
|
|
|
|
|
23
|
|
|
final class FencedCodeParser extends AbstractBlockContinueParser |
|
24
|
|
|
{ |
|
25
|
|
|
/** @var FencedCode */ |
|
26
|
|
|
private $block; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var ArrayCollection<int, string> |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $strings; |
|
32
|
|
|
|
|
33
|
105 |
|
public function __construct(int $fenceLength, string $fenceChar, int $fenceOffset) |
|
34
|
|
|
{ |
|
35
|
105 |
|
$this->block = new FencedCode($fenceLength, $fenceChar, $fenceOffset); |
|
36
|
105 |
|
$this->strings = new ArrayCollection(); |
|
37
|
105 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return FencedCode |
|
41
|
|
|
*/ |
|
42
|
105 |
|
public function getBlock(): AbstractBlock |
|
43
|
|
|
{ |
|
44
|
105 |
|
return $this->block; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
99 |
|
public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): ?BlockContinue |
|
48
|
|
|
{ |
|
49
|
|
|
// Check for closing code fence |
|
50
|
99 |
|
if (!$cursor->isIndented() && $cursor->getNextNonSpaceCharacter() === $this->block->getChar()) { |
|
51
|
93 |
|
$match = RegexHelper::matchAll('/^(?:`{3,}|~{3,})(?= *$)/', $cursor->getLine(), $cursor->getNextNonSpacePosition()); |
|
52
|
93 |
|
if ($match !== null && \strlen($match[0]) >= $this->block->getLength()) { |
|
53
|
|
|
// closing fence - we're at end of line, so we can finalize now |
|
54
|
87 |
|
return BlockContinue::finished(); |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
// Skip optional spaces of fence offset |
|
59
|
93 |
|
$cursor->match('/^ {0,' . $this->block->getOffset() . '}/'); |
|
60
|
|
|
|
|
61
|
93 |
|
return BlockContinue::at($cursor); |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
105 |
|
public function addLine(string $line): void |
|
65
|
|
|
{ |
|
66
|
105 |
|
$this->strings[] = $line; |
|
67
|
105 |
|
} |
|
68
|
|
|
|
|
69
|
105 |
|
public function closeBlock(): void |
|
70
|
|
|
{ |
|
71
|
|
|
// first line becomes info string |
|
72
|
105 |
|
$firstLine = $this->strings->first(); |
|
73
|
105 |
|
if ($firstLine === false) { |
|
74
|
|
|
$firstLine = ''; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
105 |
|
$this->block->setInfo(RegexHelper::unescape(\trim($firstLine))); |
|
78
|
|
|
|
|
79
|
105 |
|
if ($this->strings->count() === 1) { |
|
80
|
12 |
|
$this->block->setLiteral(''); |
|
81
|
|
|
} else { |
|
82
|
93 |
|
$this->block->setLiteral(\implode("\n", $this->strings->slice(1)) . "\n"); |
|
83
|
|
|
} |
|
84
|
105 |
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
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.