Completed
Push — prototype ( 3bfdd7...aec713 )
by Peter
07:47
created

Route::setLiteral()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Webino (http://webino.sk)
4
 *
5
 * @link        https://github.com/webino for the canonical source repository
6
 * @copyright   Copyright (c) 2015-2017 Webino, s.r.o. (http://webino.sk)
7
 * @author      Peter Bačinský <[email protected]>
8
 * @license     BSD-3-Clause
9
 */
10
11
namespace WebinoConfigLib\Feature;
12
13
use WebinoConfigLib\Router\Route as RouteConfig;
14
use WebinoConfigLib\Router\RouteInterface;
15
16
/**
17
 * Class Route
18
 */
19
class Route extends AbstractFeature implements
20
    RouteInterface
21
{
22
    /**
23
     * Router config key
24
     */
25
    const ROUTER = 'router';
26
27
    /**
28
     * Routes config key
29
     */
30
    const ROUTES = 'routes';
31
32
    /**
33
     * @var RouteConfig
34
     */
35
    private $route;
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function __construct($name = null)
41
    {
42
        parent::__construct();
43
        $this->route = (new RouteConfig)->setName($name);
44
    }
45
46
    /**
47
     * @param string $route Literal route
48
     * @return $this
49
     */
50
    public function setLiteral($route)
51
    {
52
        $this->setPath($route)->setType($this::LITERAL);
53
        return $this;
54
    }
55
56
    /**
57
     * @param string $route Segment route
58
     * @return $this
59
     */
60
    public function setSegment($route)
61
    {
62
        $this->setPath($route)->setType($this::SEGMENT);
63
        return $this;
64
    }
65
66
    /**
67
     * @param string $type Route type
68
     * @return $this
69
     */
70
    public function setType($type = RouteInterface::LITERAL)
71
    {
72
        $this->getRoute()->setType($type);
73
        return $this;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function getName()
80
    {
81
        return $this->getRoute()->getName();
82
    }
83
84
    /**
85
     * @param string $name Route name
86
     * @return $this
87
     */
88
    public function setName($name)
89
    {
90
        $this->getRoute()->setName($name);
91
        return $this;
92
    }
93
94
    /**
95
     * @param bool $mayTerminate
96
     * @return $this
97
     */
98
    public function setMayTerminate($mayTerminate = true)
99
    {
100
        $this->getRoute()->setMayTerminate($mayTerminate);
101
        return $this;
102
    }
103
104
    /**
105
     * @param array $defaults
106
     * @return $this
107
     */
108
    public function setDefaults(array $defaults)
109
    {
110
        $this->getRoute()->setDefaults($defaults);
111
        return $this;
112
    }
113
114
    /**
115
     * @param self[] $routes
116
     * @return $this
117
     */
118
    public function setChild(array $routes)
119
    {
120
        foreach ($routes as $route) {
121
            if ($route instanceof self) {
122
                $this->getRoute()->setChild([$route->getRoute()]);
123
            }
124
        }
125
        return $this;
126
    }
127
128
    /**
129
     * @param RouteInterface[] $routes
130
     * @return $this
131
     */
132
    public function chain(array $routes)
133
    {
134
        $this->getRoute()->chain($this->resolveRoutes($routes));
135
        return $this;
136
    }
137
138
    /**
139
     * @return RouteConfig
140
     */
141
    public function getRoute()
142
    {
143
        return $this->route;
144
    }
145
146
    /**
147
     * Set route path
148
     *
149
     * @param string|array $path Route path
150
     * @return $this
151
     */
152
    public function setPath($path)
153
    {
154
        $this->route->setPath($path);
155
        return $this;
156
    }
157
158
    /**
159
     * @return array
160
     */
161
    protected function createRoutes()
162
    {
163
        return $this->route->hasName()
164
            ? [$this->route->getName() => $this->route->toArray()]
165
            : [$this->route->toArray()];
166
    }
167
168
    /**
169
     * @param RouteInterface[] $routes
170
     * @return array
171
     */
172
    protected function resolveRoutes(array $routes)
173
    {
174
        $resolved = [];
175
        foreach ($routes as $route) {
176
            if ($route instanceof self) {
177
                $resolved[] = $route->getRoute();
178
            }
179
        }
180
        return $resolved;
181
    }
182
183
    /**
184
     * @return array
185
     */
186
    public function toArray()
187
    {
188
        $this->mergeArray([self::ROUTER => [self::ROUTES => $this->createRoutes()]]);
189
        return parent::toArray();
190
    }
191
}
192