Passed
Push — master ( 1ba3e3...448aab )
by Kacper
03:26
created

Rule::setMatcher()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
crap 1
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
 * @property string                $name
35
 *
36
 */
37
class Rule
38
{
39
    /**
40
     * @var Validator
41
     */
42
    public $validator = false;
43
    private $_matcher;
44
    private $_options;
45
    private $_enabled = true;
46
47
    /**
48
     * @param MatcherInterface|null $matcher
49
     * @param array                 $options
50
     *
51
     * @throws \InvalidArgumentException
52
     */
53 98
    public function __construct(MatcherInterface $matcher = null, array $options = [])
54
    {
55 98
        $this->_matcher = $matcher;
56
57
        // Default options:
58 98
        $options = array_merge([
59 98
            'priority' => 1,
60 98
            'language' => false,
61 98
            'factory'  => new TokenFactory(Token::class),
62 98
            'enabled'  => true,
63
            'name'     => null
64 98
        ], $options);
65
66 98
        if (isset($options['context'])) {
67 43
            $this->setContext($options['context']);
68 42
        }
69
70 97
        $this->_options = $options;
71 97
        $this->_enabled = $options['enabled'];
72
73 97
        $this->factory->setRule($this);
74 97
    }
75
76 43
    public function setContext($context)
77
    {
78 43
        if (is_array($context)) {
79 5
            $this->validator = new Validator($context);
80 43
        } elseif (is_callable($context)) {
81 1
            $this->validator = new DelegateValidator($context);
82 40
        } elseif ($context instanceof Validator) {
83 38
            $this->validator = $context;
84 38
        } else {
85 1
            throw new \InvalidArgumentException('$context must be valid Validator');
86
        }
87 42
    }
88
89 2
    public function getMatcher()
90
    {
91 2
        return $this->_matcher;
92
    }
93
94 1
    public function setMatcher(MatcherInterface $matcher)
95
    {
96 1
        $this->_matcher = $matcher;
97 1
    }
98
99
    /**
100
     * @param $source
101
     *
102
     * @return Token[]|\Iterator
103
     */
104 24
    public function match($source)
105
    {
106 24
        return $this->_enabled && $this->_matcher !== null ? $this->_matcher->match($source, $this->factory) : [];
107
    }
108
109 97
    public function __get($option)
110
    {
111 97
        return isset($this->_options[$option]) ? $this->_options[$option] : null;
112
    }
113
114 19
    public function __set($option, $value)
115
    {
116 19
        return $this->_options[$option] = $value;
117
    }
118
119 1
    public function enable()
120
    {
121 1
        $this->_enabled = true;
122 1
    }
123
124 1
    public function disable()
125
    {
126 1
        $this->_enabled = false;
127 1
    }
128
}
129