Passed
Push — master ( d17744...bd0f24 )
by 世昌
01:45
created

RouteCollection::save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace nebula\route;
3
4
use Iterator;
5
use ArrayIterator;
6
7
/**
8
 * 路由集合
9
 *
10
 */
11
class RouteCollection
12
{
13
    /**
14
     * 属性
15
     *
16
     * @var RouteMatcher[]
17
     */
18
    protected $collection = [];
19
20
    /**
21
     * 创建集合
22
     *
23
     * @param RouteMatcher[] $collection
24
     */
25
    public function __construct(array $collection=[])
26
    {
27
        $this->merge($collection);
28
    }
29
30
    /**
31
     * 合并集合
32
     *
33
     * @param array $collection
34
     * @return void
35
     */
36
    public function merge(array $collection=[])
37
    {
38
        $this->collection = array_merge($this->collection, $collection);
39
    }
40
41
    /**
42
     * 添加集合
43
     *
44
     * @param string $name
45
     * @param RouteMatcher $collection
46
     * @return void
47
     */
48
    public function add(string $name, RouteMatcher $collection)
49
    {
50
        $this->collection[$name] = $collection;
51
    }
52
53
    /**
54
     * 获取集合
55
     *
56
     * @param string $name
57
     * @return RouteMatcher|null
58
     */
59
    public function get(string $name):?RouteMatcher
60
    {
61
        return $this->collection[$name] ?? null;
62
    }
63
64
    /**
65
     * 获取迭代器
66
     *
67
     * @return RouteMatcher[]
68
     */
69
    public function getCollection():array
70
    {
71
        return $this->collection;
72
    }
73
74
    /**
75
     * 从文件创建
76
     *
77
     * @param string $path
78
     * @return void
79
     */
80
    public static function fromFile(string $path)
81
    {
82
        return \unserialize(\file_get_contents($path));
83
    }
84
85
    /**
86
     * 保存到文件
87
     *
88
     * @param string $path
89
     * @return boolean
90
     */
91
    public function save(string $path):bool
92
    {
93
        return \file_put_contents($path, \serialize($this));
0 ignored issues
show
Bug Best Practice introduced by
The expression return file_put_contents($path, serialize($this)) returns the type integer which is incompatible with the type-hinted return boolean.
Loading history...
94
    }
95
}
96