29 files changed
+876
-89
No files matched your search
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Tenant;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
final class DomainNormalizer
|
||||
{
|
||||
public function normalize(string $value): string
|
||||
{
|
||||
$value = trim($value);
|
||||
|
||||
if ($value === '') {
|
||||
throw new InvalidArgumentException('A domain is required.');
|
||||
}
|
||||
|
||||
$candidate = str_contains($value, '://') ? $value : 'http://'.$value;
|
||||
$host = parse_url($candidate, PHP_URL_HOST);
|
||||
|
||||
if (! is_string($host) || $host === '' || str_contains($host, '/')) {
|
||||
throw new InvalidArgumentException('Enter a valid hostname, such as book.example.com.');
|
||||
}
|
||||
|
||||
$host = strtolower(rtrim($host, '.'));
|
||||
|
||||
if (strlen($host) > 253 || preg_match('/\s/', $host) || ! preg_match('/^[a-z0-9.-]+$/', $host)) {
|
||||
throw new InvalidArgumentException('Enter a valid hostname, such as book.example.com.');
|
||||
}
|
||||
|
||||
return $host;
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,11 @@ final class TenantContext
|
||||
$this->business = $business;
|
||||
}
|
||||
|
||||
public function forget(): void
|
||||
{
|
||||
$this->business = null;
|
||||
}
|
||||
|
||||
public function business(): ?Business
|
||||
{
|
||||
return $this->business;
|
||||
|
||||
Reference in new issue
Block a user