Skip to content

Core\Uuid::parse

Reads a UUID from its text, such as f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4, and returns a Uuid. The text must be 32 hexadecimal digits in groups of 8, 4, 4, 4 and 12, joined by hyphens. Upper case and lower case letters are both allowed. toString() always gives the text back in lower case.

Other ways to write a UUID are not allowed: the 32 digits without hyphens, a UUID in {} braces, and a UUID after urn:uuid:. For those, and for any other text, parse throws a RuntimeError. The message quotes the start of the text.

Use parse when the text must be a UUID. When the text may not be a UUID, Core\Uuid::tryParse returns null and does not throw.

The examples below read a UUID in upper case, show the error for a UUID in braces, and check the ids in the rows of an import.

Parameters

  • $sstringThe text to read. It must be exactly 36 characters long.

Return value

Returns Core\Uuid. The Uuid that the text gives. The version digit is not checked, so the nil UUID (all zeros) and the max UUID (all f) are both valid.

Errors

  • RuntimeError$s is not in that form. The 32 digits without hyphens, a UUID in {} braces and a UUID after urn:uuid: all throw this error too. The message quotes up to 48 characters of $s.

Examples

Read a uuid from its text

<?nvs
// Shows how to read a UUID from its text and print it again.
// `Core\Uuid::parse` reads the text. Upper case letters are allowed.
var $id = Core\Uuid::parse('F9168C5E-CEB2-4FAA-B6BF-329BF39FA1E4');
// The text of a `Uuid` is always lower case.
// This prints f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4.
echo $id, "\n";
Output
f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4

A uuid in braces

<?nvs
// Shows what happens when the text is not in the form `parse` reads.
// This UUID is in `{}` braces. `parse` does not allow them, so it throws a `RuntimeError`.
try {
Core\Uuid::parse('{f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4}');
} catch (RuntimeError $e) {
echo $e->message, "\n";
}
Output
Core\Uuid::parse(): "{f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4}" is not a UUID — the canonical form is eight hexadecimal digits, three groups of four and then twelve, hyphen-separated, as in "00000000-0000-4000-8000-000000000000"

Check the ids of an import

<?nvs
// Shows how an import checks the customer id in each row before it saves anything.
class Import
{
public static function check(array<string> $rows): string
{
int $line = 0;
foreach ($rows as string $row) {
$line = $line + 1;
array<string> $fields = Core\Str::split($row, ';');
try {
// The first field of each row must be a UUID.
Core\Uuid::parse($fields[0]);
} catch (RuntimeError $e) {
return 'line ' . ($line as string) . ' has no valid customer id';
}
}
return 'all ' . ($line as string) . ' rows are valid';
}
}
// The third row has a digit too few in its id.
array<string> $rows = [
'0190d1e6-8e3c-7c2a-9a4b-1f2e3d4c5b6a;Ada;Vienna',
'F9168C5E-CEB2-4FAA-B6BF-329BF39FA1E4;Grace;Arlington',
'00112233-4455-6677-8899-aabbccddeef;Linus;Helsinki',
];
// This prints "line 3 has no valid customer id".
echo Import::check($rows), "\n";
Output
line 3 has no valid customer id