1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Patoui\Router; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Prophecy\Exception\Doubler\ClassNotFoundException; |
9
|
|
|
use Prophecy\Exception\Doubler\MethodNotFoundException; |
10
|
|
|
|
11
|
|
|
class Route implements Routable |
12
|
|
|
{ |
13
|
|
|
/* @var string */ |
14
|
|
|
private $httpVerb; |
15
|
|
|
|
16
|
|
|
/* @var string */ |
17
|
|
|
private $path; |
18
|
|
|
|
19
|
|
|
/* @var object */ |
20
|
|
|
private $classInstance; |
21
|
|
|
|
22
|
|
|
/* @var string */ |
23
|
|
|
private $classMethodName; |
24
|
|
|
|
25
|
|
|
public function __construct( |
26
|
|
|
string $httpVerb, |
27
|
|
|
string $path, |
28
|
|
|
object $classInstance, |
29
|
|
|
string $classMethodName |
30
|
|
|
) { |
31
|
|
|
if (! in_array($httpVerb, ['get', 'post'])) { |
32
|
|
|
throw new InvalidArgumentException( |
33
|
|
|
'Invalid http verb, must be: get or post' |
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if (! method_exists($classInstance, $classMethodName)) { |
38
|
|
|
throw new MethodNotFoundException( |
39
|
|
|
"Method '{$classMethodName}' not found on class '{$classInstance}'", |
40
|
|
|
$classInstance, |
41
|
|
|
$classMethodName |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->httpVerb = $httpVerb; |
46
|
|
|
$this->path = $path; |
47
|
|
|
$this->classInstance = $classInstance; |
48
|
|
|
$this->classMethodName = $classMethodName; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function getClassInstance() : object |
52
|
|
|
{ |
53
|
|
|
return $this->classInstance; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getClassMethodName() : string |
57
|
|
|
{ |
58
|
|
|
return $this->classMethodName; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getHttpVerb() : string |
62
|
|
|
{ |
63
|
|
|
return $this->httpVerb; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function isHttpVerbAndPathAMatch( |
67
|
|
|
string $httpVerb, |
68
|
|
|
string $path |
69
|
|
|
) : bool { |
70
|
|
|
return strtolower($this->getHttpVerb()) === strtolower($httpVerb) && |
71
|
|
|
trim($this->getPath(), '/') === trim($path, '/'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function getPath() : string |
75
|
|
|
{ |
76
|
|
|
return $this->path; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/* @return mixed */ |
80
|
|
|
public function resolve() |
81
|
|
|
{ |
82
|
|
|
return call_user_func( |
83
|
|
|
[$this->getClassInstance(), $this->getClassMethodName()] |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|