Passed
Push — master ( a435d0...3e0df0 )
by Radu
02:08
created

RequestUrlTrait::queryToString()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
namespace WebServCo\Framework\Traits;
3
4
trait RequestUrlTrait
5
{
6
    abstract public function getHost();
7
    abstract public function getSchema();
8
9
    public function getSuffix()
10
    {
11
        return $this->suffix;
12
    }
13
14
    public function getTarget()
15
    {
16
        return $this->target;
17
    }
18
19
    public function getAppUrl()
20
    {
21
        if (\WebServCo\Framework\Framework::isCli()) {
22
            return false;
23
        }
24
        $url = sprintf(
25
            '%s://%s%s',
26
            $this->getSchema(),
27
            $this->getHost(),
28
            $this->path
29
        );
30
        return rtrim($url, '/') . '/';
31
    }
32
33
    public function getShortUrl()
34
    {
35
        $url = $this->getAppUrl();
36
        $url .= $this->getTarget();
37
        $url .= $this->getSuffix();
38
        return $url;
39
    }
40
41
    public function getUrl($removeParameters = [])
42
    {
43
        if (!is_array($removeParameters)) {
44
            throw new \InvalidArgumentException('Agument must be an array.');
45
        }
46
47
        $url = $this->getShortUrl();
48
        $query = $this->getQuery();
49
        foreach ($removeParameters as $item) {
50
            if (array_key_exists($item, $query)) {
51
                unset($query[$item]);
52
            }
53
        }
54
        $url .= \WebServCo\Framework\Utils\Arrays::toUrlQueryString($query);
55
        return $url;
56
    }
57
58
    public function getQuery()
59
    {
60
        return $this->query;
61
    }
62
63
    public function query($key, $defaultValue = false)
64
    {
65
        return \WebServCo\Framework\ArrayStorage::get(
66
            $this->query,
67
            $key,
68
            $defaultValue
69
        );
70
    }
71
}
72