Passed
Pull Request — master (#60)
by
unknown
10:40
created

Route::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
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
namespace Yiisoft\Router\Route;
6
7
use Yiisoft\Http\Method;
8
use Yiisoft\Router\Dispatcher\DispatcherAwareInterface;
9
use Yiisoft\Router\Dispatcher\DispatcherAwareTrait;
10
use Yiisoft\Router\Dispatcher\DispatcherInterface;
11
use Yiisoft\Router\Handler\HandlerAwareTrait;
12
13
/**
14
 * Class Route
15
 *
16
 * @method static DispatcherAwareInterface withDispatcher(DispatcherInterface $dispatcher)
17
 */
18
final class Route implements RouteInterface
19
{
20
    use DispatcherAwareTrait;
21
    use HandlerAwareTrait;
22
23
    private DefinitionInterface $definition;
24
25
    private function __construct(DefinitionInterface $definition)
26
    {
27
        $this->definition = $definition;
28
    }
29
30
    public function serialize(): string
31
    {
32
        // TODO: Implement serialize() method.
33
        return '';
34
    }
35
36
    public function unserialize($serialized)
37
    {
38
        // TODO: Implement unserialize() method.
39
    }
40
41
    public function getDefinition(): DefinitionInterface
42
    {
43
        return $this->definition;
44
    }
45
46
    // TODO: Move all HTTP method factories to separate trait to make easier defining
47
    // own RouteInterface implementation and not writing all these methods again
48
    public static function get(string $path, $handler = null): self
49
    {
50
        return self::methods([Method::GET], $path, $handler);
51
    }
52
53
    public static function post(string $path, $handler = null): self
54
    {
55
        return self::methods([Method::POST], $path, $handler);
56
    }
57
58
    public static function put(string $path, $handler = null): self
59
    {
60
        return self::methods([Method::PUT], $path, $handler);
61
    }
62
63
    public static function delete(string $path, $handler = null): self
64
    {
65
        return self::methods([Method::DELETE], $path, $handler);
66
    }
67
68
    public static function patch(string $path, $handler = null): self
69
    {
70
        return self::methods([Method::PATCH], $path, $handler);
71
    }
72
73
    public static function head(string $path, $handler = null): self
74
    {
75
        return self::methods([Method::HEAD], $path, $handler);
76
    }
77
78
    public static function options(string $path, $handler = null): self
79
    {
80
        return self::methods([Method::OPTIONS], $path, $handler);
81
    }
82
83
    public static function anyMethod(string $path, $handler = null): self
84
    {
85
        return self::methods(Method::ANY, $path, $handler);
86
    }
87
88
    public static function methods(array $methods, string $path, $handler = null): self
89
    {
90
        // TODO: Do we need to validate methods? If yes, we do it here or in definition
91
        $definition = new Definition($path, $methods);
92
93
        return (new self($definition))->handler($handler);
94
    }
95
96
    public function name(string $name): self
97
    {
98
        $route = clone $this;
99
        $route->definition = $route->definition->withName($name);
100
101
        return $route;
102
    }
103
104
    // TODO: add other methods to build definition
105
}
106