RouteBuilder::stringifyValue()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 12
c 0
b 0
f 0
nc 5
nop 1
dl 0
loc 19
ccs 13
cts 13
cp 1
crap 5
rs 9.5555
1
<?php
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Nekhay <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Nekhay
8
 * @license https://github.com/sunrise-php/http-router/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-router
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sunrise\Http\Router\Helper;
15
16
use BackedEnum;
0 ignored issues
show
Bug introduced by
The type BackedEnum 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 InvalidArgumentException;
18
use Stringable;
19
20
use function get_debug_type;
21
use function is_int;
22
use function is_string;
23
use function sprintf;
24
use function str_replace;
25
26
/**
27
 * @since 3.0.0
28
 */
29
final class RouteBuilder
30
{
31
    /**
32
     * @param array<string, mixed> $values Values for the route's variables.
33
     *
34
     * @throws InvalidArgumentException
35
     */
36 16
    public static function buildRoute(string $route, array $values = []): string
37
    {
38 16
        $variables = RouteParser::parseRoute($route);
39
40 16
        $search = [];
41 16
        $replace = [];
42 16
        foreach ($variables as $variable) {
43 15
            if (isset($values[$variable['name']])) {
44
                try {
45 12
                    $value = self::stringifyValue($values[$variable['name']]);
46 3
                } catch (InvalidArgumentException $e) {
47 3
                    throw new InvalidArgumentException(sprintf(
48 3
                        'The route "%s" could not be built due to an invalid value for the variable {%s}: %s',
49 3
                        $route,
50 3
                        $variable['name'],
51 3
                        $e->getMessage(),
52 3
                    ));
53
                }
54
55 9
                $search[] = $variable['statement'];
56 9
                $replace[] = $value;
57 9
                continue;
58
            }
59
60 3
            if (isset($variable['optional_part'])) {
61 1
                $search[] = $variable['optional_part'];
62 1
                $replace[] = '';
63 1
                continue;
64
            }
65
66 2
            throw new InvalidArgumentException(sprintf(
67 2
                'The route "%s" could not be built because the required value for the variable {%s} is missing.',
68 2
                $route,
69 2
                $variable['name'],
70 2
            ));
71
        }
72
73
        // will be replaced by an empty string:
74
        // https://github.com/php/php-src/blob/a04577fb4ab5e1ebc7779608523b95ddf01e6c7f/ext/standard/string.c#L4406-L4408
75 11
        $search[] = '(';
76 11
        $search[] = ')';
77
78 11
        return str_replace($search, $replace, $route);
79
    }
80
81
    /**
82
     * Tries to cast the given value to the string type
83
     *
84
     * @throws InvalidArgumentException
85
     */
86 12
    private static function stringifyValue(mixed $value): string
87
    {
88 12
        if (is_string($value)) {
89 6
            return $value;
90
        }
91 6
        if (is_int($value)) {
92 1
            return (string) $value;
93
        }
94 5
        if ($value instanceof BackedEnum) {
95 1
            return (string) $value->value;
96
        }
97 4
        if ($value instanceof Stringable) {
98 1
            return (string) $value;
99
        }
100
101 3
        throw new InvalidArgumentException(sprintf(
102 3
            'The "%s" value could not be converted to a string; ' .
103 3
            'supported types are: string, integer, backed enum and stringable object.',
104 3
            get_debug_type($value),
105 3
        ));
106
    }
107
}
108