• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

IlyasDeckers / ody-core / 13532154862

25 Feb 2025 10:24PM UTC coverage: 30.374% (+1.7%) from 28.706%
13532154862

push

github

web-flow
Update php.yml

544 of 1791 relevant lines covered (30.37%)

9.13 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

7.69
/src/Routing/RouteParser.php
1
<?php
2
declare(strict_types=1);
3

4
namespace Ody\Core\Routing;
5

6
use FastRoute\RouteParser\Std;
7
use InvalidArgumentException;
8
use Psr\Http\Message\UriInterface;
9
use Ody\Core\Interfaces\RouteCollectorInterface;
10
use Ody\Core\Interfaces\RouteParserInterface;
11

12
use function array_key_exists;
13
use function array_reverse;
14
use function http_build_query;
15
use function implode;
16
use function is_string;
17

18
class RouteParser implements RouteParserInterface
19
{
20
    private RouteCollectorInterface $routeCollector;
21

22
    private Std $routeParser;
23

24
    public function __construct(RouteCollectorInterface $routeCollector)
98✔
25
    {
26
        $this->routeCollector = $routeCollector;
98✔
27
        $this->routeParser = new Std();
98✔
28
    }
29

30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function relativeUrlFor(string $routeName, array $data = [], array $queryParams = []): string
×
34
    {
35
        $route = $this->routeCollector->getNamedRoute($routeName);
×
36
        $pattern = $route->getPattern();
×
37

38
        $segments = [];
×
39
        $segmentName = '';
×
40

41
        /*
42
         * $routes is an associative array of expressions representing a route as multiple segments
43
         * There is an expression for each optional parameter plus one without the optional parameters
44
         * The most specific is last, hence why we reverse the array before iterating over it
45
         */
46
        $expressions = array_reverse($this->routeParser->parse($pattern));
×
47
        foreach ($expressions as $expression) {
×
48
            foreach ($expression as $segment) {
×
49
                /*
50
                 * Each $segment is either a string or an array of strings
51
                 * containing optional parameters of an expression
52
                 */
53
                if (is_string($segment)) {
×
54
                    $segments[] = $segment;
×
55
                    continue;
×
56
                }
57

58
                /** @var string[] $segment */
59
                /*
60
                 * If we don't have a data element for this segment in the provided $data
61
                 * we cancel testing to move onto the next expression with a less specific item
62
                 */
63
                if (!array_key_exists($segment[0], $data)) {
×
64
                    $segments = [];
×
65
                    $segmentName = $segment[0];
×
66
                    break;
×
67
                }
68

69
                $segments[] = $data[$segment[0]];
×
70
            }
71

72
            /*
73
             * If we get to this logic block we have found all the parameters
74
             * for the provided $data which means we don't need to continue testing
75
             * less specific expressions
76
             */
77
            if (!empty($segments)) {
×
78
                break;
×
79
            }
80
        }
81

82
        if (empty($segments)) {
×
83
            throw new InvalidArgumentException('Missing data for URL segment: ' . $segmentName);
×
84
        }
85

86
        $url = implode('', $segments);
×
87
        if ($queryParams) {
×
88
            $url .= '?' . http_build_query($queryParams);
×
89
        }
90

91
        return $url;
×
92
    }
93

94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function urlFor(string $routeName, array $data = [], array $queryParams = []): string
×
98
    {
99
        $basePath = $this->routeCollector->getBasePath();
×
100
        $url = $this->relativeUrlFor($routeName, $data, $queryParams);
×
101

102
        if ($basePath) {
×
103
            $url = $basePath . $url;
×
104
        }
105

106
        return $url;
×
107
    }
108

109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function fullUrlFor(UriInterface $uri, string $routeName, array $data = [], array $queryParams = []): string
×
113
    {
114
        $path = $this->urlFor($routeName, $data, $queryParams);
×
115
        $scheme = $uri->getScheme();
×
116
        $authority = $uri->getAuthority();
×
117
        $protocol = ($scheme ? $scheme . ':' : '') . ($authority ? '//' . $authority : '');
×
118
        return $protocol . $path;
×
119
    }
120
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc