Passed
Push — master ( 8cf222...7fa05a )
by Stephen
04:50
created

Url::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Sfneal\Dependencies\Utils;
4
5
class Url
6
{
7
    /**
8
     * @var string
9
     */
10
    protected $uri;
11
12
    /**
13
     * @var array
14
     */
15
    protected $params;
16
17
    /**
18
     * Url constructor.
19
     *
20
     * @param  string  $uri
21
     * @param  array|null  $params
22
     */
23
    public function __construct(string $uri, array $params = null)
24
    {
25
        $this->uri = $uri;
26
        $this->params = $params;
27
    }
28
29
    /**
30
     * Retrieve a URL with params appended to the query string.
31
     *
32
     * @return string
33
     */
34
    public function get(): string
35
    {
36
        return self::generateUrl($this->uri).self::generateQueryString($this->params);
37
    }
38
39
    /**
40
     * Generate a URL.
41
     *
42
     * @param  string  $uri
43
     * @return string
44
     */
45
    public static function generateUrl(string $uri): string
46
    {
47
        return 'https://'.$uri;
48
    }
49
50
    /**
51
     * Generate a query string.
52
     *
53
     * @param  array|null  $params
54
     * @return string
55
     */
56
    public static function generateQueryString(array $params = null): string
57
    {
58
        if (! is_null($params)) {
59
            $query = '?';
60
61
            $paramStrings = [];
62
            foreach (array_unique($params) as $key => $value) {
63
                $paramStrings[] = "{$key}={$value}";
64
            }
65
66
            return $query.implode('&', $paramStrings);
67
        }
68
69
        return '';
70
    }
71
}
72