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

eliashaeussler / composer-package-template / 12861990819

20 Jan 2025 01:05AM UTC coverage: 99.502%. Remained the same
12861990819

push

github

web-flow
[TASK] Update all dependencies

400 of 402 relevant lines covered (99.5%)

3.5 hits per line

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

100.0
/src/Helper/UriHelper.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the Composer package "eliashaeussler/composer-package-template".
7
 *
8
 * Copyright (C) 2023-2025 Elias Häußler <elias@haeussler.dev>
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation, either version 3 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22
 */
23

24
namespace EliasHaeussler\ComposerPackageTemplate\Helper;
25

26
use Psr\Http\Message;
27

28
use function ltrim;
29
use function preg_replace_callback;
30
use function rtrim;
31

32
/**
33
 * UriHelper.
34
 *
35
 * @author Elias Häußler <elias@haeussler.dev>
36
 * @license GPL-3.0-or-later
37
 */
38
final class UriHelper
39
{
40
    /**
41
     * @param non-empty-string                          $path
42
     * @param array<non-empty-string, non-empty-string> $parameters
43
     */
44
    public static function mergePath(
2✔
45
        Message\UriInterface $uri,
46
        string $path,
47
        array $parameters = [],
48
    ): Message\UriInterface {
49
        $mergedPath = rtrim($uri->getPath(), '/').'/'.ltrim($path, '/');
2✔
50

51
        if ([] !== $parameters) {
2✔
52
            $mergedPath = self::interpolatePathParameters(urldecode($mergedPath), $parameters);
1✔
53
        }
54

55
        return $uri->withPath($mergedPath);
2✔
56
    }
57

58
    /**
59
     * @param non-empty-string                                    $path
60
     * @param non-empty-array<non-empty-string, non-empty-string> $parameters
61
     */
62
    public static function interpolatePathParameters(string $path, array $parameters): string
2✔
63
    {
64
        return preg_replace_callback(
2✔
65
            '/\{(\w+)}/',
2✔
66
            static fn (array $matches): string => $parameters[$matches[1]] ?? '',
2✔
67
            $path,
2✔
68
        ) ?? $path;
2✔
69
    }
70

71
    /**
72
     * @param array<string, mixed> $queryParams
73
     */
74
    public static function mergeQueryParams(Message\UriInterface $uri, array $queryParams): Message\UriInterface
1✔
75
    {
76
        parse_str($uri->getQuery(), $query);
1✔
77

78
        $mergedQueryParams = array_replace_recursive($query, $queryParams);
1✔
79
        $queryString = http_build_query($mergedQueryParams);
1✔
80

81
        return $uri->withQuery($queryString);
1✔
82
    }
83
}
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

© 2025 Coveralls, Inc