|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2014 Krzysztof Magosa |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
6
|
|
|
* you may not use this file except in compliance with the License. |
|
7
|
|
|
* You may obtain a copy of the License at |
|
8
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
9
|
|
|
* |
|
10
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
11
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
12
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13
|
|
|
* See the License for the specific language governing permissions and |
|
14
|
|
|
* limitations under the License. |
|
15
|
|
|
*/ |
|
16
|
|
|
namespace KM\Saffron\UrlBuilder; |
|
17
|
|
|
|
|
18
|
|
|
use KM\Saffron\Exception\NoSuchRoute; |
|
19
|
|
|
|
|
20
|
|
|
abstract class Base |
|
21
|
|
|
{ |
|
22
|
|
|
protected $routes; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param string $name |
|
26
|
|
|
*/ |
|
27
|
|
|
protected function hasRoute($name) |
|
28
|
|
|
{ |
|
29
|
|
|
return isset($this->routes[$name]); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param string $name |
|
34
|
|
|
*/ |
|
35
|
|
|
protected function getRoute($name) |
|
36
|
|
|
{ |
|
37
|
|
|
if (!$this->hasRoute($name)) { |
|
38
|
|
|
throw new NoSuchRoute("There is no route $name."); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
return $this->routes[$name]; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param string $string |
|
46
|
|
|
* @param array $parameters |
|
47
|
|
|
* @return string |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function fillPlaceholders($string, array $parameters = []) |
|
50
|
|
|
{ |
|
51
|
|
|
foreach ($parameters as $name => $value) { |
|
52
|
|
|
$string = str_replace('{'.$name.'}', $value, $string); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $string; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param boolean $https |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function getScheme($https) |
|
63
|
|
|
{ |
|
64
|
|
|
if (null !== $https) { |
|
65
|
|
|
$scheme = $https ? 'https://' : 'http://'; |
|
66
|
|
|
} else { |
|
67
|
|
|
$scheme = 'http://'; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return $scheme; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param string $name |
|
75
|
|
|
* @param array $parameters |
|
76
|
|
|
* @param boolean $fullUrl |
|
77
|
|
|
*/ |
|
78
|
|
|
public function assemble($name, array $parameters = [], $fullUrl = false) |
|
79
|
|
|
{ |
|
80
|
|
|
$route = $this->getRoute($name); |
|
81
|
|
|
$values = array_replace($route['defaults'], $parameters); |
|
82
|
|
|
|
|
83
|
|
|
$link = ''; |
|
84
|
|
|
if ($fullUrl) { |
|
85
|
|
|
$link .= $this->getScheme($route['https']); |
|
86
|
|
|
$link .= $this->fillPlaceholders($route['domain'], $values); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$link .= $this->fillPlaceholders($route['uri'], $values); |
|
90
|
|
|
|
|
91
|
|
|
return $link; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|