/
Route.php
88 lines (80 loc) · 2.15 KB
/
Route.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
<?php
namespace Lucinda\STDERR\Application;
use Lucinda\MVC\ConfigurationException;
use Lucinda\MVC\Response\HttpStatus;
use Lucinda\STDERR\ErrorType;
/**
* Encapsulates a route that matches a handled exception
*/
class Route extends \Lucinda\MVC\Application\Route
{
private ?HttpStatus $httpStatus = null;
private ?ErrorType $errorType = null;
/**
* Detects route info from <exception> tag
*
* @param \SimpleXMLElement $info
* @throws ConfigurationException
*/
public function __construct(\SimpleXMLElement $info)
{
parent::__construct($info);
$this->setHttpStatus((string) $info["http_status"]);
$this->setErrorType((string) $info["error_type"]);
}
/**
* Sets http status associated to exception handled.
*
* @param string $httpStatus
* @throws ConfigurationException
*/
private function setHttpStatus(string $httpStatus): void
{
if (!$httpStatus) {
return;
}
$cases = HttpStatus::cases();
foreach ($cases as $case) {
if (str_starts_with($case->value, $httpStatus)) {
$this->httpStatus = $case;
return;
}
}
throw new ConfigurationException("Invalid http status: ".$httpStatus);
}
/**
* Gets HTTP status associated to exception handled.
*
* @return ?HttpStatus
*/
public function getHttpStatus(): ?HttpStatus
{
return $this->httpStatus;
}
/**
* Sets HTTP status associated to exception handled.
*
* @param string $errorType
* @throws ConfigurationException
*/
private function setErrorType(string $errorType): void
{
if (!$errorType) {
return;
}
if ($case = ErrorType::tryFrom($errorType)) {
$this->errorType = $case;
} else {
throw new ConfigurationException("Invalid error type: ".$errorType);
}
}
/**
* Gets error type associated to exception handled.
*
* @return ?ErrorType
*/
public function getErrorType(): ?ErrorType
{
return $this->errorType;
}
}