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; |
17
|
|
|
|
18
|
|
|
use Kadet\Highlighter\Language\Language; |
19
|
|
|
use Kadet\Highlighter\Matcher\MatcherInterface; |
20
|
|
|
use Kadet\Highlighter\Parser\Token\Token; |
21
|
|
|
use Kadet\Highlighter\Parser\Validator\DelegateValidator; |
22
|
|
|
use Kadet\Highlighter\Parser\Validator\Validator; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class Rule |
26
|
|
|
* |
27
|
|
|
* @package Kadet\Highlighter\Parser |
28
|
|
|
* |
29
|
|
|
* @property Language $language |
30
|
|
|
* @property Language $inject |
31
|
|
|
* @property integer $priority |
32
|
|
|
* @property string $type |
33
|
|
|
* @property TokenFactoryInterface $factory |
34
|
|
|
* |
35
|
|
|
*/ |
36
|
|
|
class Rule |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* @var Validator |
40
|
|
|
*/ |
41
|
|
|
public $validator = false; |
42
|
|
|
private $_matcher; |
43
|
|
|
private $_options; |
44
|
|
|
private $_enabled = true; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param MatcherInterface|null $matcher |
48
|
|
|
* @param array $options |
49
|
|
|
*/ |
50
|
75 |
|
public function __construct(MatcherInterface $matcher = null, array $options = []) |
51
|
|
|
{ |
52
|
75 |
|
$this->_matcher = $matcher; |
53
|
|
|
|
54
|
|
|
// Default options: |
55
|
75 |
|
$options = array_merge([ |
56
|
75 |
|
'priority' => 1, |
57
|
75 |
|
'language' => false, |
58
|
75 |
|
'factory' => new TokenFactory(Token::class), |
59
|
75 |
|
'enabled' => true, |
60
|
75 |
|
], $options); |
61
|
|
|
|
62
|
75 |
|
if (isset($options['context'])) { |
63
|
23 |
|
$this->setContext($options['context']); |
64
|
22 |
|
} |
65
|
|
|
|
66
|
74 |
|
$this->_options = $options; |
67
|
74 |
|
$this->_enabled = $options['enabled']; |
68
|
|
|
|
69
|
74 |
|
$this->factory->setRule($this); |
70
|
74 |
|
} |
71
|
|
|
|
72
|
23 |
|
public function setContext($context) |
73
|
|
|
{ |
74
|
23 |
|
if (is_array($context)) { |
75
|
4 |
|
$this->validator = new Validator($context); |
76
|
23 |
|
} elseif (is_callable($context)) { |
77
|
1 |
|
$this->validator = new DelegateValidator($context); |
78
|
20 |
|
} elseif ($context instanceof Validator) { |
79
|
18 |
|
$this->validator = $context; |
80
|
18 |
|
} else { |
81
|
1 |
|
throw new \InvalidArgumentException('$context must be valid Validator'); |
82
|
|
|
} |
83
|
22 |
|
} |
84
|
|
|
|
85
|
2 |
|
public function getMatcher() |
86
|
|
|
{ |
87
|
2 |
|
return $this->_matcher; |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
public function setMatcher(MatcherInterface $matcher) |
91
|
|
|
{ |
92
|
1 |
|
$this->_matcher = $matcher; |
93
|
1 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param $source |
97
|
|
|
* |
98
|
|
|
* @return Token[] |
99
|
|
|
*/ |
100
|
15 |
|
public function match($source) |
101
|
|
|
{ |
102
|
15 |
|
return $this->_enabled && $this->_matcher !== null ? $this->_matcher->match($source, $this->factory) : []; |
103
|
|
|
} |
104
|
|
|
|
105
|
74 |
|
public function __get($option) |
106
|
|
|
{ |
107
|
74 |
|
return isset($this->_options[$option]) ? $this->_options[$option] : null; |
108
|
|
|
} |
109
|
|
|
|
110
|
15 |
|
public function __set($option, $value) |
111
|
|
|
{ |
112
|
15 |
|
return $this->_options[$option] = $value; |
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
public function enable() |
116
|
|
|
{ |
117
|
1 |
|
$this->_enabled = true; |
118
|
1 |
|
} |
119
|
|
|
|
120
|
1 |
|
public function disable() |
121
|
|
|
{ |
122
|
1 |
|
$this->_enabled = false; |
123
|
1 |
|
} |
124
|
|
|
} |
125
|
|
|
|