Completed
Branch master (e25020)
by Richan
03:06
created

UrlGenerator::setUrl()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
c 0
b 0
f 0
rs 9.0856
cc 3
eloc 15
nc 4
nop 1
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 string
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
     * Localize parsed URL based on the given
84
     * locale object.
85
     *
86
     * @param \RichanFongdasen\I18n\Locale $locale
87
     *
88
     * @return string
89
     */
90
    public function localize(Locale $locale)
91
    {
92
        $this->path = '/'.$locale->{$this->key}.$this->path;
93
94
        return $this->scheme.$this->user.$this->pass.$this->host.
95
            $this->port.$this->path.$this->query.$this->fragment;
96
    }
97
98
    /**
99
     * Parse the given URL and extract all of
100
     * its information.
101
     *
102
     * @param string $url
103
     */
104
    public function setUrl($url)
105
    {
106
        $url = parse_url($url);
107
108
        $this->scheme = $this->extract($url, 'scheme', '//', '', '://');
109
        $this->user = $this->extract($url, 'user');
110
        $this->pass = $this->extract($url, 'pass', '', ':', '@');
111
        $this->host = $this->extract($url, 'host');
112
        $this->port = $this->extract($url, 'port', '', ':');
113
        $this->path = $this->extract($url, 'path', '/');
114
        $this->query = $this->extract($url, 'query', '', '?');
115
        $this->fragment = $this->extract($url, 'fragment', '', '#');
116
117
        if (!$this->user) {
118
            $this->pass = '';
119
        }
120
121
        if (!$this->host) {
122
            $this->scheme = '';
123
        }
124
125
        return $this;
126
    }
127
128
    /**
129
     * Extract URL information and format it
130
     * to fit our needs.
131
     *
132
     * @param mixed  $url
133
     * @param string $key
134
     * @param string $default
135
     * @param string $prefix
136
     * @param string $suffix
137
     *
138
     * @return string
139
     */
140
    private function extract($url, $key, $default = '', $prefix = '', $suffix = '')
141
    {
142
        if (empty($url)) {
143
            return $default;
144
        }
145
146
        return isset($url[$key]) ? $prefix.$url[$key].$suffix : $default;
147
    }
148
}
149