Passed
Push — master ( 0d52c3...1ee19c )
by Kacper
02:45
created

Rule::validate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 4
rs 10
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 2
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
    
41
    /**
42
     * @var Validator
43
     */
44
    public $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 58
        ], $options);
61
62 58
        $this->setContext($options['context']);
63 57
        $this->_options = $options;
64
65 57
        $this->factory->setRule($this);
66 57
    }
67
68 58
    public function setContext($context) {
69 58
        if(is_array($context)) {
70 51
            $this->validator = new Validator($context);
71 58
        } elseif(is_callable($context)) {
72 1
            $this->validator = new DelegateValidator($context);
73 18
        }elseif($context instanceof Validator) {
74 16
            $this->validator = $context;
75 16
        } else {
76 1
            throw new \InvalidArgumentException('$context must be valid Validator');
77
        }
78 57
    }
79
80
    /**
81
     * @param $source
82
     *
83
     * @return Token[]
84
     */
85 13
    public function match($source)
86
    {
87 13
        return $this->_matcher !== null ? $this->_matcher->match($source, $this->factory) : [];
88
    }
89
90 57
    public function __get($option)
91
    {
92 57
        return isset($this->_options[$option]) ? $this->_options[$option] : null;
93
    }
94
95 11
    public function __set($option, $value)
96
    {
97 11
        return $this->_options[$option] = $value;
98
    }
99
}
100