Passed
Push — master ( 5b6f7f...57f3ff )
by Melech
03:58
created

Url::isInternalUri()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 23
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
dl 0
loc 23
rs 10
c 1
b 0
f 0
cc 3
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Valkyrja\Http\Routing\Url;
15
16
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Valkyrja\Http\Routing\Collection\Contract\Collection;
18
use Valkyrja\Http\Routing\Exception\InvalidRouteNameException;
19
use Valkyrja\Http\Routing\Url\Contract\Url as Contract;
20
21
use function str_replace;
22
23
/**
24
 * Class Url.
25
 *
26
 * @author Melech Mizrachi
27
 */
28
class Url implements Contract
29
{
30
    /**
31
     * Url constructor.
32
     */
33
    public function __construct(
34
        protected Collection $collection,
35
    ) {
36
    }
37
38
    /**
39
     * @inheritDoc
40
     *
41
     * @throws InvalidRouteNameException
42
     */
43
    #[Override]
44
    public function getUrl(string $name, array|null $data = null): string
45
    {
46
        // Get the matching route
47
        $route = $this->collection->getByName($name);
48
49
        if ($route === null) {
50
            throw new InvalidRouteNameException("$name is not a valid named route");
51
        }
52
53
        // Get the path from the generator
54
        $path = $route->getPath();
55
56
        // If any data was passed
57
        if ($data !== null) {
58
            // Iterate through the data and replace it in the path
59
            foreach ($data as $datumName => $datum) {
60
                $path = str_replace('{' . $datumName . '}', (string) $datum, $path);
61
            }
62
        }
63
64
        return $path;
65
    }
66
}
67