|
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\Parser\Token; |
|
17
|
|
|
|
|
18
|
|
|
use Kadet\Highlighter\Language\Language; |
|
19
|
|
|
use Kadet\Highlighter\Parser\Context; |
|
20
|
|
|
use Kadet\Highlighter\Parser\Result; |
|
21
|
|
|
use Kadet\Highlighter\Parser\TokenIterator; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class LanguageToken |
|
25
|
|
|
* |
|
26
|
|
|
* @package Kadet\Highlighter\Parser\Token |
|
27
|
|
|
* |
|
28
|
|
|
* @property bool $postProcess True if language is post processed. |
|
29
|
|
|
* @property Language $inject |
|
30
|
|
|
*/ |
|
31
|
|
|
class LanguageToken extends Token |
|
32
|
|
|
{ |
|
33
|
12 |
|
public function __construct($name, $options = []) { |
|
34
|
12 |
|
parent::__construct($name, $options); |
|
35
|
12 |
|
if(isset($options['inject'])) { |
|
36
|
|
|
$this->inject = $options['inject']; |
|
37
|
|
|
} |
|
38
|
12 |
|
} |
|
39
|
|
|
|
|
40
|
11 |
|
public function getLanguage() |
|
41
|
2 |
|
{ |
|
42
|
11 |
|
return $this->getStart() ? $this->getStart()->inject : $this->rule->language; |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
11 |
|
protected function validate(Context $context) |
|
46
|
|
|
{ |
|
47
|
11 |
|
$valid = false; |
|
48
|
|
|
|
|
49
|
11 |
|
if ($this->isStart()) { |
|
50
|
2 |
|
$lang = $this->rule->language; |
|
51
|
2 |
|
if ($lang === null && $this->inject !== $context->language) { |
|
52
|
1 |
|
$valid = true; |
|
53
|
2 |
|
} elseif ($context->language === $lang && $this->rule->validator->validate($context)) { |
|
54
|
1 |
|
$valid = true; |
|
55
|
1 |
|
} |
|
56
|
2 |
|
} else { |
|
57
|
11 |
|
$desired = $this->getLanguage(); |
|
58
|
11 |
|
$valid = $context->language === $desired && $this->rule->validator->validate($context); |
|
59
|
|
|
} |
|
60
|
11 |
|
$this->setValid($valid); |
|
61
|
11 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
2 |
|
protected function processStart(Context $context, Language $language, Result $result, TokenIterator $tokens) |
|
65
|
|
|
{ |
|
66
|
2 |
|
$result->merge($this->inject->parse($tokens)); |
|
67
|
|
|
|
|
68
|
2 |
|
return true; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
11 |
|
protected function processEnd(Context $context, Language $language, Result $result, TokenIterator $tokens) |
|
72
|
|
|
{ |
|
73
|
11 |
|
$this->setStart($result->getStart()); |
|
74
|
|
|
|
|
75
|
11 |
|
if ($this->_start->postProcess) { |
|
76
|
1 |
|
$source = substr($tokens->getSource(), $this->_start->pos, $this->_start->getLength()); |
|
77
|
1 |
|
$tokens = $this->_start->inject->tokenize( |
|
78
|
1 |
|
$source, $result, $this->_start->pos, Language::EMBEDDED_BY_PARENT |
|
79
|
1 |
|
); |
|
80
|
1 |
|
$result->exchangeArray($this->_start->inject->parse($tokens)->getTokens()); |
|
81
|
1 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
# closing unclosed tokens |
|
84
|
11 |
|
foreach (array_reverse($context->stack, true) as $id => $name) { |
|
85
|
1 |
|
$end = new Token($name, ['pos' => $this->pos]); |
|
86
|
1 |
|
$tokens[$id]->setEnd($end); |
|
87
|
1 |
|
$result->append($end); |
|
88
|
11 |
|
} |
|
89
|
|
|
|
|
90
|
11 |
|
$result->append($this); |
|
91
|
11 |
|
return false; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.