Completed
Push — master ( dfaf51...188ba1 )
by Samuel
02:15
created

Route::getPregPattern()   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
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Kelemen\ApiNette\Route;
4
5
class Route
6
{
7
    /** @var string */
8
    private $method;
9
10
    /** @var string */
11
    private $pattern;
12
13
    /** @var string */
14
    private $handler;
15
16
    /** @var array */
17
    private $config;
18
19
    /**
20
     * @param string $method
21
     * @param string $pattern
22
     * @param string $handler
23
     * @param array $config
24
     */
25 22
    public function __construct($method, $pattern, $handler, array $config = [])
26
    {
27 22
        $this->method = strtolower($method);
28 22
        $this->pattern = $pattern;
29 22
        $this->handler = $handler;
30 22
        $this->config = $config;
31 22
    }
32
33
    /**
34
     * @return string
35
     */
36 14
    public function getMethod()
37
    {
38 14
        return $this->method;
39
    }
40
41
    /**
42
     * @return string
43
     */
44 2
    public function getPattern()
45
    {
46 2
        return $this->pattern;
47
    }
48
49
    /**
50
     * @return string
51
     */
52 4
    public function getHandler()
53
    {
54 4
        return $this->handler;
55
    }
56
57
    /**
58
     * @param string|null $key
59
     * @return array|false
60
     */
61 2
    public function getConfig($key = null)
62
    {
63 2
        if ($key === null) {
64 2
            return $this->config;
65
        }
66
67 2
        return isset($this->config[$key]) ? $this->config[$key] : false;
68
    }
69
70
    /**
71
     * Parse defined pattern params
72
     * @return array
73
     */
74 10
    public function getParams()
75
    {
76 10
        preg_match_all('#\{(.*?)\}#', $this->pattern, $params);
77 10
        return $params[1];
78
    }
79
80
    /**
81
     * Prepare preg replace pattern from defined route pattern
82
     * @return string
83
     */
84 12
    public function getPregPattern()
85
    {
86 12
        return '^' . preg_replace('#\{.*?\}#', '([^/]*)?', $this->pattern) . '$';
87
    }
88
}
89