|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Highlighter |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (C) 2016, Some right reserved. |
|
6
|
|
|
* |
|
7
|
|
|
* @author Kacper "Kadet" Donat <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* Contact with author: |
|
10
|
|
|
* Xmpp: [email protected] |
|
11
|
|
|
* E-mail: [email protected] |
|
12
|
|
|
* |
|
13
|
|
|
* From Kadet with love. |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Kadet\Highlighter\Language; |
|
17
|
|
|
|
|
18
|
|
|
use Kadet\Highlighter\Matcher\RegexMatcher; |
|
19
|
|
|
use Kadet\Highlighter\Parser\CloseRule; |
|
20
|
|
|
use Kadet\Highlighter\Parser\Token\LanguageToken; |
|
21
|
|
|
use Kadet\Highlighter\Parser\OpenRule; |
|
22
|
|
|
use Kadet\Highlighter\Parser\TokenFactory; |
|
23
|
|
|
|
|
24
|
|
|
class Html extends Xml |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Tokenization rules |
|
28
|
|
|
*/ |
|
29
|
|
|
public function setupRules() |
|
30
|
|
|
{ |
|
31
|
|
|
parent::setupRules(); |
|
32
|
|
|
|
|
33
|
|
|
$css = new Css(); |
|
34
|
|
|
$js = new JavaScript(); |
|
35
|
|
|
$this->rules->addMany([ |
|
36
|
|
|
'language.'.$js->getIdentifier() => [ |
|
37
|
|
|
new OpenRule(new RegexMatcher('/<script.*?>()/'), [ |
|
38
|
|
|
'factory' => new TokenFactory(LanguageToken::class), |
|
39
|
|
|
'inject' => $js, |
|
40
|
|
|
'language' => $this, |
|
41
|
|
|
'postProcess' => true |
|
42
|
|
|
]), |
|
43
|
|
|
new CloseRule(new RegexMatcher('/()<\/script>/'), [ |
|
44
|
|
|
'factory' => new TokenFactory(LanguageToken::class), |
|
45
|
|
|
'language' => $js |
|
46
|
|
|
]) |
|
47
|
|
|
], |
|
48
|
|
|
'language.'.$css->getIdentifier() => [ |
|
49
|
|
|
new OpenRule(new RegexMatcher('/<style.*?>()/'), [ |
|
50
|
|
|
'factory' => new TokenFactory(LanguageToken::class), |
|
51
|
|
|
'inject' => $css, |
|
52
|
|
|
'language' => $this, |
|
53
|
|
|
'postProcess' => true |
|
54
|
|
|
]), |
|
55
|
|
|
new CloseRule(new RegexMatcher('/()<\/style>/'), [ |
|
56
|
|
|
'factory' => new TokenFactory(LanguageToken::class), |
|
57
|
|
|
'language' => $css |
|
58
|
|
|
]) |
|
59
|
|
|
] |
|
60
|
|
|
]); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function getIdentifier() |
|
64
|
|
|
{ |
|
65
|
|
|
return 'html'; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
View Code Duplication |
public static function getAliases() |
|
69
|
|
|
{ |
|
70
|
|
|
return [ |
|
|
|
|
|
|
71
|
|
|
'name' => ['html'], |
|
72
|
|
|
'mime' => ['text/html'], |
|
73
|
|
|
'extension' => ['*.html', '*.htm'] |
|
74
|
|
|
]; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
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.