1 <?php
2 /**
3 * Part of the Joomla Framework Uri Package
4 *
5 * @copyright Copyright (C) 2005 - 2016 Open Source Matters. All rights reserved.
6 * @license GNU General Public License version 2 or later; see LICENSE
7 */
8
9 namespace Joomla\Uri;
10
11 /**
12 * Uri Class
13 *
14 * This class parses a URI and provides a common interface for the Joomla Framework
15 * to access and manipulate a URI.
16 *
17 * @since 1.0
18 */
19 class Uri extends AbstractUri
20 {
21 /**
22 * Adds a query variable and value, replacing the value if it
23 * already exists and returning the old value.
24 *
25 * @param string $name Name of the query variable to set.
26 * @param string $value Value of the query variable.
27 *
28 * @return string Previous value for the query variable.
29 *
30 * @since 1.0
31 */
32 public function setVar($name, $value)
33 {
34 $tmp = isset($this->vars[$name]) ? $this->vars[$name] : null;
35
36 $this->vars[$name] = $value;
37
38 // Empty the query
39 $this->query = null;
40
41 return $tmp;
42 }
43
44 /**
45 * Removes an item from the query string variables if it exists.
46 *
47 * @param string $name Name of variable to remove.
48 *
49 * @return void
50 *
51 * @since 1.0
52 */
53 public function delVar($name)
54 {
55 if (array_key_exists($name, $this->vars))
56 {
57 unset($this->vars[$name]);
58
59 // Empty the query
60 $this->query = null;
61 }
62 }
63
64 /**
65 * Sets the query to a supplied string in format:
66 * foo=bar&x=y
67 *
68 * @param mixed $query The query string or array.
69 *
70 * @return void
71 *
72 * @since 1.0
73 */
74 public function setQuery($query)
75 {
76 if (is_array($query))
77 {
78 $this->vars = $query;
79 }
80 else
81 {
82 if (strpos($query, '&') !== false)
83 {
84 $query = str_replace('&', '&', $query);
85 }
86
87 parse_str($query, $this->vars);
88 }
89
90 // Empty the query
91 $this->query = null;
92 }
93
94 /**
95 * Set URI scheme (protocol)
96 * ie. http, https, ftp, etc...
97 *
98 * @param string $scheme The URI scheme.
99 *
100 * @return void
101 *
102 * @since 1.0
103 */
104 public function setScheme($scheme)
105 {
106 $this->scheme = $scheme;
107 }
108
109 /**
110 * Set URI username.
111 *
112 * @param string $user The URI username.
113 *
114 * @return void
115 *
116 * @since 1.0
117 */
118 public function setUser($user)
119 {
120 $this->user = $user;
121 }
122
123 /**
124 * Set URI password.
125 *
126 * @param string $pass The URI password.
127 *
128 * @return void
129 *
130 * @since 1.0
131 */
132 public function setPass($pass)
133 {
134 $this->pass = $pass;
135 }
136
137 /**
138 * Set URI host.
139 *
140 * @param string $host The URI host.
141 *
142 * @return void
143 *
144 * @since 1.0
145 */
146 public function setHost($host)
147 {
148 $this->host = $host;
149 }
150
151 /**
152 * Set URI port.
153 *
154 * @param integer $port The URI port number.
155 *
156 * @return void
157 *
158 * @since 1.0
159 */
160 public function setPort($port)
161 {
162 $this->port = $port;
163 }
164
165 /**
166 * Set the URI path string.
167 *
168 * @param string $path The URI path string.
169 *
170 * @return void
171 *
172 * @since 1.0
173 */
174 public function setPath($path)
175 {
176 $this->path = $this->cleanPath($path);
177 }
178
179 /**
180 * Set the URI anchor string
181 * everything after the "#".
182 *
183 * @param string $anchor The URI anchor string.
184 *
185 * @return void
186 *
187 * @since 1.0
188 */
189 public function setFragment($anchor)
190 {
191 $this->fragment = $anchor;
192 }
193 }
194