Completed
Pull Request — 3.x (#131)
by Hari
02:08
created

Url   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 1
dl 0
loc 65
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C __invoke() 0 22 7
1
<?php
2
/**
3
 *
4
 * This file is part of Aura for PHP.
5
 *
6
 * @license http://opensource.org/licenses/mit-license.php MIT
7
 *
8
 */
9
namespace Aura\Router\Helper;
10
11
use Aura\Router\Exception\RouteNotFound;
12
use Aura\Router\Generator;
13
14
/**
15
 *
16
 * Generic Url Helper class
17
 *
18
 * @package Aura.Router
19
 *
20
 */
21
class Url
22
{
23
    /**
24
     *
25
     * The Generator object used by the RouteContainer
26
     *
27
     * @var Generator
28
     *
29
     */
30
    protected $generator;
31
32
    /**
33
     *
34
     * Constructor.
35
     *
36
     * @param Generator $generator The generator object to use
37
     *
38
     */
39 7
    public function __construct(Generator $generator)
40
    {
41 7
        $this->generator = $generator;
42 7
    }
43
44
    /**
45
     *
46
     * Returns the Generator
47
     *
48
     * @param string $name The name of the route to lookup.
49
     *
50
     * @param array $data The data to pass into the route.
51
     *
52
     * @param array | string $queryData The data that is used to create query string.
53
     *
54
     * @param string $fragment The data to pass into the route.
55
     *
56
     * @param bool $returnRawUrl Whether or not to return the raw url.
57
     *
58
     * @return string The results of calling the appropriate _Generator_ method .
59
     *
60
     * @throws RouteNotFound When the route cannot be found.
61
     *
62
     */
63 7
    public function __invoke($name, array $data = [], $queryData = [], $fragment = '', $returnRawUrl = false)
64
    {
65
        $url = $returnRawUrl
66 7
            ? $this->generator->generateRaw($name, $data)
67 7
            : $this->generator->generate($name, $data);
68
69 7
        if (! empty($queryData)) {
70 4
            if (is_array($queryData)) {
71 2
                $url .= '?' . http_build_query($queryData);
72 2
            }
73
74 4
            if (is_string($queryData)) {
75 1
                $url .= '?' . ltrim($queryData, "?");
76 1
            }
77 4
        }
78
79 7
        if (is_string($fragment) && ! empty($fragment)) {
80 4
            $url .= '#' . ltrim($fragment, "#");
81 4
        }
82
83 7
        return $url;
84
    }
85
}
86