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