|
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
|
|
|
|