-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from glenncoppens/feature/configure-and-valida…
…te-schemes Add configurable and validatable schemes.
- Loading branch information
Showing
12 changed files
with
414 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace Spatie\Url; | ||
|
||
use Spatie\Url\Contracts\Validator; | ||
|
||
abstract class BaseValidator implements Validator | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Spatie\Url\Contracts; | ||
|
||
interface Validator | ||
{ | ||
public function validate(): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace Spatie\Url; | ||
|
||
use Spatie\Macroable\Macroable; | ||
use Stringable; | ||
|
||
class Scheme implements Stringable | ||
{ | ||
use Macroable; | ||
|
||
protected string $scheme; | ||
|
||
protected SchemeValidator $validator; | ||
|
||
public function __construct( | ||
string $scheme = '', | ||
array|null $allowedSchemes = null, | ||
) { | ||
$this->validator = new SchemeValidator($allowedSchemes); | ||
|
||
$this->setScheme($scheme); | ||
} | ||
|
||
protected function validate(string $scheme): void | ||
{ | ||
$this->validator->setScheme($scheme); | ||
|
||
$this->validator->validate(); | ||
} | ||
|
||
public function getScheme(): string | ||
{ | ||
return $this->scheme; | ||
} | ||
|
||
public function setScheme(string $scheme): void | ||
{ | ||
$sanitizedScheme = $this->validator::sanitizeScheme($scheme); | ||
|
||
$this->validate($sanitizedScheme); | ||
|
||
$this->scheme = $sanitizedScheme; | ||
} | ||
|
||
public function getAllowedSchemes(): array | ||
{ | ||
return $this->validator->getAllowedSchemes(); | ||
} | ||
|
||
public function setAllowedSchemes(array $allowedSchemes): void | ||
{ | ||
$this->validator->setAllowedSchemes($allowedSchemes); | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return $this->getScheme(); | ||
} | ||
|
||
public function __clone() | ||
{ | ||
$this->validator = clone $this->validator; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace Spatie\Url; | ||
|
||
use Spatie\Url\Exceptions\InvalidArgument; | ||
|
||
class SchemeValidator extends BaseValidator | ||
{ | ||
public const VALID_SCHEMES = ['http', 'https', 'mailto', 'tel']; | ||
|
||
private string|null $scheme; | ||
|
||
public function __construct( | ||
private array|null $allowedSchemes = null | ||
) { | ||
$this->scheme = null; | ||
$this->allowedSchemes = $allowedSchemes ?? self::VALID_SCHEMES; | ||
} | ||
|
||
public function validate(): void | ||
{ | ||
// '' aka "no scheme" must always be valid | ||
$alwaysAllowedSchemes = ['']; | ||
|
||
if (! in_array($this->scheme, [...$this->allowedSchemes, ...$alwaysAllowedSchemes])) { | ||
throw InvalidArgument::invalidScheme($this->scheme, $this->allowedSchemes); | ||
} | ||
} | ||
|
||
public static function sanitizeScheme(string $scheme): string | ||
{ | ||
// TODO: regex to allow correct format according to https://datatracker.ietf.org/doc/html/rfc3986#section-3.1 | ||
return strtolower($scheme); | ||
} | ||
|
||
public function getScheme(): string|null | ||
{ | ||
return $this->scheme; | ||
} | ||
|
||
public function setScheme(string $scheme): void | ||
{ | ||
$this->scheme = $scheme; | ||
} | ||
|
||
public function getAllowedSchemes(): array|null | ||
{ | ||
return $this->allowedSchemes; | ||
} | ||
|
||
public function setAllowedSchemes(array $allowedSchemes): void | ||
{ | ||
$this->allowedSchemes = array_map( | ||
fn($scheme) => static::sanitizeScheme($scheme), | ||
$allowedSchemes | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.