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