UrlGenerator   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 176
c 0
b 0
f 0
wmc 10
lcom 1
cbo 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A extract() 0 8 3
A localize() 0 12 1
A setUrl() 0 23 3
A stripLocaleFromPath() 0 9 2
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
     * I18n service instance.
74
     *
75
     * @var I18nService
76
     */
77
    protected $i18n;
78
79
    /**
80
     * Class constructor.
81
     *
82
     * @param I18nService $i18n
83
     * @param string      $key
84
     */
85
    public function __construct(I18nService $i18n, string $key)
86
    {
87
        $this->i18n = $i18n;
88
        $this->key = $key;
89
    }
90
91
    /**
92
     * Extract URL information and format it
93
     * to fit our needs.
94
     *
95
     * @param mixed  $url
96
     * @param string $key
97
     * @param string $default
98
     * @param string $prefix
99
     * @param string $suffix
100
     *
101
     * @return string
102
     */
103
    private function extract($url, string $key, string $default = '', string $prefix = '', string $suffix = ''): string
104
    {
105
        if (empty($url)) {
106
            return $default;
107
        }
108
109
        return isset($url[$key]) ? $prefix.$url[$key].$suffix : $default;
110
    }
111
112
    /**
113
     * Localize parsed URL based on the given
114
     * locale object.
115
     *
116
     * @param \RichanFongdasen\I18n\Locale $locale
117
     *
118
     * @return string
119
     */
120
    public function localize(Locale $locale): string
121
    {
122
        $this->stripLocaleFromPath();
123
124
        $index = (int) $this->i18n->getConfig('locale_url_segment');
125
        array_splice($this->path, $index, 0, $locale->{$this->key});
126
127
        $path = implode('/', $this->path);
128
129
        return $this->scheme.$this->user.$this->pass.$this->host.
130
            $this->port.$path.$this->query.$this->fragment;
131
    }
132
133
    /**
134
     * Parse the given URL and extract all of
135
     * its information.
136
     *
137
     * @param string $url
138
     *
139
     * @return $this
140
     */
141
    public function setUrl(string $url): self
142
    {
143
        $url = parse_url($url);
144
145
        $this->scheme = $this->extract($url, 'scheme', '//', '', '://');
146
        $this->user = $this->extract($url, 'user');
147
        $this->pass = $this->extract($url, 'pass', '', ':', '@');
148
        $this->host = $this->extract($url, 'host');
149
        $this->port = $this->extract($url, 'port', '', ':');
150
        $this->path = explode('/', $this->extract($url, 'path', '/'));
151
        $this->query = $this->extract($url, 'query', '', '?');
152
        $this->fragment = $this->extract($url, 'fragment', '', '#');
153
154
        if (!$this->user) {
155
            $this->pass = '';
156
        }
157
158
        if (!$this->host) {
159
            $this->scheme = '';
160
        }
161
162
        return $this;
163
    }
164
165
    /**
166
     * Strip any locale keyword from the current
167
     * URL path.
168
     *
169
     * @return void
170
     */
171
    public function stripLocaleFromPath(): void
172
    {
173
        $index = (int) $this->i18n->getConfig('locale_url_segment');
174
        $locale = $this->i18n->getLocale($this->path[$index]);
175
176
        if ($locale instanceof Locale) {
177
            array_splice($this->path, $index, 1);
178
        }
179
    }
180
}
181