|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Limoncello\Core\Routing\Traits; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Copyright 2015-2020 [email protected] |
|
7
|
|
|
* |
|
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
9
|
|
|
* you may not use this file except in compliance with the License. |
|
10
|
|
|
* You may obtain a copy of the License at |
|
11
|
|
|
* |
|
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
13
|
|
|
* |
|
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
17
|
|
|
* See the License for the specific language governing permissions and |
|
18
|
|
|
* limitations under the License. |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
use function strlen; |
|
22
|
|
|
use function substr; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @package Limoncello\Core |
|
26
|
|
|
*/ |
|
27
|
|
|
trait UriTrait |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @param string $uri |
|
31
|
|
|
* @param bool $trailingSlash |
|
32
|
|
|
* |
|
33
|
|
|
* @return string |
|
34
|
|
|
*/ |
|
35
|
22 |
|
protected function normalizeUri(string $uri, bool $trailingSlash): string |
|
36
|
|
|
{ |
|
37
|
|
|
// add starting '/' and cut ending '/' if necessary |
|
38
|
22 |
|
$uri = strlen($uri) > 0 && $uri[0] === '/' ? $uri : '/' . $uri; |
|
39
|
22 |
|
$prefixLen = strlen($uri); |
|
40
|
22 |
|
$uri = $prefixLen > 1 && substr($uri, -1) === '/' ? substr($uri, 0, $prefixLen - 1) : $uri; |
|
41
|
|
|
|
|
42
|
|
|
// feature: trailing slashes are possible when asked |
|
43
|
22 |
|
$uri = $trailingSlash === true && substr($uri, -1) !== '/' ? $uri . '/' : $uri; |
|
44
|
|
|
|
|
45
|
22 |
|
return $uri; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param string $uri1 |
|
50
|
|
|
* @param string $uri2 |
|
51
|
|
|
* |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
18 |
|
protected function concatUri(string $uri1, string $uri2): string |
|
55
|
|
|
{ |
|
56
|
18 |
|
$fEndsWithSlash = strlen($uri1) > 0 && substr($uri1, -1) === '/'; |
|
57
|
18 |
|
$sStartsWithSlash = strlen($uri2) > 0 && $uri2[0] === '/'; |
|
58
|
|
|
|
|
59
|
|
|
// only one has '/' |
|
60
|
18 |
|
if ($fEndsWithSlash xor $sStartsWithSlash) { |
|
61
|
17 |
|
return $uri1 . $uri2; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
// either both have '/' nor both don't have |
|
65
|
|
|
|
|
66
|
17 |
|
$result = $fEndsWithSlash === true ? $uri1 . substr($uri2, 1) : $uri1 . '/' . $uri2; |
|
67
|
|
|
|
|
68
|
17 |
|
return $result; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|