Repository navigation
Expand file tree
/
Copy pathDomain.php
More file actions
85 lines (74 loc) · 1.69 KB
/
Copy pathDomain.php
File metadata and controls
85 lines (74 loc) · 1.69 KB
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
<?php
namespace Dingo\Api\Http\Validation;
use Illuminate\support\Str;
use Illuminate\Http\Request;
use Dingo\Api\Contract\Http\Validator;
class Domain implements Validator
{
const PATTERN_STRIP_PROTOCOL = '/:\d*$/';
/**
* API domain.
*
* @var string
*/
protected $domain;
/**
* Create a new domain validator instance.
*
* @param string $domain
*
* @return void
*/
public function __construct($domain)
{
$this->domain = $domain;
}
/**
* Validate that the request domain matches the configured domain.
*
* @param \Illuminate\Http\Request $request
*
* @return bool
*/
public function validate(Request $request)
{
return ! is_null($this->domain) && $request->getHost() === $this->getStrippedDomain();
}
/**
* Strip the protocol from a domain.
*
* @param string $domain
*
* @return string
*/
protected function stripProtocol($domain)
{
if (Str::contains($domain, '://')) {
$domain = substr($domain, strpos($domain, '://') + 3);
}
return $domain;
}
/**
* Strip the port from a domain.
*
* @param $domain
*
* @return mixed
*/
protected function stripPort($domain)
{
if ($domainStripped = preg_replace(self::PATTERN_STRIP_PROTOCOL, null, $domain)) {
return $domainStripped;
}
return $domain;
}
/**
* Get the domain stripped from protocol and port.
*
* @return mixed
*/
protected function getStrippedDomain()
{
return $this->stripPort($this->stripProtocol($this->domain));
}
}