Test Failed
Push — 1.0.0-dev ( 7e4712...a1a2d0 )
by nguereza
02:53
created

Url   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 59
c 4
b 1
f 0
dl 0
loc 136
rs 10
wmc 22

7 Methods

Rating   Name   Duplication   Size   Complexity  
A appUrl() 0 12 3
A mainUrl() 0 5 2
A __construct() 0 2 1
A domain() 0 24 6
A queryString() 0 2 1
A addSuffixInPath() 0 14 4
A ugly() 0 15 3
1
<?php
2
    defined('ROOT_PATH') or exit('Access denied');
3
    /**
4
     * TNH Framework
5
     *
6
     * A simple PHP framework using HMVC architecture
7
     *
8
     * This content is released under the MIT License (MIT)
9
     *
10
     * Copyright (c) 2017 TNH Framework
11
     *
12
     * Permission is hereby granted, free of charge, to any person obtaining a copy
13
     * of this software and associated documentation files (the "Software"), to deal
14
     * in the Software without restriction, including without limitation the rights
15
     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
     * copies of the Software, and to permit persons to whom the Software is
17
     * furnished to do so, subject to the following conditions:
18
     *
19
     * The above copyright notice and this permission notice shall be included in all
20
     * copies or substantial portions of the Software.
21
     *
22
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
     * SOFTWARE.
29
     */
30
31
    class Url extends BaseClass{
32
33
        /**
34
         * Construct new instance
35
         */
36
        public function __construct() {
37
            parent::__construct();
38
        }
39
        
40
        /**
41
         * Return the link using "base_url" config without front controller "index.php"
42
         * @param  string $path the link path or full URL
43
         * @return string the full link URL
44
         */
45
        public function mainUrl($path = '') {
46
            if (is_url($path)) {
47
                return $path;
48
            }
49
            return get_config('base_url') . $path;
50
        }
51
52
        /**
53
         * Return the link using "base_url" config with front controller "index.php"
54
         * @param  string $path the link path or full URL
55
         * @return string the full link URL
56
         */
57
        public function appUrl($path = '') {
58
            if (is_url($path)) {
59
                return $path;
60
            }
61
            $path = rtrim($path, '/');
62
            $url = get_config('base_url');
63
            $frontController = get_config('front_controller');
64
            if ($frontController) {
65
                $url .= $frontController . '/';
66
            }
67
            $path = $this->addSuffixInPath($path);
68
            return $url . $path;
69
        }
70
71
        /**
72
         * Return the current site URL
73
         * @return string
74
         */
75
        public function current() {
76
            $current = '/';
77
            $requestUri = get_instance()->request->requestUri();
78
            if ($requestUri) {
79
                $current = $requestUri;
80
            }
81
            return $this->domain() . $current;
82
        }
83
84
        /**
85
         * Generate a friendly  text to use in link (slugs)
86
         * @param  string  $str       the title or text to use to get the friendly text
87
         * @param  string  $separator the caracters separator
88
         * @param  boolean $lowercase whether to set the final text to lowe case or not
89
         * @return string the friendly generated text
90
         */
91
        public function ugly($str = null, $separator = '-', $lowercase = true) {
92
            $str  = trim($str);
93
            $from = array('ç', 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'à', 'á', 'â', 'ã', 'ä', 'å', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'È', 'É', 'Ê', 'Ë', 'è', 'é', 'ê', 'ë', 'Ç', 'ç', 'Ì', 'Í', 'Î', 'Ï', 'ì', 'í', 'î', 'ï', 'Ù', 'Ú', 'Û', 'Ü', 'ù', 'ú', 'û', 'ü', 'ÿ', 'Ñ', 'ñ');
94
            $to   = array('c', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'o', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'y', 'n', 'n');
95
            $str  = str_replace($from, $to, $str);
96
            $str  = preg_replace('#([^a-z0-9]+)#i', $separator, $str);
97
            $str  = str_replace('--', $separator, $str);
98
            //if after process we get something like one-two-three-, need truncate the last separator "-"
99
            if (substr($str, -1) == $separator) {
100
                $str = substr($str, 0, -1);
101
            }
102
            if ($lowercase) {
103
                $str = strtolower($str);
104
            }
105
            return $str;
106
        }
107
108
        /**
109
         * Get the current application domain with protocol
110
         * @return string the domain name
111
         */
112
        public function domain() {
113
            $domain = 'localhost';
114
            $protocol = 'http';
115
            if (is_https()) {
116
                $protocol = 'https';
117
            }
118
119
            $domainServerVars = array(
120
                'HTTP_HOST',
121
                'SERVER_NAME',
122
                'SERVER_ADDR'
123
            );
124
            foreach ($domainServerVars as $var) {
125
                $value = get_instance()->request->server($var);
126
                if ($value) {
127
                    $domain = $value;
128
                    break;
129
                }
130
            }
131
	    $port = get_instance()->request->server('SERVER_PORT');
132
            if ($port && !in_array($port, array(80, 443))) {
133
                $domain .= ':' . $port;
134
            }
135
            return $protocol . '://' . $domain;
136
        }
137
138
        /**
139
         * Get the current request query string
140
         * @return string
141
         */
142
        public function queryString() {
143
            return get_instance()->request->server('QUERY_STRING');
144
        }
145
146
        /**
147
         * Add configured suffixe in the path
148
         * @param string $path the path
149
         *
150
         * @return string the final path after add suffix if configured
151
         * otherwise the same value will be returned
152
         */
153
        protected function addSuffixInPath($path){
154
            $suffix = get_config('url_suffix');
155
            if ($suffix && $path) {
156
                if (strpos($path, '?') !== false) {
157
                    $query    = explode('?', $path);
158
                    $query[0] = str_ireplace($suffix, '', $query[0]);
159
                    $query[0] = rtrim($query[0], '/');
160
                    $query[0] .= $suffix;
161
                    $path     = implode('?', $query);
162
                } else {
163
                    $path .= $suffix;
164
                }
165
            }
166
            return $path;
167
        }
168
169
    }
170