summaryrefslogtreecommitdiff
path: root/src/Concerns/CreatesGroups.php
blob: ae178a03217ee152dcd0a5645d7e72496f965bc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php

declare(strict_types=1);

namespace Lightscale\Router\Concerns;

use Lightscale\Router\Contracts\Middleware;
use Lightscale\Router\Group;
use Lightscale\Router\GroupDefinition;

trait CreatesGroups
{
    public function prefix(string $value): Group
    {
        return new Group(
            router: $this->getRouter(),
            parent: $this->getGroup(),
            prefix: $value,
        );
    }

    public function name(string $value): Group
    {
        return new Group(
            router: $this->getRouter(),
            parent: $this->getGroup(),
            name: $value,
        );
    }

    /** @param Middleware|Middleware[] $value */
    public function middleware(Middleware|array $value): Group
    {
        $value = is_array($value) ? $value : [$value];

        return new Group(
            router: $this->getRouter(),
            parent: $this->getGroup(),
            middleware: $value,
        );
    }

    /** @param callable(GroupDefinition): void $cb */
    public function group(callable $cb): Group
    {
        $group = new Group(
            router: $this->getRouter(),
            parent: $this->getGroup(),
        );
        ($cb)(new GroupDefinition(
            $group
        ));

        return $group;
    }
}