Passed
Push — master ( 454cf7...0d52c3 )
by Kacper
02:57
created

Rule::_getContextRule()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 5

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 30
ccs 19
cts 19
cp 1
rs 8.439
cc 5
eloc 18
nc 5
nop 1
crap 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A Rule::match() 0 4 2
A Rule::validate() 0 4 1
A Rule::__get() 0 4 2
A Rule::__set() 0 4 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
    
41
    /**
42
     * @var Validator
43
     */
44
    private $_validator;
45
46
    /**
47
     * @param MatcherInterface|null $matcher
48
     * @param array                 $options
49
     */
50 58
    public function __construct(MatcherInterface $matcher = null, array $options = [])
51
    {
52 58
        $this->_matcher = $matcher;
53
54
        // Default options:
55 58
        $options = array_merge([
56 58
            'context'  => [],
57 58
            'priority' => 1,
58 58
            'language' => false,
59 58
            'factory'  => new TokenFactory(Token::class),
60
            'closedBy' => false
61 58
        ], $options);
62
63 58
        $this->setContext($options['context']);
64 57
        $this->_options = $options;
65
66 57
        $this->factory->setRule($this);
67 57
    }
68
69 58
    public function setContext($context) {
70 58
        if(is_array($context)) {
71 51
            $this->_validator = new Validator($context);
72 58
        } elseif(is_callable($context)) {
73 1
            $this->_validator = new DelegateValidator($context);
74 18
        }elseif($context instanceof Validator) {
75 16
            $this->_validator = $context;
76 16
        } else {
77 1
            throw new \InvalidArgumentException('$context must be valid Validator');
78
        }
79 57
    }
80
81
    /**
82
     * @param $source
83
     *
84
     * @return Token[]
85
     */
86 13
    public function match($source)
87
    {
88 13
        return $this->_matcher !== null ? $this->_matcher->match($source, $this->factory) : [];
89
    }
90
91 16
    public function validate($context, array $additional = [])
92
    {
93 16
        return $this->_validator->validate($context, $additional);
94
    }
95
96 57
    public function __get($option)
97
    {
98 57
        return isset($this->_options[$option]) ? $this->_options[$option] : null;
99
    }
100
101 11
    public function __set($option, $value)
102
    {
103 11
        return $this->_options[$option] = $value;
104
    }
105
}
106