1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SoliDry\Helpers; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use SoliDry\Extension\JSONApiInterface; |
7
|
|
|
use Illuminate\Http\Request as Req; |
8
|
|
|
use SoliDry\Types\RoutesInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Request |
12
|
|
|
* |
13
|
|
|
* @package SoliDry\Helpers |
14
|
|
|
*/ |
15
|
|
|
class Request |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @param Req $request |
19
|
|
|
* @return array |
20
|
|
|
*/ |
21
|
|
|
public static function getSupportedExtensions(Req $request): array |
22
|
|
|
{ |
23
|
|
|
$contentType = $request->header(JSONApiInterface::CONTENT_TYPE_KEY); |
24
|
|
|
if (mb_strpos($contentType, JSONApiInterface::EXT) === false) { |
|
|
|
|
25
|
|
|
return []; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
[$i, $ext] = explode(';', $contentType); |
|
|
|
|
29
|
|
|
[$j, $supportedExt] = explode('=', $ext); |
30
|
|
|
|
31
|
|
|
return explode(',', trim($supportedExt, '"')); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param Req $request |
36
|
|
|
* @param string $ext |
37
|
|
|
* @return bool |
38
|
|
|
*/ |
39
|
|
|
public static function isExt(Req $request, string $ext): bool |
40
|
|
|
{ |
41
|
|
|
$extensions = static::getSupportedExtensions($request); |
42
|
|
|
|
43
|
|
|
return \in_array($ext, $extensions, true); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Returns base path of current url |
48
|
|
|
* |
49
|
|
|
* @example: https://example.com/api/v3/ |
50
|
|
|
* |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
public function getBasePath(): string |
54
|
|
|
{ |
55
|
|
|
$parsedUrl = parse_url(url()->current()); |
56
|
|
|
$explodedPath = explode('/', $parsedUrl['path']); |
57
|
|
|
|
58
|
|
|
$baseUrl = $parsedUrl['scheme'] . '://' . $parsedUrl['host']; |
59
|
|
|
if ($explodedPath[1] === RoutesInterface::ROUTES_FILE_NAME) { |
60
|
|
|
$baseUrl .= '/' . $explodedPath[1] . '/' . $explodedPath[2]; |
61
|
|
|
} else { |
62
|
|
|
$baseUrl .= '/' . $explodedPath[1]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $baseUrl; |
66
|
|
|
} |
67
|
|
|
} |