Completed
Push — master ( ec1826...17149c )
by Taosikai
15:14
created

RouteCollection   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 76
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A add() 0 7 2
A search() 0 4 1
A merge() 0 6 2
A count() 0 4 1
A getIterator() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the jade/jade package.
5
 *
6
 * (c) Slince <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Jade\Routing;
13
14
final class RouteCollection implements \Countable, \IteratorAggregate
15
{
16
    /**
17
     * 路由集合
18
     * @var Route[]
19
     */
20
    protected $routes = [];
21
22
    /**
23
     * 路由计数
24
     *
25
     * @var int
26
     */
27
    protected $counter = 0;
28
29
    public function __construct($routes = [])
30
    {
31
        foreach ($routes as $route) {
32
            $this->add($route);
33
        }
34
    }
35
36
    /**
37
     * 添加一条 route
38
     *
39
     * @param Route $route
40
     */
41
    public function add(Route $route)
42
    {
43
        if (null === $route->getName()) { //路由名称为空则给予默认名称
44
            $route->setName($this->counter ++);
45
        }
46
        $this->routes[$route->getName()] = $route;
47
    }
48
49
    /**
50
     * 查找 route
51
     *
52
     * @param string $name
53
     * @return Route|null
54
     */
55
    public function search($name)
56
    {
57
        return $this->routes[$name] ?? null;
58
    }
59
60
    /**
61
     * 合并 route 集合
62
     *
63
     * @param RouteCollection $collection
64
     */
65
    public function merge(RouteCollection $collection)
66
    {
67
        foreach ($collection as $route) {
68
            $this->add($route);
69
        }
70
    }
71
72
    /**
73
     * 获取集合内路由总数
74
     *
75
     * @return int
76
     */
77
    public function count()
78
    {
79
        return count($this->routes);
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function getIterator()
86
    {
87
        return new \ArrayIterator($this->routes);
88
    }
89
}