Completed
Push — master ( 22a15c...1f9f80 )
by Mikael
02:18
created

CUrl::setStaticSiteUrl()   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
    use \Anax\TConfigure;
12
13
14
15
    /**
16
     * Properties
17
     *
18
     */
19
    const URL_CLEAN  = 'clean';  // controller/action/param1/param2
20
    const URL_APPEND = 'append'; // index.php/controller/action/param1/param2
21
22
    private $urlType = self::URL_APPEND; // What type of urls to generate
23
24
    private $siteUrl = null; // Siteurl to prepend to all absolute urls created
25
    private $baseUrl = null; // Baseurl to prepend to all relative urls created
26
    private $scriptName = null; // Name of the frontcontroller script
27
28
29
    private $staticSiteUrl = null; // Siteurl to prepend to all absolute urls for assets
30
    private $staticBaseUrl = null; // Baseurl to prepend to all relative urls for assets
31
32
33
34
    /**
35
     * Set default values from configuration.
36
     *
37
     * @return this.
0 ignored issues
show
Documentation introduced by
The doc-type this. could not be parsed: Unknown type name "this." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
38
     */
39 17
    public function setDefaultsFromConfiguration()
40
    {
41 17
        $set = [
42
            "urlType",
43 2
            "siteUrl",
44 2
            "baseUrl",
45 2
            "staticSiteUrl",
46 2
            "staticBaseUrl",
47
            "scriptName",
48 15
        ];
49
        
50 3
        foreach ($set as $item) {
51
            if (!isset($this->config[$item])) {
52 12
                continue;
53
            }
54 4
            
55
            $this->$item = $this->config[$item];
56
        }
57
58 8
        return $this;
59 8
    }
60 4
61
62 4
63
    /**
64
     * Create an url and prepending the baseUrl.
65
     *
66
     * @param string $uri     part of uri to use when creating an url.
67
     *                        "" or null means baseurl to current
68
     *                        frontcontroller.
69
     * @param string $baseuri optional base to prepend uri.
70
     *
71
     * @return string as resulting url.
72
     */
73
    public function create($uri = null, $baseuri = null)
74
    {
75
        if (empty($uri) && empty($baseuri)) {
76
            // Empty uri means baseurl
77
            return $this->baseUrl
78
                . (($this->urlType == self::URL_APPEND)
79
                    ? "/$this->scriptName"
80
                    : null);
81
        } elseif (empty($uri)) {
82
            // Empty uri means baseurl with appended $baseuri
83
            ;
84
        } elseif (substr($uri, 0, 7) == "http://"
85
            || substr($uri, 0, 8) == "https://"
86
            || substr($uri, 0, 2) == "//"
87
        ) {
88
            // Fully qualified, just leave as is.
89
            return rtrim($uri, "/");
90
        } elseif ($uri[0] == "/") {
91
            // Absolute url, prepend with siteUrl
92
            return rtrim($this->siteUrl . rtrim($uri, '/'), '/');
93
        } elseif ($uri[0] == "#"
94
            || $uri[0] == "?"
95
        ) {
96
            // Hashtag url to local page, or query part, leave as is.
97
            return $uri;
98
        }
99
100
        // Prepend uri with baseuri
101
        $uri = rtrim($uri, "/");
102
        if (!empty($baseuri)) {
103
            $uri = rtrim($baseuri, "/") . "/$uri";
104
        }
105 8
106
        // Remove the trailing index part of the url
107 8
        if (basename($uri) == "index") {
108
            $uri = dirname($uri);
109
        }
110 8
111
        if ($this->urlType == self::URL_CLEAN) {
112 4
            return rtrim($this->baseUrl . "/" . $uri, "/");
113
        } else {
114 4
            return rtrim($this->baseUrl . "/" . $this->scriptName . "/" . $uri, "/");
115
        }
116 2
    }
117
118
119
120 2
    /**
121
     * Create an url and prepend the baseUrl to the directory of
122 2
     * the frontcontroller.
123 2
     *
124 2
     * @param string $uri part of uri to use when creating an url.
125
     *                    "" or null means baseurl to directory of
126
     *                    the current frontcontroller.
127
     *
128
     * @return string as resulting url.
129
     */
130
    public function createRelative($uri = null)
131
    {
132 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...
133
            // Empty uri means baseurl
134
            return $this->baseUrl;
135
        } elseif (substr($uri, 0, 7) == "http://"
136 17
            || substr($uri, 0, 8) == "https://"
137
            || substr($uri, 0, 2) == "//"
138 17
        ) {
139 17
            // Fully qualified, just leave as is.
140
            return rtrim($uri, '/');
141
        } elseif ($uri[0] == '/') {
142
            // Absolute url, prepend with siteUrl
143
            return rtrim($this->siteUrl . rtrim($uri, '/'), '/');
144
        }
145
146
        $uri = rtrim($uri, '/');
147
        return $this->baseUrl . '/' . $uri;
148
    }
149
150
151 18
152
    /**
153 18
     * Create an url for a static asset.
154 18
     *
155
     * @param string $uri part of uri to use when creating an url.
156
     *
157
     * @return string as resulting url.
158
     */
159
    public function asset($uri = null)
160
    {
161 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...
162
            // Allow empty
163
        } elseif (substr($uri, 0, 7) == "http://"
164
            || substr($uri, 0, 8) == "https://"
165
            || substr($uri, 0, 2) == "//"
166 4
        ) {
167
            // Fully qualified, just leave as is.
168 4
            return rtrim($uri, '/');
169 4
        } elseif ($uri[0] == '/') {
170
            // Absolute url, prepend with staticSiteUrl
171
            return rtrim($this->staticSiteUrl . rtrim($uri, '/'), '/');
172
        }
173
174
        $baseUrl = isset($this->staticBaseUrl)
175
            ? $this->staticBaseUrl
176
            : $this->baseUrl;
177
178
        return empty($uri)
179
            ? $baseUrl
180
            : $baseUrl . '/' . $uri;
181 4
    }
182
183 4
184 4
185
    /**
186
     * Set the siteUrl to prepend all absolute urls created.
187
     *
188
     * @param string $url part of url to use when creating an url.
189
     *
190
     * @return $this
191
     */
192
    public function setSiteUrl($url)
193
    {
194
        $this->siteUrl = rtrim($url, '/');
195
        return $this;
196 7
    }
197
198 7
199 7
200
    /**
201
     * Set the baseUrl to prepend all relative urls created.
202
     *
203
     * @param string $url part of url to use when creating an url.
204
     *
205
     * @return $this
206
     */
207
    public function setBaseUrl($url)
208
    {
209
        $this->baseUrl = rtrim($url, '/');
210
        return $this;
211 15
    }
212
213 15
214 1
215
    /**
216
     * Set the siteUrl to prepend absolute urls for assets.
217 14
     *
218 14
     * @param string $url part of url to use when creating an url.
219
     *
220
     * @return $this
221
     */
222
    public function setStaticSiteUrl($url)
223
    {
224
        $this->staticSiteUrl = rtrim($url, '/');
225
        return $this;
226
    }
227
228
229
230
    /**
231
     * Set the baseUrl to prepend relative urls for assets.
232
     *
233
     * @param string $url part of url to use when creating an url.
234
     *
235
     * @return $this
236
     */
237
    public function setStaticBaseUrl($url)
238
    {
239
        $this->staticBaseUrl = rtrim($url, '/');
240
        return $this;
241
    }
242
243
244
245
    /**
246
     * Set the scriptname to use when creating URL_APPEND urls.
247
     *
248
     * @param string $name as the scriptname, for example index.php.
249
     *
250
     * @return $this
251
     */
252
    public function setScriptName($name)
253
    {
254
        $this->scriptName = $name;
255
        return $this;
256
    }
257
258
259
260
    /**
261
     * Set the type of urls to be generated, URL_CLEAN, URL_APPEND.
262
     *
263
     * @param string $type what type of urls to create.
264
     *
265
     * @return $this
266
     */
267
    public function setUrlType($type)
268
    {
269
        if (!in_array($type, [self::URL_APPEND, self::URL_CLEAN])) {
270
            throw new \Exception("Unsupported Url type.");
271
        }
272
273
        $this->urlType = $type;
274
        return $this;
275
    }
276
277
278
279
    /**
280
     * Create a slug of a string, to be used as url.
281
     *
282
     * @param string $str the string to format as slug.
283
     *
284
     * @return str the formatted slug.
285
     */
286
    public function slugify($str)
287
    {
288
        $str = mb_strtolower(trim($str));
289
        $str = str_replace(array('å','ä','ö'), array('a','a','o'), $str);
290
        $str = preg_replace('/[^a-z0-9-]/', '-', $str);
291
        $str = trim(preg_replace('/-+/', '-', $str), '-');
292
        return $str;
293
    }
294
}
295