function UriSigner::sign
Signs a URI.
The given URI is signed by adding the query string parameter which value depends on the URI and the secret.
Parameters
\DateTimeInterface|\DateInterval|int|null $expiration The expiration for the given URI.: If $expiration is a \DateTimeInterface, it's expected to be the exact date + time. If $expiration is a \DateInterval, the interval is added to "now" to get the date + time. If $expiration is an int, it's expected to be a timestamp in seconds of the exact date + time. If $expiration is null, no expiration.
The expiration is added as a query string parameter.
File
-
vendor/
symfony/ http-foundation/ UriSigner.php, line 49
Class
- UriSigner
- @author Fabien Potencier <fabien@symfony.com>
Namespace
Symfony\Component\HttpFoundationCode
public function sign(string $uri) : string {
$expiration = null;
if (1 < \func_num_args()) {
$expiration = func_get_arg(1);
}
if (null !== $expiration && !$expiration instanceof \DateTimeInterface && !$expiration instanceof \DateInterval && !\is_int($expiration)) {
throw new \TypeError(\sprintf('The second argument of %s() must be an instance of %s or %s, an integer or null (%s given).', __METHOD__, \DateTimeInterface::class, \DateInterval::class, get_debug_type($expiration)));
}
$url = parse_url($uri);
$params = [];
if (isset($url['query'])) {
parse_str($url['query'], $params);
}
if (isset($params[$this->hashParameter])) {
throw new LogicException(\sprintf('URI query parameter conflict: parameter name "%s" is reserved.', $this->hashParameter));
}
if (isset($params[$this->expirationParameter])) {
throw new LogicException(\sprintf('URI query parameter conflict: parameter name "%s" is reserved.', $this->expirationParameter));
}
if (null !== $expiration) {
$params[$this->expirationParameter] = $this->getExpirationTime($expiration);
}
$uri = $this->buildUrl($url, $params);
$params[$this->hashParameter] = $this->computeHash($uri);
return $this->buildUrl($url, $params);
}