1 | <?php |
||
16 | class Route implements RouteInterface { |
||
17 | use HasMiddlewareTrait; |
||
18 | |||
19 | /** |
||
20 | * Allowed methods |
||
21 | * |
||
22 | * @var string[] |
||
23 | */ |
||
24 | protected $methods = []; |
||
25 | |||
26 | /** |
||
27 | * Route target |
||
28 | * |
||
29 | * @var ConditionInterface |
||
30 | */ |
||
31 | protected $target = null; |
||
32 | |||
33 | /** |
||
34 | * Route handler |
||
35 | * |
||
36 | * @var Handler |
||
37 | */ |
||
38 | protected $handler = null; |
||
39 | |||
40 | /** |
||
41 | * Constructor |
||
42 | * |
||
43 | * @throws Exception |
||
44 | * @param string[] $methods |
||
45 | * @param mixed $target |
||
46 | * @param string|\Closure $handler |
||
47 | */ |
||
48 | 3 | public function __construct( $methods, $target, $handler ) { |
|
49 | 3 | if ( ! is_a( $target, ConditionInterface::class ) ) { |
|
50 | try { |
||
51 | 2 | $target = Factory::make( $target ); |
|
52 | 2 | } catch ( InvalidRouteConditionException $e ) { |
|
53 | 1 | throw new Exception( 'Route target is not a valid route string or condition.' ); |
|
54 | } |
||
55 | 1 | } |
|
56 | |||
57 | 2 | $this->methods = $methods; |
|
58 | 2 | $this->target = $target; |
|
59 | 2 | $this->handler = new Handler( $handler ); |
|
60 | 2 | } |
|
61 | |||
62 | /** |
||
63 | * Get allowed methods |
||
64 | * |
||
65 | * @return string[] |
||
66 | */ |
||
67 | 1 | public function getMethods() { |
|
68 | 1 | return $this->methods; |
|
69 | } |
||
70 | |||
71 | /** |
||
72 | * Get target |
||
73 | * |
||
74 | * @return ConditionInterface |
||
75 | */ |
||
76 | 1 | public function getTarget() { |
|
77 | 1 | return $this->target; |
|
78 | } |
||
79 | |||
80 | /** |
||
81 | * Get handler |
||
82 | * |
||
83 | * @return Handler |
||
84 | */ |
||
85 | 1 | public function getHandler() { |
|
86 | 1 | return $this->handler; |
|
87 | } |
||
88 | |||
89 | /** |
||
90 | * {@inheritDoc} |
||
91 | */ |
||
92 | 2 | public function isSatisfied( Request $request ) { |
|
93 | 2 | if ( ! in_array( $request->getMethod(), $this->methods) ) { |
|
94 | 1 | return false; |
|
95 | } |
||
96 | 2 | return $this->target->isSatisfied( $request ); |
|
97 | } |
||
98 | |||
99 | /** |
||
100 | * {@inheritDoc} |
||
101 | */ |
||
102 | public function handle( Request $request, $view ) { |
||
108 | |||
109 | /** |
||
110 | * Add a rewrite rule to WordPress for url-based routes |
||
111 | * |
||
112 | * @throws Exception |
||
113 | * @param string $rewrite_to |
||
114 | * @return static $this |
||
115 | */ |
||
116 | public function rewrite( $rewrite_to ) { |
||
131 | } |
||
132 |