Passed
Push — master ( 4c697a...778310 )
by Kacper
02:53
created

TokenFactory::getRule()   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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
ccs 2
cts 2
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
19
class TokenFactory implements TokenFactoryInterface
20
{
21
    const START = 0x1;
22
    const END   = 0x2;
23
24
    private $_class;
25
    private $_base;
26
    private $_rule;
27
28
    private $_cache = [];
29
30
    /**
31
     * TokenFactory constructor.
32
     *
33
     * @param $class
34
     */
35 18
    public function __construct($class) {
36 18
        $this->setClass($class);
37 17
    }
38
39 3
    public function create($params) {
40 3
        $params[0] = !empty($params[0]) ? $this->getName($params[0]) : $this->_base;
41 3
        if(empty($params['rule'])) {
42 3
            $params['rule'] = $this->_rule;
43 3
        }
44
45 3
        return new $this->_class($params);
46
    }
47
48 1
    private function getName($name) {
49 1
        if (!isset($this->_cache[$name])) {
50 1
            $this->_cache[$name] = str_replace('$', $this->_base, $name);
51 1
        }
52
53 1
        return $this->_cache[$name];
54
    }
55
56
    /**
57
     * @return string
58
     */
59 1
    public function getBase()
60
    {
61 1
        return $this->_base;
62
    }
63
64
    /**
65
     * @param string $base
66
     */
67 1
    public function setBase($base)
68
    {
69 1
        $this->_cache = []; // invalidate cache
70 1
        $this->_base = $base;
71 1
    }
72
73
    /**
74
     * @return mixed
75
     */
76 1
    public function getRule()
77
    {
78 1
        return $this->_rule;
79
    }
80
81
    /**
82
     * @param mixed $rule
83
     */
84 13
    public function setRule($rule)
85
    {
86 13
        $this->_rule = $rule;
87 13
    }
88
89
    /**
90
     * @return mixed
91
     */
92 1
    public function getClass()
93
    {
94 1
        return $this->_class;
95
    }
96
97
    /**
98
     * @param mixed $class
99
     *
100
     * @throws \InvalidArgumentException
101
     */
102 18
    public function setClass($class)
103
    {
104 18
        if(!is_a($class, 'Kadet\Highlighter\Parser\Token', true)) {
105 1
            throw new \InvalidArgumentException('$class must extend Kadet\Highlighter\Parser\Token');
106
        }
107
108 17
        $this->_class = $class;
109
    }
110
}