Skip to content

Core\Uuid::format

$uuid->format(): string

Returns the text that Core\Uuid::parse reads, such as f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4. The text is always 36 characters long and in lower case. format returns the same text as toString.

Core\Uuid implements the Parses interface, and format is the method that interface needs. Core\Router::url calls it to write a UUID into a link, in the path or in the query string. Core\Uri::buildQuery calls it too. The text has only digits, the letters a to f and hyphens, so a URL never escapes it. A route that declares a Core\Uuid parameter reads the same UUID from the link.

The examples below read the text back with parse, write a UUID into a link to a route, and write one into a query string.

Return value

Returns string. A string of 36 characters, in lower case.

Changelog

VersionChange
0.0.6Core\Uuid::format and Core\Queue\Id::format return the text that parse reads. It is the same text as toString.

Examples

Format gives the text parse reads

<?nvs
// Shows that `format` returns the text that `Core\Uuid::parse` reads.
var $id = Core\Uuid::parse('F9168C5E-CEB2-4FAA-B6BF-329BF39FA1E4');
// The text is in lower case. This prints f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4.
string $text = $id->format();
echo $text, "\n";
// `format` and `toString` return the same text. This prints "same".
echo $text == $id->toString() ? 'same' : 'different', "\n";
// `parse` reads the text back to the same sixteen bytes. This prints "same UUID".
var $again = Core\Uuid::parse($text);
echo $again->toBytes() == $id->toBytes() ? 'same UUID' : 'different UUID', "\n";
Output
f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4
same
same UUID

Put a uuid in a link

<?nvs
// Shows how `Core\Router::url` writes a UUID into a link.
class Orders
{
#[Core\Route(path: '/orders/{order}', method: Core\Http\Method::Get)]
#[Core\Access(allow: Core\Audience::Public)]
public static function show(Core\Uuid $order): void
{
echo 'Order ', $order->format(), "\n";
}
}
// A gate is the class that `Core\Router::dispatch` calls to check each request.
// This gate allows every request.
class Open implements Core\Router\Gate
{
public function allows(Core\Router\Match $route): bool
{
return true;
}
public function refused(Core\Router\Refusal $reason, ?Core\Router\Match $route): void
{
echo 'no page here';
}
public function failed(?Core\Router\Match $route, ?Throwable $error): void
{
echo 'something went wrong';
}
}
if (Core\Env::context() == Core\Env::Context::Request) {
Core\Router::dispatch(new Open());
} else {
var $order = Core\Uuid::parse('0190D1E6-8E3C-7C2A-9A4B-1F2E3D4C5B6A');
// `Core\Router::url` calls `format` to write the UUID into the path.
// This prints /orders/0190d1e6-8e3c-7c2a-9a4b-1f2e3d4c5b6a.
string $link = Core\Router::url(Orders::show, ['order' => $order]);
echo $link, "\n";
// The route reads the same UUID from the link. This prints 200 and the order.
var $answer = Core\Test::request(Core\Http\Method::Get, $link);
echo $answer->status(), ' ', $answer->body();
}
Output
/orders/0190d1e6-8e3c-7c2a-9a4b-1f2e3d4c5b6a
200 Order 0190d1e6-8e3c-7c2a-9a4b-1f2e3d4c5b6a

Build a query string

<?nvs
// Shows how `Core\Uri::buildQuery` writes a UUID into a query string.
var $customer = Core\Uuid::parse('F9168C5E-CEB2-4FAA-B6BF-329BF39FA1E4');
// `buildQuery` calls `format` for the UUID. Other values are written as they are.
// This prints customer=f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4&page=2.
string $query = Core\Uri::buildQuery(['customer' => $customer, 'page' => 2]);
echo $query, "\n";
// The text has no character that a URL must escape, so it is not changed.
// This prints "unchanged".
echo Core\Uri::encodeComponent($customer->format()) == $customer->format() ? 'unchanged' : 'changed', "\n";
Output
customer=f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4&page=2
unchanged