|
1
|
|
|
<?php |
|
2
|
|
|
namespace LunixREST\APIRequest\URLParser\RegexURLParser; |
|
3
|
|
|
|
|
4
|
|
|
use LunixREST\APIRequest\URLParser\Exceptions\UnableToProvideMIMEException; |
|
5
|
|
|
use LunixREST\APIRequest\URLParser\MIMEProvider; |
|
6
|
|
|
use LunixREST\APIRequest\URLParser\ParsedURL; |
|
7
|
|
|
use LunixREST\APIRequest\URLParser\RegexURLParser\Exceptions\InvalidRegexPatternException; |
|
8
|
|
|
use LunixREST\APIRequest\URLParser\Exceptions\InvalidRequestURLException; |
|
9
|
|
|
use LunixREST\APIRequest\URLParser\URLParser; |
|
10
|
|
|
use Psr\Http\Message\UriInterface; |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* An implementation of a URLParser that uses named regex capture groups to parse urls. |
|
15
|
|
|
* Class RegexURLParser |
|
16
|
|
|
* @package LunixREST\APIRequest\URLParser\RegexURLParser |
|
17
|
|
|
*/ |
|
18
|
|
|
class RegexURLParser implements URLParser |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $pattern; |
|
24
|
|
|
/** |
|
25
|
|
|
* @var MIMEProvider|null |
|
26
|
|
|
*/ |
|
27
|
|
|
private $MIMEProvider; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* RegexURLParser constructor. |
|
31
|
|
|
* @param string $pattern - a pattern that matches named groups: endpoint. And optionally: element, version, apiKey, acceptableExtension |
|
32
|
|
|
* @param MIMEProvider|null $MIMEProvider |
|
33
|
|
|
* @throws InvalidRegexPatternException |
|
34
|
|
|
*/ |
|
35
|
10 |
|
public function __construct(string $pattern, ?MIMEProvider $MIMEProvider = null) |
|
36
|
|
|
{ |
|
37
|
10 |
|
$this->pattern = $pattern; |
|
38
|
|
|
|
|
39
|
10 |
|
if(@preg_match($pattern, null) === false) { |
|
40
|
1 |
|
throw new InvalidRegexPatternException("Unable to parse regex pattern", preg_last_error()); |
|
41
|
|
|
} |
|
42
|
9 |
|
$this->MIMEProvider = $MIMEProvider; |
|
43
|
9 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Parses API request data out of a url |
|
47
|
|
|
* @param UriInterface $uri |
|
48
|
|
|
* @return ParsedURL |
|
49
|
|
|
* @throws InvalidRequestURLException |
|
50
|
|
|
*/ |
|
51
|
8 |
|
public function parse(UriInterface $uri): ParsedURL |
|
52
|
|
|
{ |
|
53
|
8 |
|
$matches = []; |
|
54
|
8 |
|
if(preg_match($this->pattern, $uri->getPath(), $matches) === 0) { |
|
55
|
1 |
|
throw new InvalidRequestURLException("Unable to parse request path: did not match regex"); |
|
56
|
|
|
} |
|
57
|
7 |
|
if(!($endpoint = $matches["endpoint"] ?? null)) { |
|
58
|
1 |
|
throw new InvalidRequestURLException("Unable to match endpoint in url"); |
|
59
|
|
|
} |
|
60
|
6 |
|
$element = $matches["element"] ?? null; |
|
61
|
6 |
|
$version = $matches["version"] ?? null; |
|
62
|
6 |
|
$apiKey = $matches["apiKey"] ?? null; |
|
63
|
|
|
|
|
64
|
6 |
|
$acceptableMimeTypes = []; |
|
65
|
6 |
|
if(($acceptableExtension = $matches["acceptableExtension"] ?? null)) { |
|
66
|
3 |
|
if(!$this->MIMEProvider) { |
|
67
|
1 |
|
throw new InvalidRequestURLException("Unable to accept acceptable extensions"); |
|
68
|
|
|
} else { |
|
69
|
|
|
try { |
|
70
|
2 |
|
$acceptableMimeTypes[] = $this->MIMEProvider->provideMIME($acceptableExtension); |
|
71
|
1 |
|
} catch (UnableToProvideMIMEException $exception) { |
|
72
|
1 |
|
throw new InvalidRequestURLException($exception->getMessage()); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
4 |
|
return new ParsedURL($endpoint, $element, $version, $apiKey, $acceptableMimeTypes, $uri->getQuery()); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
1 |
|
public function getPattern(): string |
|
84
|
|
|
{ |
|
85
|
1 |
|
return $this->pattern; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|