RegexRouteCollector   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 93.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 51
ccs 14
cts 15
cp 0.9333
rs 10
wmc 8
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
C addRoute() 0 26 7
A getData() 0 4 1
1
<?php
2
namespace JayaCode\Framework\Core\Route\Collector;
3
4
use JayaCode\Framework\Core\Route\Dispatcher\RegexDispatcher;
5
use JayaCode\Framework\Core\Route\Route;
6
7
/**
8
 * Class RegexRouteCollector
9
 * @package JayaCode\Framework\Core\Route\Collector
10
 */
11
class RegexRouteCollector implements RouteCollector
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $regex = '/^(\w+|\{(\w+)(\:([\w|\[|\-|\]|\\|\+|\?|\*|\^|\$|\||\.]+))?\})$/';
17
18
    /**
19
     * @var array
20
     */
21
    protected $routes = [];
22
23
    /**
24
     * @param Route $route
25
     * @throws \Exception
26
     */
27 3
    public function addRoute(Route $route)
28
    {
29 3
        $route->path = RegexDispatcher::normalizePath($route->path);
30 3
        $route->explodePath = RegexDispatcher::explodePath($route->path);
31
32 3
        foreach ($route->explodePath as $item) {
33 3
            $matches = array();
34 3
            preg_match($this->regex, $item, $matches);
35
36 3
            $count = count($matches);
37
38
            /**
39
             * path == "" // path / home
40
             * count == 2 // basic path ex: user
41
             * count == 3 // var path ex: {id}
42
             * count == 5 // var path with regex ex: {id:[1-9]*}
43
             */
44 3
            if ($route->path != "" && $count != 0 && $count != 2 && $count != 3 && $count != 5) {
45
                throw new \Exception("bad route! path : {$item} {$count}");
46
            }
47
48 3
            $route->ruleMatches[] = $matches;
49 3
        }
50
51 3
        $this->routes[] = $route;
52 3
    }
53
54
    /**
55
     * @return array
56
     */
57 3
    public function getData()
58
    {
59 3
        return $this->routes;
60
    }
61
}
62