diff options
Diffstat (limited to 'vendor/ramsey/uuid/src/Type')
-rw-r--r-- | vendor/ramsey/uuid/src/Type/Hexadecimal.php | 39 | ||||
-rw-r--r-- | vendor/ramsey/uuid/src/Type/Integer.php | 86 | ||||
-rw-r--r-- | vendor/ramsey/uuid/src/Type/Time.php | 2 |
3 files changed, 71 insertions, 56 deletions
diff --git a/vendor/ramsey/uuid/src/Type/Hexadecimal.php b/vendor/ramsey/uuid/src/Type/Hexadecimal.php index 3c8f30adf..bf71ec4b1 100644 --- a/vendor/ramsey/uuid/src/Type/Hexadecimal.php +++ b/vendor/ramsey/uuid/src/Type/Hexadecimal.php @@ -17,10 +17,8 @@ namespace Ramsey\Uuid\Type; use Ramsey\Uuid\Exception\InvalidArgumentException; use ValueError; -use function ctype_xdigit; +use function preg_match; use function sprintf; -use function str_starts_with; -use function strtolower; use function substr; /** @@ -37,23 +35,11 @@ final class Hexadecimal implements TypeInterface private string $value; /** - * @param string $value The hexadecimal value to store + * @param self|string $value The hexadecimal value to store */ - public function __construct(string $value) + public function __construct(self | string $value) { - $value = strtolower($value); - - if (str_starts_with($value, '0x')) { - $value = substr($value, 2); - } - - if (!ctype_xdigit($value)) { - throw new InvalidArgumentException( - 'Value must be a hexadecimal number' - ); - } - - $this->value = $value; + $this->value = $value instanceof self ? (string) $value : $this->prepareValue($value); } public function toString(): string @@ -109,4 +95,21 @@ final class Hexadecimal implements TypeInterface $this->unserialize($data['string']); } + + private function prepareValue(string $value): string + { + $value = strtolower($value); + + if (str_starts_with($value, '0x')) { + $value = substr($value, 2); + } + + if (!preg_match('/^[A-Fa-f0-9]+$/', $value)) { + throw new InvalidArgumentException( + 'Value must be a hexadecimal number' + ); + } + + return $value; + } } diff --git a/vendor/ramsey/uuid/src/Type/Integer.php b/vendor/ramsey/uuid/src/Type/Integer.php index e41b3cad5..50dac9934 100644 --- a/vendor/ramsey/uuid/src/Type/Integer.php +++ b/vendor/ramsey/uuid/src/Type/Integer.php @@ -17,10 +17,10 @@ namespace Ramsey\Uuid\Type; use Ramsey\Uuid\Exception\InvalidArgumentException; use ValueError; -use function ctype_digit; -use function ltrim; +use function assert; +use function is_numeric; +use function preg_match; use function sprintf; -use function str_starts_with; use function substr; /** @@ -46,40 +46,7 @@ final class Integer implements NumberInterface public function __construct(float | int | string | self $value) { - $value = (string) $value; - $sign = '+'; - - // If the value contains a sign, remove it for ctype_digit() check. - if (str_starts_with($value, '-') || str_starts_with($value, '+')) { - $sign = substr($value, 0, 1); - $value = substr($value, 1); - } - - if (!ctype_digit($value)) { - throw new InvalidArgumentException( - 'Value must be a signed integer or a string containing only ' - . 'digits 0-9 and, optionally, a sign (+ or -)' - ); - } - - // Trim any leading zeros. - $value = ltrim($value, '0'); - - // Set to zero if the string is empty after trimming zeros. - if ($value === '') { - $value = '0'; - } - - // Add the negative sign back to the value. - if ($sign === '-' && $value !== '0') { - $value = $sign . $value; - $this->isNegative = true; - } - - /** @psalm-var numeric-string $numericValue */ - $numericValue = $value; - - $this->value = $numericValue; + $this->value = $value instanceof self ? (string) $value : $this->prepareValue($value); } public function isNegative(): bool @@ -95,6 +62,9 @@ final class Integer implements NumberInterface return $this->value; } + /** + * @psalm-return numeric-string + */ public function __toString(): string { return $this->toString(); @@ -143,4 +113,46 @@ final class Integer implements NumberInterface $this->unserialize($data['string']); } + + /** + * @return numeric-string + */ + private function prepareValue(float | int | string $value): string + { + $value = (string) $value; + $sign = '+'; + + // If the value contains a sign, remove it for digit pattern check. + if (str_starts_with($value, '-') || str_starts_with($value, '+')) { + $sign = substr($value, 0, 1); + $value = substr($value, 1); + } + + if (!preg_match('/^\d+$/', $value)) { + throw new InvalidArgumentException( + 'Value must be a signed integer or a string containing only ' + . 'digits 0-9 and, optionally, a sign (+ or -)' + ); + } + + // Trim any leading zeros. + $value = ltrim($value, '0'); + + // Set to zero if the string is empty after trimming zeros. + if ($value === '') { + $value = '0'; + } + + // Add the negative sign back to the value. + if ($sign === '-' && $value !== '0') { + $value = $sign . $value; + + /** @psalm-suppress InaccessibleProperty */ + $this->isNegative = true; + } + + assert(is_numeric($value)); + + return $value; + } } diff --git a/vendor/ramsey/uuid/src/Type/Time.php b/vendor/ramsey/uuid/src/Type/Time.php index 745b5ccab..0cedb4476 100644 --- a/vendor/ramsey/uuid/src/Type/Time.php +++ b/vendor/ramsey/uuid/src/Type/Time.php @@ -56,7 +56,7 @@ final class Time implements TypeInterface public function toString(): string { - return $this->seconds->toString() . '.' . $this->microseconds->toString(); + return $this->seconds->toString() . '.' . sprintf('%06s', $this->microseconds->toString()); } public function __toString(): string |