This repository was archived by the owner on Feb 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResolverAggregator.php
162 lines (148 loc) · 4.05 KB
/
ResolverAggregator.php
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
//
// +---------------------------------------------------------------------+
// | CODE INC. SOURCE CODE |
// +---------------------------------------------------------------------+
// | Copyright (c) 2018 - Code Inc. SAS - All Rights Reserved. |
// | Visit https://www.codeinc.fr for more information about licensing. |
// +---------------------------------------------------------------------+
// | NOTICE: All information contained herein is, and remains the |
// | property of Code Inc. SAS. The intellectual and technical concepts |
// | contained herein are proprietary to Code Inc. SAS are protected by |
// | trade secret or copyright law. Dissemination of this information or |
// | reproduction of this material is strictly forbidden unless prior |
// | written permission is obtained from Code Inc. SAS. |
// +---------------------------------------------------------------------+
//
// Author: Joan Fabrégat <[email protected]>
// Date: 08/10/2018
// Project: Router
//
declare(strict_types=1);
namespace CodeInc\Router\Resolvers;
use CodeInc\Router\Exceptions\NotAResolverException;
/**
* Class HandlerResolverAggregator
*
* @package CodeInc\Router\Resolvers
* @author Joan Fabrégat <[email protected]>
*/
class ResolverAggregator implements ResolverInterface, \Countable, \Iterator
{
/**
* @var ResolverInterface[]
*/
private $resolvers = [];
/**
* @var int
*/
private $iteratorPosition = 0;
/**
* HandlerResolverAggregator constructor.
*
* @param iterable|null $resolvers
*/
public function __construct(?iterable $resolvers = null)
{
if ($resolvers !== null) {
$this->addResolvers($resolvers);
}
}
/**
* Adds a resolver.
*
* @param ResolverInterface $resolver
*/
public function addResolver(ResolverInterface $resolver):void
{
$this->resolvers[] = $resolver;
}
/**
* Adds multiple resolvers. Only object implementing ResolverInterface will be added.
*
* @param iterable|ResolverInterface[] $resolvers
*/
public function addResolvers(iterable $resolvers):void
{
foreach ($resolvers as $resolver) {
if (!$resolver instanceof ResolverInterface) {
throw new NotAResolverException($resolver);
}
$this->addResolver($resolver);
}
}
/**
* @inheritdoc
* @param string $route
* @return string|null
*/
public function getControllerClass(string $route):?string
{
foreach ($this->resolvers as $resolver) {
if (($handlerClass = $resolver->getControllerClass($route)) !== null) {
return $handlerClass;
}
}
return null;
}
/**
* @inheritdoc
* @param string $controllerClass
* @return string|null
*/
public function getControllerRoute(string $controllerClass):?string
{
foreach ($this->resolvers as $resolver) {
if (($route = $resolver->getControllerRoute($controllerClass)) !== null) {
return $route;
}
}
return null;
}
/**
* @inheritdoc
*/
public function rewind():void
{
$this->iteratorPosition = 0;
}
/**
* @inheritdoc
*/
public function next():void
{
$this->iteratorPosition++;
}
/**
* @inheritdoc
* @return bool
*/
public function valid():bool
{
return array_key_exists($this->iteratorPosition, $this->resolvers);
}
/**
* @inheritdoc
* @return ResolverInterface
*/
public function current():ResolverInterface
{
return $this->resolvers[$this->iteratorPosition];
}
/**
* @inheritdoc
* @return int
*/
public function key():int
{
return $this->iteratorPosition;
}
/**
* @inheritdoc
* @return int
*/
public function count():int
{
return count($this->resolvers);
}
}