1
|
|
|
<?php |
2
|
|
|
namespace Danhunsaker\Markua\Extension; |
3
|
|
|
|
4
|
|
|
use League\CommonMark\Block\Parser as BlockParser; |
5
|
|
|
use League\CommonMark\Extension\CommonMarkCoreExtension; |
6
|
|
|
use Danhunsaker\Markua\Block\Parser as MarkuaBlockParser; |
7
|
|
|
use Danhunsaker\Markua\Block\Renderer\AsideRenderer; |
8
|
|
|
use Danhunsaker\Markua\Block\Renderer\IconBlockRenderer; |
9
|
|
|
|
10
|
|
|
class MarkuaExtension extends CommonMarkCoreExtension |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* {@inheritdoc} |
15
|
|
|
*/ |
16
|
30 |
|
public function getBlockParsers() |
17
|
|
|
{ |
18
|
|
|
return array( |
|
|
|
|
19
|
|
|
// This order is important |
20
|
30 |
|
new BlockParser\IndentedCodeParser(), |
21
|
30 |
|
new BlockParser\LazyParagraphParser(), |
22
|
30 |
|
new BlockParser\BlockQuoteParser(), |
23
|
30 |
|
new MarkuaBlockParser\AsideParser(), |
24
|
30 |
|
new MarkuaBlockParser\IconBlockParser(), |
25
|
30 |
|
new BlockParser\ATXHeadingParser(), |
26
|
30 |
|
new BlockParser\FencedCodeParser(), |
27
|
30 |
|
new BlockParser\HtmlBlockParser(), |
28
|
30 |
|
new BlockParser\SetExtHeadingParser(), |
29
|
30 |
|
new BlockParser\ThematicBreakParser(), |
30
|
30 |
|
new BlockParser\ListParser(), |
31
|
30 |
|
); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
30 |
|
public function getBlockRenderers() |
38
|
|
|
{ |
39
|
30 |
|
$renderers = parent::getBlockRenderers(); |
40
|
30 |
|
$renderers['Danhunsaker\Markua\Block\Element\Aside'] = new AsideRenderer(); |
41
|
30 |
|
$renderers['Danhunsaker\Markua\Block\Element\IconBlock'] = new IconBlockRenderer(); |
42
|
30 |
|
return $renderers; |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
1 |
|
public function getName() |
49
|
|
|
{ |
50
|
1 |
|
return 'markua'; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
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.