Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.67% covered (success)
91.67%
11 / 12
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
UrlRouteDefinition
91.67% covered (success)
91.67%
11 / 12
80.00% covered (warning)
80.00%
4 / 5
8.04
0.00% covered (danger)
0.00%
0 / 1
 convert
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 withBaseUrl
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getPlaceholders
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 withFilledInPlaceholders
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 toStringList
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2namespace Apie\Core\ValueObjects;
3
4use Apie\Core\Attributes\Description;
5use Apie\Core\Lists\StringList;
6use Apie\Core\ValueObjects\Interfaces\StringValueObjectInterface;
7
8#[Description('Represents a url route definition with placeholders, for example "/test/{id}"')]
9class UrlRouteDefinition implements StringValueObjectInterface
10{
11    use IsStringValueObject;
12
13    protected function convert(string $input): string
14    {
15        if (substr($input, 0, 1) !== '/') {
16            return '/' . $input;
17        }
18        return $input;
19    }
20
21    public function withBaseUrl(string $baseUrl): self
22    {
23        return new self(rtrim($baseUrl, '/') . $this->internal);
24    }
25
26    /**
27     * @return array<int, string>
28     */
29    public function getPlaceholders(): array
30    {
31        if (preg_match_all('/\{\s*(?<placeholder>[a-zA-Z0-9_]+)\s*\}/', $this->internal, $matches)) {
32            return $matches['placeholder'];
33        }
34        return [];
35    }
36
37    /**
38     * @param array<string, string> $values
39     * @return self
40     */
41    public function withFilledInPlaceholders(array $values): self
42    {
43        $result = $this->internal;
44        foreach ($values as $key => $value) {
45            $result = preg_replace('/\{\s*' . preg_quote($key, '/') . '\s*\}/', $value, $result);
46        }
47        return new self($result);
48    }
49
50    public function toStringList(): StringList
51    {
52        return new StringList(explode('/', substr($this->internal, 1)));
53    }
54}