Completed
Push — master ( 9b981d...ec0b2f )
by Mikael
02:21
created

CUrl::setScriptName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 5
ccs 0
cts 0
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Anax\Url;
4
5
/**
6
 * A helper to create urls.
7
 *
8
 */
9
class CUrl
10
{
11
12
    /**
13
     * Properties
14
     *
15
     */
16
    const URL_CLEAN  = 'clean';  // controller/action/param1/param2
17
    const URL_APPEND = 'append'; // index.php/controller/action/param1/param2
18
19
    private $urlType = self::URL_APPEND; // What type of urls to generate
20
21
    private $siteUrl = null; // Siteurl to prepend to all absolute urls created
22
    private $baseUrl = null; // Baseurl to prepend to all relative urls created
23
    private $scriptName = null; // Name of the frontcontroller script
24
25
26
    private $staticSiteUrl = null; // Siteurl to prepend to all absolute urls for assets
27
    private $staticBaseUrl = null; // Baseurl to prepend to all relative urls for assets
28
29
30
31
    /**
32
     * Create an url and prepending the baseUrl.
33
     *
34
     * @param string $uri part of uri to use when creating an url. "" or null
35
     *                    means baseurl to current frontcontroller.
36
     *
37
     * @return string as resulting url.
38
     */
39 17
    public function create($uri = null)
40
    {
41 17
        if (empty($uri)) {
42
            // Empty uri means baseurl
43 2
            return $this->baseUrl
44 2
                . (($this->urlType == self::URL_APPEND)
45 2
                    ? "/$this->scriptName"
46 2
                    : null);
47
48 15
        } elseif (substr($uri, 0, 7) == "http://"
49
            || substr($uri, 0, 8) == "https://"
50 3
            || substr($uri, 0, 2) == "//"
51
        ) {
52 12
            // Fully qualified, just leave as is.
53
            return rtrim($uri, '/');
54 4
55
        } elseif ($uri[0] == '/') {
56
            // Absolute url, prepend with siteUrl
57
            return rtrim($this->siteUrl . rtrim($uri, '/'), '/');
58 8
59 8
        }
60 4
61
        $uri = rtrim($uri, '/');
62 4
        if (basename($uri) == "index") {
63
            $uri = dirname($uri);
64
        }
65
66
        if ($this->urlType == self::URL_CLEAN) {
67
            return $this->baseUrl . '/' . $uri;
68
        } else {
69
            return $this->baseUrl . '/' . $this->scriptName . '/' . $uri;
70
        }
71
    }
72
73
74
75
    /**
76
     * Create an url and prepend the baseUrl to the directory of the frontcontroller.
77
     *
78
     * @param string $uri part of uri to use when creating an url. "" or null means baseurl to
79
     * directory of the current frontcontroller.
80
     *
81
     * @return string as resulting url.
82
     */
83
    public function createRelative($uri = null)
84
    {
85 View Code Duplication
        if (empty($uri)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
            // Empty uri means baseurl
87
            return $this->baseUrl;
88
89
        } elseif (substr($uri, 0, 7) == "http://" || substr($uri, 0, 2) == "//") {
90
            // Fully qualified, just leave as is.
91
            return rtrim($uri, '/');
92
93
        } elseif ($uri[0] == '/') {
94
            // Absolute url, prepend with siteUrl
95
            return rtrim($this->siteUrl . rtrim($uri, '/'), '/');
96
97
        }
98
99
        $uri = rtrim($uri, '/');
100
        return $this->baseUrl . '/' . $uri;
101
    }
102
103
104
105 8
    /**
106
     * Create an url for a static asset.
107 8
     *
108
     * @param string $uri part of uri to use when creating an url.
109
     *
110 8
     * @return string as resulting url.
111
     */
112 4
    public function asset($uri = null)
113
    {
114 4 View Code Duplication
        if (empty($uri)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
115
            // Allow empty
116 2
117
        } elseif (substr($uri, 0, 7) == "http://" || substr($uri, 0, 2) == "//") {
118
            // Fully qualified, just leave as is.
119
            return rtrim($uri, '/');
120 2
121
        } elseif ($uri[0] == '/') {
122 2
            // Absolute url, prepend with staticSiteUrl
123 2
            return rtrim($this->staticSiteUrl . rtrim($uri, '/'), '/');
124 2
125
        }
126
127
        $baseUrl = isset($this->staticBaseUrl) ? $this->staticBaseUrl : $this->baseUrl;
128
129
        return empty($uri)
130
            ? $baseUrl
131
            : $baseUrl . '/' . $uri;
132
    }
133
134
135
136 17
    /**
137
     * Set the siteUrl to prepend all absolute urls created.
138 17
     *
139 17
     * @param string $url part of url to use when creating an url.
140
     *
141
     * @return $this
142
     */
143
    public function setSiteUrl($url)
144
    {
145
        $this->siteUrl = rtrim($url, '/');
146
        return $this;
147
    }
148
149
150
151 18
    /**
152
     * Set the baseUrl to prepend all relative urls created.
153 18
     *
154 18
     * @param string $url part of url to use when creating an url.
155
     *
156
     * @return $this
157
     */
158
    public function setBaseUrl($url)
159
    {
160
        $this->baseUrl = rtrim($url, '/');
161
        return $this;
162
    }
163
164
165
166 4
    /**
167
     * Set the siteUrl to prepend absolute urls for assets.
168 4
     *
169 4
     * @param string $url part of url to use when creating an url.
170
     *
171
     * @return $this
172
     */
173
    public function setStaticSiteUrl($url)
174
    {
175
        $this->staticSiteUrl = rtrim($url, '/');
176
        return $this;
177
    }
178
179
180
181 4
    /**
182
     * Set the baseUrl to prepend relative urls for assets.
183 4
     *
184 4
     * @param string $url part of url to use when creating an url.
185
     *
186
     * @return $this
187
     */
188
    public function setStaticBaseUrl($url)
189
    {
190
        $this->staticBaseUrl = rtrim($url, '/');
191
        return $this;
192
    }
193
194
195
196 7
    /**
197
     * Set the scriptname to use when creating URL_APPEND urls.
198 7
     *
199 7
     * @param string $name as the scriptname, for example index.php.
200
     *
201
     * @return $this
202
     */
203
    public function setScriptName($name)
204
    {
205
        $this->scriptName = $name;
206
        return $this;
207
    }
208
209
210
211 15
    /**
212
     * Set the type of urls to be generated, URL_CLEAN, URL_APPEND.
213 15
     *
214 1
     * @param string $type what type of urls to create.
215
     *
216
     * @return $this
217 14
     */
218 14
    public function setUrlType($type)
219
    {
220
        if (!in_array($type, [self::URL_APPEND, self::URL_CLEAN])) {
221
            throw new \Exception("Unsupported Url type.");
222
        }
223
224
        $this->urlType = $type;
225
        return $this;
226
    }
227
}
228