RouteUri.php
1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
namespace Illuminate\Routing;
class RouteUri
{
/**
* The route URI.
*
* @var string
*/
public $uri;
/**
* The fields that should be used when resolving bindings.
*
* @var array
*/
public $bindingFields = [];
/**
* Create a new route URI instance.
*
* @param string $uri
* @param array $bindingFields
* @return void
*/
public function __construct(string $uri, array $bindingFields = [])
{
$this->uri = $uri;
$this->bindingFields = $bindingFields;
}
/**
* Parse the given URI.
*
* @param string $uri
* @return static
*/
public static function parse($uri)
{
preg_match_all('/\{([\w\:]+?)\??\}/', $uri, $matches);
$bindingFields = [];
foreach ($matches[0] as $match) {
if (strpos($match, ':') === false) {
continue;
}
$segments = explode(':', trim($match, '{}?'));
$bindingFields[$segments[0]] = $segments[1];
$uri = strpos($match, '?') !== false
? str_replace($match, '{'.$segments[0].'?}', $uri)
: str_replace($match, '{'.$segments[0].'}', $uri);
}
return new static($uri, $bindingFields);
}
}