public function EasyRdf_ParsedUri::normalise

Normalises the path of this URI if it has one. Normalising a path means that any unnecessary '.' and '..' segments are removed. For example, the URI http://example.com/a/b/../c/./d would be normalised to http://example.com/a/c/d

Return value

object EasyRdf_ParsedUri

File

drupal/core/vendor/easyrdf/easyrdf/lib/EasyRdf/ParsedUri.php, line 203

Class

EasyRdf_ParsedUri
A RFC3986 compliant URI parser

Code

public function normalise() {
  if (empty($this->path)) {
    return $this;
  }

  // Remove ./ from the start
  if (substr($this->path, 0, 2) == './') {

    // Remove both characters
    $this->path = substr($this->path, 2);
  }

  // Remove /. from the end
  if (substr($this->path, -2) == '/.') {

    // Remove only the last dot, not the slash!
    $this->path = substr($this->path, 0, -1);
  }
  if (substr($this->path, -3) == '/..') {
    $this->path .= '/';
  }

  // Split the path into its segments
  $segments = explode('/', $this->path);
  $newSegments = array();

  // Remove all unnecessary '.' and '..' segments
  foreach ($segments as $segment) {
    if ($segment == '..') {

      // Remove the previous part of the path
      $count = count($newSegments);
      if ($count > 0 && $newSegments[$count - 1]) {
        array_pop($newSegments);
      }
    }
    elseif ($segment == '.') {

      // Ignore
      continue;
    }
    else {
      array_push($newSegments, $segment);
    }
  }

  // Construct the new normalised path
  $this->path = implode($newSegments, '/');

  // Allow easy chaining of methods
  return $this;
}