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

Route   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 2
cbo 0
dl 0
loc 84
ccs 21
cts 21
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getMethod() 0 4 1
A getPattern() 0 4 1
A getHandler() 0 4 1
A getConfig() 0 8 3
A getParams() 0 5 1
A getPregPattern() 0 4 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