Url::internal()   B
last analyzed

Complexity

Conditions 8
Paths 72

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 8.1475
c 0
b 0
f 0
cc 8
nc 72
nop 3
1
<?php
2
3
namespace FFMVC\Helpers;
4
5
/**
6
 * URL Helper Class
7
 *
8
 * @package helpers
9
 * @author Vijay Mahrra <[email protected]>
10
 * @copyright (c) Copyright 2016 Vijay Mahrra
11
 * @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html)
12
 */
13
class Url extends \Prefab
14
{
15
    /**
16
     * Build and return a URL
17
     * converting 2nd argument $params to querystring if array
18
     *
19
     * @param string $url name of the url alias
20
     * @param mixed $params url params as string or array
21
     * @param boolean $https force it to be https?
22
     * @return string
23
     */
24
    public static function external(string $url, $params = null, bool $https = true): string
25
    {
26
        if (!empty($params)) {
27
28
            if (is_array($params)) {
29
                $params = http_build_query($params);
30
            }
31
32
            if (!empty($params)) {
33
                $url .= '?' . $params;
34
            }
35
        }
36
37
        if (!empty($https)) {
38
39
            $p = \UTF::instance()->strpos($url, 'http://');
40
41
            if ($p !== false) {
42
                $url = 'https://' . \UTF::instance()->substr($url, 7);
43
            }
44
        }
45
46
        return $url;
47
    }
48
49
    /**
50
     * Return an f3 URL route by its alias or the given path
51
     * converting 2nd argument $params to querystring if array
52
     *
53
     * @param string $url name of the url alias if beginning with @ or relative
54
     *
55
     * @param mixed $params url params as string or array
56
     * @param boolean $full default true
57
     * @return string
58
     */
59
    public static function internal(string $url, $params = null, bool $full = true): string
60
    {
61
        $f3 = \Base::instance();
62
63
        if ('@' == $url[0]) {
64
            $url = $f3->alias(\UTF::instance()->substr($url,1));
65
        }
66
67
        // missing slash at start of url path
68
        if ('/' !== \UTF::instance()->substr($url, 0, 1)) {
69
            $url = '/' . $url;
70
        }
71
72
        if (!empty($params)) {
73
74
            $session_name = strtolower(session_name());
75
76
            if (array_key_exists($session_name, $params)) {
77
                unset($params[$session_name]);
78
            }
79
80
            if (is_array($params)) {
81
                $params = http_build_query($params);
82
            }
83
84
            if (!empty($params)) {
85
                $url .= '?' . $params;
86
            }
87
88
        }
89
90
        return empty($full) ? $url : $f3->get('SCHEME') . '://' . $f3->get('HOST') . $url;
91
    }
92
93
}
94