aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/ramsey/uuid/src/Type/Hexadecimal.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/ramsey/uuid/src/Type/Hexadecimal.php')
-rw-r--r--vendor/ramsey/uuid/src/Type/Hexadecimal.php39
1 files changed, 21 insertions, 18 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;
+ }
}