Completed
Push — master ( 0f92a6...d02b5c )
by Richan
01:33
created

UrlGenerator::stripLocaleFromPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
namespace RichanFongdasen\I18n;
4
5
class UrlGenerator
6
{
7
    /**
8
     * URL scheme, ie: https://.
9
     *
10
     * @var string
11
     */
12
    protected $scheme;
13
14
    /**
15
     * Basic HTTP Authentication
16
     * User information.
17
     *
18
     * @var string
19
     */
20
    protected $user;
21
22
    /**
23
     * Basic HTTP Authentication
24
     * Password information.
25
     *
26
     * @var string
27
     */
28
    protected $pass;
29
30
    /**
31
     * Hostname.
32
     *
33
     * @var string
34
     */
35
    protected $host;
36
37
    /**
38
     * HTTP port number.
39
     *
40
     * @var string
41
     */
42
    protected $port;
43
44
    /**
45
     * Request path.
46
     *
47
     * @var array
48
     */
49
    protected $path;
50
51
    /**
52
     * Query string.
53
     *
54
     * @var string
55
     */
56
    protected $query;
57
58
    /**
59
     * Fragment identifyer.
60
     *
61
     * @var string
62
     */
63
    protected $fragment;
64
65
    /**
66
     * Default language key.
67
     *
68
     * @var string
69
     */
70
    protected $key;
71
72
    /**
73
     * Class constructor.
74
     *
75
     * @param string $key
76
     */
77
    public function __construct($key)
78
    {
79
        $this->key = $key;
80
    }
81
82
    /**
83
     * Extract URL information and format it
84
     * to fit our needs.
85
     *
86
     * @param mixed  $url
87
     * @param string $key
88
     * @param string $default
89
     * @param string $prefix
90
     * @param string $suffix
91
     *
92
     * @return string
93
     */
94
    private function extract($url, $key, $default = '', $prefix = '', $suffix = '')
95
    {
96
        if (empty($url)) {
97
            return $default;
98
        }
99
100
        return isset($url[$key]) ? $prefix.$url[$key].$suffix : $default;
101
    }
102
103
    /**
104
     * Localize parsed URL based on the given
105
     * locale object.
106
     *
107
     * @param \RichanFongdasen\I18n\Locale $locale
108
     *
109
     * @return string
110
     */
111
    public function localize(Locale $locale)
112
    {
113
        $this->stripLocaleFromPath();
114
115
        $index = (int) \I18n::getConfig('locale_url_segment');
116
        array_splice($this->path, $index, 0, $locale->{$this->key});
117
118
        $path = implode('/', $this->path);
119
120
        return $this->scheme.$this->user.$this->pass.$this->host.
121
            $this->port.$path.$this->query.$this->fragment;
122
    }
123
124
    /**
125
     * Parse the given URL and extract all of
126
     * its information.
127
     *
128
     * @param string $url
129
     */
130
    public function setUrl($url)
131
    {
132
        $url = parse_url($url);
133
134
        $this->scheme = $this->extract($url, 'scheme', '//', '', '://');
135
        $this->user = $this->extract($url, 'user');
136
        $this->pass = $this->extract($url, 'pass', '', ':', '@');
137
        $this->host = $this->extract($url, 'host');
138
        $this->port = $this->extract($url, 'port', '', ':');
139
        $this->path = explode('/', $this->extract($url, 'path', '/'));
140
        $this->query = $this->extract($url, 'query', '', '?');
141
        $this->fragment = $this->extract($url, 'fragment', '', '#');
142
143
        if (!$this->user) {
144
            $this->pass = '';
145
        }
146
147
        if (!$this->host) {
148
            $this->scheme = '';
149
        }
150
151
        return $this;
152
    }
153
154
    /**
155
     * Strip any locale keyword from the current
156
     * URL path.
157
     *
158
     * @return void
159
     */
160
    public function stripLocaleFromPath()
161
    {
162
        $index = (int) \I18n::getConfig('locale_url_segment');
163
        $locale = \I18n::getLocale($this->path[$index]);
164
165
        if ($locale instanceof Locale) {
166
            array_splice($this->path, $index, 1);
167
        }
168
    }
169
}
170