-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouteMethod.php
More file actions
90 lines (76 loc) · 1.87 KB
/
RouteMethod.php
File metadata and controls
90 lines (76 loc) · 1.87 KB
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
<?php
require_once 'RouteMethodInterface.php';
class RouteMethod implements RouteMethodInterface
{
private $requestMethod;
private $requestUri;
private $routeString;
private $callback;
static $requestMethods = ['POST', 'GET'];
public function __construct( string $routeString, Closure $callback )
{
$this->setRouteString( $routeString );
$this->setCallback( $callback );
$this->requestMethod = $_SERVER['REQUEST_METHOD'];
$this->requestUri = $_SERVER['REQUEST_URI'];
}
/**
* @param Closure $callBackFunction
*/
public function setCallback( Closure $callBackFunction ): void
{
$this->callback = $callBackFunction;
}
/**
* @param string $routeString
*/
public function setRouteString( string $routeString ): void
{
$this->routeString = trim( $routeString );
}
/**
* @param string $requestMethod
*/
public function setRequestMethod( string $requestMethod ): void
{
try {
if ( !in_array( $requestMethod, self::$requestMethods ) ) {
throw new InvalidArgumentException( 'Request method is not valid' );
} else {
$this->requestMethod = $requestMethod;
}
} catch ( InvalidArgumentException $exception ) {
echo $exception->getMessage();
}
}
/**
* @param string $uri
* @param Closure $callback
* @return mixed
*/
public function get( string $uri, Closure $callback )
{
return $this->request( 'GET', $uri, $callback );
}
/**
* @param string $uri
* @param Closure $callback
* @return mixed
*/
public function post( string $uri, Closure $callback )
{
return $this->request( 'POST', $uri, $callback );
}
/**
* @param string $method
* @param string $URI
* @param $callback
* @return mixed
*/
protected function request( string $method, string $URI, $callback )
{
if ( $this->requestMethod == $method && $URI == $this->requestUri ) {
return call_user_func( $this->callback );
}
}
}