function EasyRdf_ParsedUri::normalize

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

Return value

object EasyRdf_ParsedUri

File

core/modules/rdf/tests/src/Traits/EasyRdf_ParsedUri.php, line 214

Class

EasyRdf_ParsedUri
A RFC3986 compliant URI parser

Namespace

Drupal\Tests\rdf\Traits

Code

public function normalize() {
  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 normalized path
  $this->path = implode('/', $newSegments);
  // Allow easy chaining of methods
  return $this;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.