Passed
Pull Request — master (#3)
by Бабичев
01:48
created

Pattern::setPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Bavix\Router;
4
5
class Pattern
6
{
7
8
    /**
9
     * if methods === null -> full methods
10
     *
11
     * @var null|array
12
     */
13
    protected $methods;
14
15
    /**
16
     * @var array
17
     */
18
    protected $defaults = [];
19
20
    /**
21
     * @var string
22
     */
23
    protected $name;
24
25
    /**
26
     * @var string
27
     */
28
    protected $path;
29
30
    /**
31
     * Pattern constructor.
32
     * @param string $path
33
     * @param string $name
34
     */
35
    public function __construct(string $path, ?string $name)
36
    {
37
        $this->path = $path;
38
        $this->name = $name;
39
    }
40
41
    /**
42
     * @return string
43
     */
44
    public function getName(): string
45
    {
46
        return $this->name ?: $this->generateName();
47
    }
48
49
    /**
50
     * @param string $name
51
     * @return $this
52
     */
53
    public function setName(string $name): self
54
    {
55
        $this->name = $name;
56
        return $this;
57
    }
58
59
    /**
60
     * @return array|null
61
     */
62
    public function getMethods(): ?array
63
    {
64
        return $this->methods;
65
    }
66
67
    /**
68
     * @param array|null $methods
69
     * @return $this
70
     */
71
    public function setMethods(?array $methods): self
72
    {
73
        $this->methods = $methods;
74
        return $this;
75
    }
76
77
    /**
78
     * @return array
79
     */
80
    public function getDefaults(): array
81
    {
82
        return $this->defaults;
83
    }
84
85
    /**
86
     * @param array $defaults
87
     * @return $this
88
     */
89
    public function setDefaults(array $defaults): self
90
    {
91
        $this->defaults = $defaults;
92
        return $this;
93
    }
94
95
    /**
96
     * @return string
97
     */
98
    public function getPath(): string
99
    {
100
        return $this->path;
101
    }
102
103
    /**
104
     * @param string $path
105
     * @return $this
106
     */
107
    public function setPath(string $path): self
108
    {
109
        $this->path = $path;
110
        return $this;
111
    }
112
113
    /**
114
     * @return array
115
     */
116
    public function toArray(): array
117
    {
118
        return [
119
            $this->getName() => [
120
                'type' => 'pattern',
121
                'path' => $this->getPath(),
122
                'methods' => $this->getMethods(),
123
                'defaults' => $this->getDefaults(),
124
            ]
125
        ];
126
    }
127
128
    /**
129
     * @return string
130
     */
131
    protected function generateName(): string
132
    {
133
        return \preg_replace('~[^\w-]~', '_', $this->path);
134
    }
135
136
}
137