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\Collector; |
16
|
|
|
|
17
|
|
|
use Phossa2\Route\Status; |
18
|
|
|
use Phossa2\Route\Message\Message; |
19
|
|
|
use Phossa2\Route\Exception\LogicException; |
20
|
|
|
use Phossa2\Route\Interfaces\RouteInterface; |
21
|
|
|
use Phossa2\Route\Interfaces\ResultInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* CollectorPPR |
25
|
|
|
* |
26
|
|
|
* Parameter Pairs Routing (PPR) |
27
|
|
|
* |
28
|
|
|
* Using parameter and value pairs like the following |
29
|
|
|
* |
30
|
|
|
* ``` |
31
|
|
|
* http://servername/path/index.php/controller/action/id/1/name/nick |
32
|
|
|
* ``` |
33
|
|
|
* |
34
|
|
|
* @package Phossa2\Route |
35
|
|
|
* @author Hong Zhang <[email protected]> |
36
|
|
|
* @see CollectorAbstract |
37
|
|
|
* @version 2.0.0 |
38
|
|
|
* @since 2.0.0 added |
39
|
|
|
*/ |
40
|
|
|
class CollectorPPR extends CollectorAbstract |
41
|
|
|
{ |
42
|
|
|
/** |
43
|
|
|
* {@inheritDoc} |
44
|
|
|
*/ |
45
|
|
|
public function addRoute(RouteInterface $route) |
46
|
|
|
{ |
47
|
|
|
throw new LogicException( |
48
|
|
|
Message::get(Message::RTE_ROUTE_DISALLOWED, get_called_class()), |
49
|
|
|
Message::RTE_ROUTE_DISALLOWED |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Parameter Pairs Routing (PPR) |
55
|
|
|
* |
56
|
|
|
* /path/index.php/controller/action/id/1/name/nick |
57
|
|
|
* |
58
|
|
|
* {@inheritDoc} |
59
|
|
|
*/ |
60
|
|
|
protected function match(ResultInterface $result)/*# : bool */ |
61
|
|
|
{ |
62
|
|
|
$parts = explode('/', trim($result->getPath(), '/')); |
63
|
|
|
if (count($parts) > 1) { |
64
|
|
|
return $this->processParts($parts, $result); |
65
|
|
|
} |
66
|
|
|
$result->setStatus(Status::BAD_REQUEST); |
67
|
|
|
return false; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* |
72
|
|
|
* @param array $parts |
73
|
|
|
* @access protected |
74
|
|
|
*/ |
75
|
|
|
protected function processParts( |
76
|
|
|
array $parts, |
77
|
|
|
ResultInterface $result |
78
|
|
|
)/*# : bool */ { |
79
|
|
|
if (count($parts) % 2) { |
80
|
|
|
$result->setStatus(Status::BAD_REQUEST); |
81
|
|
|
return false; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$result->setStatus(Status::OK) |
85
|
|
|
->setHandler($this->retrieveHandler($parts)) |
86
|
|
|
->setParameters($this->retrieveParams($parts)); |
87
|
|
|
return true; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Retrieve controller, action pair |
92
|
|
|
* |
93
|
|
|
* @param array $data |
94
|
|
|
* @return array |
95
|
|
|
* @access protected |
96
|
|
|
*/ |
97
|
|
|
protected function retrieveHandler(array &$data)/*# : array */ |
98
|
|
|
{ |
99
|
|
|
$controller = array_shift($data); |
100
|
|
|
$action = array_shift($data); |
101
|
|
|
return [$controller, $action]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Retrieve pair of params |
106
|
|
|
* |
107
|
|
|
* @param array $data |
108
|
|
|
* @return array |
109
|
|
|
* @access protected |
110
|
|
|
*/ |
111
|
|
|
protected function retrieveParams(array $data)/*# : array */ |
112
|
|
|
{ |
113
|
|
|
$params = []; |
114
|
|
|
foreach ($data as $i => $val) { |
115
|
|
|
if (0 === $i % 2) { |
116
|
|
|
$params[$val] = $data[$i + 1]; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
return $params; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|