AddRouteTrait::addRoute()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
nc 1
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Route
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Route\Traits;
16
17
use Phossa2\Route\Route;
18
use Phossa2\Route\Interfaces\RouteInterface;
19
use Phossa2\Route\Interfaces\AddRouteInterface;
20
21
/**
22
 * AddRouteTrait
23
 *
24
 * Implementation of AddRouteInterface
25
 *
26
 * @package Phossa2\Route
27
 * @author  Hong Zhang <[email protected]>
28
 * @see     AddRouteInterface
29
 * @version 2.0.0
30
 * @since   2.0.0 added
31
 */
32
trait AddRouteTrait
33
{
34
    /**
35
     * {@inheritDoc}
36
     */
37
    abstract public function addRoute(RouteInterface $route);
38
39
    /**
40
     * {@inheritDoc}
41
     */
42
    public function loadRoutes(array $routes)
43
    {
44
        foreach ($routes as $pattern => $definition) {
45
            $method  = $definition[0];
46
            $handler = $definition[1];
47
            $default = isset($definition[2]) ? $definition[2] : [];
48
            $this->addRoute(new Route($method, $pattern, $handler, $default));
49
        }
50
        return $this;
51
    }
52
53
    /**
54
     * {@inheritDoc}
55
     */
56
    public function addGet(
57
        /*# string */ $pattern,
58
        $handler,
59
        array $defaultValues = []
60
    ) {
61
        return $this->addRoute(
62
            new Route('GET,HEAD', $pattern, $handler, $defaultValues)
63
        );
64
    }
65
66
    /**
67
     * {@inheritDoc}
68
     */
69
    public function addPost(
70
        /*# string */ $pattern,
71
        $handler,
72
        array $defaultValues = []
73
    ) {
74
        return $this->addRoute(
75
            new Route('POST', $pattern, $handler, $defaultValues)
76
        );
77
    }
78
}
79