Url::withoutQueryAndFragment()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php declare(strict_types=1);
2
/**
3
 * Utils.
4
 *
5
 * @copyright Copyright (c) 2016 Starweb AB
6
 * @license   BSD 3-Clause
7
 */
8
9
namespace Starlit\Utils;
10
11
class Url
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $url;
17
18 11
    public function __construct(string $url)
19
    {
20 11
        $this->url = $url;
21 11
    }
22
23 4
    public function getPath(): ?string
24
    {
25 4
        $urlPath = parse_url($this->url, PHP_URL_PATH);
26
27 4
        return $urlPath === false ? null : $urlPath;
28
    }
29
30
    /**
31
     * @param string $newPath
32
     * @return string|self
33
     */
34 3
    public function replacePath(string $newPath)
35
    {
36 3
        if (!$this->url) {
37 1
            return $newPath;
38
        }
39
40 2
        $path = $this->getPath();
41 2
        if ($path !== null) {
42 1
            $this->url = Str::replaceFirst($path, $newPath, $this->url);
43
        }
44
45 2
        return $this;
46
    }
47
48 4
    public function getQuery(): ?string
49
    {
50 4
        $urlQuery = parse_url($this->url, PHP_URL_QUERY);
51
52 4
        return $urlQuery === false ? null : $urlQuery;
53
    }
54
55 3
    public function getFragment(): ?string
56
    {
57 3
        $urlFragment = parse_url($this->url, PHP_URL_FRAGMENT);
58
59 3
        return $urlFragment === false ? null : $urlFragment;
60
    }
61
62 3
    public function getQueryParameters(): array
63
    {
64 3
        $parameters = [];
65 3
        if (($query = $this->getQuery()) !== null) {
66 3
            parse_str($query, $parameters);
67
        }
68
69 3
        return $parameters;
70
    }
71
72 3
    public function withoutQueryAndFragment(): self
73
    {
74 3
        if (($pos = strpos($this->url, '?')) !== false) {
75 3
            $this->url = substr($this->url, 0, $pos);
76
        }
77
78 3
        return $this;
79
    }
80
81
    /**
82
     * @param array  $newParameters
83
     * @param bool   $merge If parameters should be merged (overwrite existing) or added (not overwriting existing)
84
     * @param string $argSeparator
85
     * @return self
86
     */
87 2
    public function addQueryParameters(array $newParameters = [], bool $merge = true, string $argSeparator = '&'): self
88
    {
89 2
        $currentParameters = $this->getQueryParameters();
90 2
        if ($merge) {
91 1
            $parameters = array_merge($currentParameters, $newParameters);
92
        } else {
93 1
            $parameters = $currentParameters + $newParameters;
94
        }
95
96 2
        $fragment = $this->getFragment();
97 2
        $this->withoutQueryAndFragment();
98
99 2
        if ($parameters) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $parameters of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
100 2
            $this->url .= '?' . http_build_query($parameters, '', $argSeparator);
101
        }
102
103 2
        if ($fragment) {
104 2
            $this->url .= '#' . urlencode($fragment);
105
        }
106
107 2
        return $this;
108
    }
109
110 4
    public function __toString(): string
111
    {
112 4
        return $this->url;
113
    }
114
}
115