. */ namespace SP\Http; use SP\Core\Crypt\Hash; /** * Class Uri */ final class Uri { private array $params = []; public function __construct(private readonly string $base) { } private static function mapParts(string $key, string $value): string { if (str_starts_with($key, '_')) { $key = substr($key, 1); } return sprintf('%s=%s', $key, urlencode($value)); } /** * @param string $param Param's name. If an '_' is set at the beginning, it will be a protected param * @param int|string $value * * @return Uri */ public function addParam(string $param, int|string $value): Uri { $this->params[$param] = (string)$value; return $this; } /** * @param array $params Param's name. If an '_' is set at the beginning, it will be a protected param * * @return Uri */ public function addParams(array $params): Uri { $this->params = array_map(static fn(int|string $value) => (string)$value, $params); return $this; } public function getUri(): string { return sprintf( '%s?%s', $this->base, implode('&', array_map([__CLASS__, 'mapParts'], array_keys($this->params), $this->params)) ); } public function getUriSigned(string $key): string { $uri = implode('&', array_map([__CLASS__, 'mapParts'], array_keys($this->params), $this->params)); return sprintf('%s?%s&h=%s', $this->base, $uri, Hash::signMessage($uri, $key)); } }