/** * Resolves a relative URI using this URI as the base URI. */ public function resolve($relUri) { // If it is a string, then convert it to a parsed object if (is_string($relUri)) { $relUri = new self($relUri); } // This code is based on the pseudocode in section 5.2.2 of RFC3986 $target = new self(); if ($relUri->scheme) { $target->scheme = $relUri->scheme; $target->authority = $relUri->authority; $target->path = $relUri->path; $target->query = $relUri->query; } else { if ($relUri->authority) { $target->authority = $relUri->authority; $target->path = $relUri->path; $target->query = $relUri->query; } else { if (empty($relUri->path)) { $target->path = $this->path; if ($relUri->query) { $target->query = $relUri->query; } else { $target->query = $this->query; } } else { if (substr($relUri->path, 0, 1) == '/') { $target->path = $relUri->path; } else { $path = $this->path; $lastSlash = strrpos($path, '/'); if ($lastSlash !== false) { $path = substr($path, 0, $lastSlash + 1); } else { $path = '/'; } $target->path .= $path . $relUri->path; } $target->query = $relUri->query; } $target->authority = $this->authority; } $target->scheme = $this->scheme; } $target->fragment = $relUri->fragment; $target->normalise(); return $target; }