Passed
Push — master ( ad0973...f9c297 )
by Kacper
02:48
created

Rule::enable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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