|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mpociot\ApiDoc\Tools; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
use Illuminate\Routing\Route; |
|
7
|
|
|
|
|
8
|
|
|
class Utils |
|
9
|
|
|
{ |
|
10
|
|
|
public static function getFullUrl(Route $route, array $bindings = []): string |
|
11
|
|
|
{ |
|
12
|
|
|
$uri = $route->uri(); |
|
13
|
|
|
|
|
14
|
|
|
return self::replaceUrlParameterBindings($uri, $bindings); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public static function getRouteActionUses(array $action): ?array |
|
18
|
|
|
{ |
|
19
|
|
|
if ($action['uses'] !== null) { |
|
20
|
|
|
if (is_array($action['uses'])) { |
|
21
|
|
|
return $action['uses']; |
|
22
|
|
|
} elseif (is_string($action['uses'])) { |
|
23
|
|
|
return explode('@', $action['uses']); |
|
24
|
|
|
} |
|
25
|
|
|
} |
|
26
|
|
|
if (array_key_exists(0, $action) && array_key_exists(1, $action)) { |
|
27
|
|
|
return [ |
|
28
|
|
|
0 => $action[0], |
|
29
|
|
|
1 => $action[1], |
|
30
|
|
|
]; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
return null; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Transform parameters in URLs into real values (/users/{user} -> /users/2). |
|
38
|
|
|
* Uses bindings specified by caller, otherwise just uses '1'. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $uri |
|
41
|
|
|
* @param array $bindings |
|
42
|
|
|
* |
|
43
|
|
|
* @return mixed |
|
44
|
|
|
*/ |
|
45
|
|
|
protected static function replaceUrlParameterBindings(string $uri, array $bindings) |
|
46
|
|
|
{ |
|
47
|
|
|
foreach ($bindings as $path => $binding) { |
|
48
|
|
|
// So we can support partial bindings like |
|
49
|
|
|
// 'bindings' => [ |
|
50
|
|
|
// 'foo/{type}' => 4, |
|
51
|
|
|
// 'bar/{type}' => 2 |
|
52
|
|
|
//], |
|
53
|
|
|
if (Str::is("*$path*", $uri)) { |
|
54
|
|
|
preg_match('/({.+?})/', $path, $parameter); |
|
55
|
|
|
$uri = str_replace("{$parameter['1']}", $binding, $uri); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
// Replace any unbound parameters with '1' |
|
59
|
|
|
$uri = preg_replace('/{(.+?)}/', 1, $uri); |
|
60
|
|
|
|
|
61
|
|
|
return $uri; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|