Passed
Push — master ( 8cbd50...daf0b2 )
by
unknown
13:13
created

Route   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 18
dl 0
loc 136
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setOptions() 0 4 1
A setPath() 0 4 1
A __construct() 0 3 1
A getOption() 0 3 1
A getOptions() 0 3 1
A hasOption() 0 3 1
A setOption() 0 4 1
A getPath() 0 3 1
A getMethods() 0 3 1
A setMethods() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace TYPO3\CMS\Backend\Routing;
17
18
/**
19
 * This is a single entity for a Route.
20
 *
21
 * The architecture is highly inspired by the Symfony Routing Component.
22
 */
23
class Route
24
{
25
    /**
26
     * @var string
27
     */
28
    protected $path = '/';
29
30
    protected array $methods = [];
31
32
    /**
33
     * @var array
34
     */
35
    protected $options = [];
36
37
    /**
38
     * Constructor setting up the required path and options
39
     *
40
     * @param string $path The path pattern to match
41
     * @param array $options An array of options
42
     */
43
    public function __construct($path, $options)
44
    {
45
        $this->setPath($path)->setOptions($options);
46
    }
47
48
    /**
49
     * Returns the path
50
     *
51
     * @return string The path pattern
52
     */
53
    public function getPath()
54
    {
55
        return $this->path;
56
    }
57
58
    /**
59
     * Sets the pattern for the path
60
     * A pattern must start with a slash and must not have multiple slashes at the beginning because the
61
     * generated path for this route would be confused with a network path, e.g. '//domain.com/path'.
62
     *
63
     * This method implements a fluent interface.
64
     *
65
     * @param string $pattern The path pattern
66
     * @return Route The current Route instance
67
     */
68
    public function setPath($pattern)
69
    {
70
        $this->path = '/' . ltrim(trim($pattern), '/');
71
        return $this;
72
    }
73
74
    /**
75
     * Returns the uppercased HTTP methods this route is restricted to.
76
     * An empty array means that any method is allowed.
77
     *
78
     * @return string[] The methods
79
     */
80
    public function getMethods(): array
81
    {
82
        return $this->methods;
83
    }
84
85
    /**
86
     * Sets the HTTP methods (e.g. ['POST']) this route is restricted to.
87
     * An empty array means that any method is allowed.
88
     *
89
     * This method implements a fluent interface.
90
     *
91
     * @param string[] $methods The array of allowed methods
92
     * @return self
93
     */
94
    public function setMethods(array $methods): self
95
    {
96
        $this->methods = array_map('strtoupper', $methods);
97
        return $this;
98
    }
99
100
    /**
101
     * Returns the options set
102
     *
103
     * @return array The options
104
     */
105
    public function getOptions()
106
    {
107
        return $this->options;
108
    }
109
110
    /**
111
     * Sets the options
112
     *
113
     * This method implements a fluent interface.
114
     *
115
     * @param array $options The options
116
     * @return Route The current Route instance
117
     */
118
    public function setOptions(array $options)
119
    {
120
        $this->options = $options;
121
        return $this;
122
    }
123
124
    /**
125
     * Sets an option value
126
     *
127
     * This method implements a fluent interface.
128
     *
129
     * @param string $name An option name
130
     * @param mixed $value The option value
131
     * @return Route The current Route instance
132
     */
133
    public function setOption($name, $value)
134
    {
135
        $this->options[$name] = $value;
136
        return $this;
137
    }
138
139
    /**
140
     * Get an option value
141
     *
142
     * @param string $name An option name
143
     * @return mixed The option value or NULL when not given
144
     */
145
    public function getOption($name)
146
    {
147
        return $this->options[$name] ?? null;
148
    }
149
150
    /**
151
     * Checks if an option has been set
152
     *
153
     * @param string $name An option name
154
     * @return bool TRUE if the option is set, FALSE otherwise
155
     */
156
    public function hasOption($name)
157
    {
158
        return array_key_exists($name, $this->options);
159
    }
160
}
161