From a8bbc2f58a630bc3ca4f7f96f2b4511186966322 Mon Sep 17 00:00:00 2001 From: Ben Ramsey Date: Sun, 23 Feb 2020 16:12:52 -0600 Subject: [PATCH] Add ValidatorInterface::getPattern() and set constants to private --- src/Rfc4122/Validator.php | 18 +++++++++++++----- src/Uuid.php | 2 +- src/Validator/GenericValidator.php | 15 +++++++++++++-- src/Validator/ValidatorInterface.php | 10 ++++++++++ 4 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/Rfc4122/Validator.php b/src/Rfc4122/Validator.php index ad6d6f0..97cdb7e 100644 --- a/src/Rfc4122/Validator.php +++ b/src/Rfc4122/Validator.php @@ -23,14 +23,22 @@ use function str_replace; /** * Rfc4122\Validator validates strings as UUIDs of the RFC 4122 variant */ -class Validator implements ValidatorInterface +final class Validator implements ValidatorInterface { - /** - * Regular expression pattern for matching an RFC 4122 UUID - */ - public const VALID_PATTERN = '^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-' + private const VALID_PATTERN = '^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-' . '[1-5]{1}[0-9A-Fa-f]{3}-[ABab89]{1}[0-9A-Fa-f]{3}-[0-9A-Fa-f]{12}$'; + /** + * @psalm-pure + * @psalm-return non-empty-string + * @psalm-suppress MoreSpecificReturnType we know that the retrieved `string` is never empty + * @psalm-suppress LessSpecificReturnStatement we know that the retrieved `string` is never empty + */ + public function getPattern(): string + { + return self::VALID_PATTERN; + } + /** * @psalm-pure */ diff --git a/src/Uuid.php b/src/Uuid.php index 2a3e628..903f6b4 100644 --- a/src/Uuid.php +++ b/src/Uuid.php @@ -112,7 +112,7 @@ class Uuid implements UuidInterface public const RESERVED_FUTURE = 7; /** - * @deprecated Use {@see GenericValidator::VALID_PATTERN} instead. + * @deprecated Use {@see ValidatorInterface::getPattern()} instead. */ public const VALID_PATTERN = '^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$'; diff --git a/src/Validator/GenericValidator.php b/src/Validator/GenericValidator.php index cb7b486..84fc645 100644 --- a/src/Validator/GenericValidator.php +++ b/src/Validator/GenericValidator.php @@ -22,12 +22,23 @@ use function str_replace; /** * GenericValidator validates strings as UUIDs of any variant */ -class GenericValidator implements ValidatorInterface +final class GenericValidator implements ValidatorInterface { /** * Regular expression pattern for matching a UUID of any variant. */ - public const VALID_PATTERN = '^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$'; + private const VALID_PATTERN = '^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$'; + + /** + * @psalm-pure + * @psalm-return non-empty-string + * @psalm-suppress MoreSpecificReturnType we know that the retrieved `string` is never empty + * @psalm-suppress LessSpecificReturnStatement we know that the retrieved `string` is never empty + */ + public function getPattern(): string + { + return self::VALID_PATTERN; + } /** * @psalm-pure diff --git a/src/Validator/ValidatorInterface.php b/src/Validator/ValidatorInterface.php index 61cfda8..ef813c2 100644 --- a/src/Validator/ValidatorInterface.php +++ b/src/Validator/ValidatorInterface.php @@ -19,6 +19,16 @@ namespace Ramsey\Uuid\Validator; */ interface ValidatorInterface { + /** + * Returns the regular expression pattern used by this validator + * + * @return string The regular expression pattern this validator uses + * + * @psalm-pure + * @psalm-return non-empty-string + */ + public function getPattern(): string; + /** * Returns true if the provided string represents a UUID *