Completed
Push — master ( ec71aa...2c64ed )
by Ruben
01:09
created

UrlResolver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Spatie\LaravelEndpointResources;
4
5
use Illuminate\Routing\Exceptions\UrlGenerationException;
6
use Illuminate\Routing\Route;
7
use Illuminate\Routing\RouteUrlGenerator;
8
use Illuminate\Routing\UrlGenerator;
9
10
class UrlResolver extends RouteUrlGenerator
11
{
12
    public function __construct(UrlGenerator $urlGenerator)
13
    {
14
        parent::__construct($urlGenerator, $urlGenerator->getRequest());
15
16
        // The URL defaults are not given through automatically
17
        $this->defaults($urlGenerator->getDefaultParameters());
18
    }
19
20
    public function resolve(Route $route, array $parameters): string
21
    {
22
        try {
23
            return $this->to(
24
                $route,
25
                $this->url->formatParameters($parameters),
26
                true
27
            );
28
        } catch (UrlGenerationException $exception) {
29
            // Create an uri with missing parameters between brackets
30
            $domain = $this->getRouteDomain($route, $parameters);
31
32
            return $this->addQueryString($this->url->format(
33
                $root = $this->replaceRootParameters($route, $domain, $parameters),
34
                $this->replaceRouteParameters($route->uri(), $parameters),
35
                $route
36
            ), $parameters);
37
        }
38
    }
39
}
40