Passed
Push — main ( dd4eea...91afa7 )
by Dimitri
03:22
created

SampleURIGenerator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
c 0
b 0
f 0
dl 0
loc 40
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 12 2
A __construct() 0 3 1
1
<?php
2
3
/**
4
 * This file is part of Blitz PHP framework.
5
 *
6
 * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace BlitzPHP\Cli\Commands\Routes;
13
14
use BlitzPHP\Container\Services;
15
use BlitzPHP\Router\RouteCollection;
16
17
/**
18
 * Générez un exemple de chemin d'URI à partir de l'expression régulière de la clé de route.
19
 */
20
final class SampleURIGenerator
21
{
22
    private RouteCollection $routes;
23
24
    /**
25
     * Exemple de chemin URI pour l'espace réservé.
26
     *
27
     * @var array<string, string>
28
     */
29
    private array $samples = [
30
        'any'      => '123/abc',
31
        'segment'  => 'abc_123',
32
        'alphanum' => 'abc123',
33
        'num'      => '123',
34
        'alpha'    => 'abc',
35
        'hash'     => 'abc_123',
36
    ];
37
38
    public function __construct(?RouteCollection $routes = null)
39
    {
40
        $this->routes = $routes ?? Services::routes();
41
    }
42
43
    /**
44
     * @param string $routeKey clé de routage regex
45
     *
46
     * @return string exemple de chemin URI
47
     */
48
    public function get(string $routeKey): string
49
    {
50
        $sampleUri = $routeKey;
51
52
        foreach ($this->routes->getPlaceholders() as $placeholder => $regex) {
53
            $sample = $this->samples[$placeholder] ?? '::unknown::';
54
55
            $sampleUri = str_replace('(' . $regex . ')', $sample, $sampleUri);
56
        }
57
58
        // auto route
59
        return str_replace('[/...]', '/1/2/3/4/5', $sampleUri);
60
    }
61
}
62