Passed
Pull Request — main (#8)
by Jesús
02:11
created

Controller::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
require_once \dirname(__DIR__) . '/vendor/autoload.php';
6
7
use Gacela\Router\Entities\Request;
8
use Gacela\Router\Router;
9
use Gacela\Router\Routes;
10
11
12
# To run this example locally, you can run in your terminal:
13
# $ composer serve
14
15
class Controller
16
{
17
    public function __construct(
18
        private Request $request
19
    ) {
20
    }
21
22
    public function __invoke(): string
23
    {
24
        $number = $this->request->get('number');
25
26
        if (!empty($number)) {
27
            return "__invoke with GET 'number'={$number}";
28
        }
29
30
        return '__invoke';
31
    }
32
33
    public function customAction(int $number = 0): string
34
    {
35
        return "customAction(number: {$number})";
36
    }
37
}
38
39
Router::configure(static function (Routes $routes): void {
40
    # Try it out: http://localhost:8081/docs
41
    $routes->redirect('docs', 'https://gacela-project.com/');
42
43
    # Try it out: http://localhost:8081?number=456
44
    $routes->get('/', Controller::class);
45
46
    # Try it out: http://localhost:8081/custom/123
47
    $routes->get('custom/{number}', Controller::class, 'customAction');
48
49
    # Try it out: http://localhost:8081/custom
50
    $routes->any('custom', Controller::class);
51
});
52