Router::parseCustomRoutes()   B
last analyzed

Complexity

Conditions 7
Paths 4

Size

Total Lines 25
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 25
rs 8.8333
c 0
b 0
f 0
cc 7
nc 4
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WebServCo\Framework\Libraries;
6
7
use WebServCo\Framework\Objects\Route;
8
9
final class Router extends \WebServCo\Framework\AbstractLibrary
10
{
11
    public function getFourOhfourRoute(): Route
12
    {
13
        // Check if we have a 404 route
14
        $fourOhfourRoute = $this->setting('404_route', []);
15
        if (!isset($fourOhfourRoute[1])) {
16
            // No 404 route found, throw 404 exception.
17
            throw new \WebServCo\Framework\Exceptions\NotFoundException('The requested resource was not found.');
18
        }
19
        return new Route(
20
            $fourOhfourRoute[0],
21
            $fourOhfourRoute[1],
22
            \WebServCo\Framework\Helpers\ArrayHelper::get($fourOhfourRoute, 2, []),
23
        );
24
    }
25
26
    /**
27
    * @param array<string,string> $routes
28
    * @param array<int,string> $extraArgs
29
    */
30
    public function getRoute(string $requestCustom, array $routes, array $extraArgs = []): Route
31
    {
32
        $routeString = $this->parseCustomRoutes($requestCustom, $routes);
33
        if (!$routeString || 'index' === $routeString) {
34
            $defaultRoute = $this->setting('default_route', []);
35
            if (!isset($defaultRoute[1])) {
36
                throw new \WebServCo\Framework\Exceptions\NotFoundException("Default route missing or not valid.");
37
            }
38
            return new Route(
39
                $defaultRoute[0],
40
                $defaultRoute[1],
41
                \WebServCo\Framework\Helpers\ArrayHelper::get($defaultRoute, 2, []),
42
            );
43
        }
44
45
        $parts = \explode('/', $routeString, 3);
46
47
        $class = \WebServCo\Framework\Helpers\ArrayHelper::get($parts, 0, false);
48
        $method = \WebServCo\Framework\Helpers\ArrayHelper::get($parts, 1, false);
49
        if (!$class || !$method) {
50
            // Route is invalid
51
            // Return 404 route
52
            // throws \WebServCo\Framework\Exceptions\NotFoundException
53
            return $this->getFourOhfourRoute();
54
        }
55
56
        $args = \array_key_exists(2, $parts)
57
            ? \explode('/', $parts[2])
58
            : [];
59
60
        foreach ($extraArgs as $k => $v) {
61
            $args[] = $v;
62
        }
63
64
        return new Route($class, $method, $args);
65
    }
66
67
    /**
68
    * @param array<string,string> $routes
69
    */
70
    private function parseCustomRoutes(string $requestCustom, array $routes): string
71
    {
72
        if (\is_array($routes)) {
0 ignored issues
show
introduced by
The condition is_array($routes) is always true.
Loading history...
73
            foreach ($routes as $k => $v) {
74
                 $k = \str_replace(['{num}', '{any}'], ['[0-9]+', '.+'], $k);
75
                 /**
76
                  * Check for a custom route match.
77
                  */
78
                if (!\preg_match("#^{$k}$#", $requestCustom) && !\preg_match("#^{$k}/$#", $requestCustom)) {
79
                    continue;
80
                }
81
82
                /**
83
                 * Check for back references.
84
                 */
85
                if (false !== \strpos($v, '$') && false !== \strpos($k, '(')) {
86
                    /**
87
                     * Parse request.
88
                     */
89
                     $v = \preg_replace("#^{$k}$#", $v, $requestCustom);
90
                }
91
                return (string) $v;
92
            }
93
        }
94
        return $requestCustom;
95
    }
96
}
97