You are here

public static function EasyRdf_Namespace::splitUri in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/easyrdf/easyrdf/lib/EasyRdf/Namespace.php \EasyRdf_Namespace::splitUri()

Try and breakup a URI into a prefix and local part

If $createNamespace is true, and the URI isn't part of an existing namespace, then EasyRdf will attempt to create a new namespace and return the name of the new prefix (for example 'ns0', 'term').

If it isn't possible to split the URI, then null will be returned.

Parameters

string $uri The full URI (eg 'http://xmlns.com/foaf/0.1/name'):

bool $createNamespace If true, a new namespace will be created:

Return value

array The split URI (eg 'foaf', 'name') or null

2 calls to EasyRdf_Namespace::splitUri()
EasyRdf_Namespace::prefixOfUri in vendor/easyrdf/easyrdf/lib/EasyRdf/Namespace.php
Return the prefix namespace that a URI belongs to.
EasyRdf_Namespace::shorten in vendor/easyrdf/easyrdf/lib/EasyRdf/Namespace.php
Shorten a URI by substituting in the namespace prefix.

File

vendor/easyrdf/easyrdf/lib/EasyRdf/Namespace.php, line 308

Class

EasyRdf_Namespace
A namespace registry and manipulation class.

Code

public static function splitUri($uri, $createNamespace = false) {
  if ($uri === null or $uri === '') {
    throw new InvalidArgumentException("\$uri cannot be null or empty");
  }
  if (is_object($uri) and $uri instanceof EasyRdf_Resource) {
    $uri = $uri
      ->getUri();
  }
  elseif (!is_string($uri)) {
    throw new InvalidArgumentException("\$uri should be a string or EasyRdf_Resource");
  }
  foreach (self::namespaces() as $prefix => $long) {
    if (substr($uri, 0, strlen($long)) !== $long) {
      continue;
    }
    $local_part = substr($uri, strlen($long));
    if (strpos($local_part, '/') !== false) {

      // we can't have '/' in local part
      continue;
    }
    return array(
      $prefix,
      $local_part,
    );
  }
  if ($createNamespace) {

    // Try and create a new namespace

    # FIXME: check the valid characters for an XML element name
    if (preg_match('/^(.+?)([\\w\\-]+)$/', $uri, $matches)) {
      $prefix = "ns" . self::$anonymousNamespaceCount++;
      self::set($prefix, $matches[1]);
      return array(
        $prefix,
        $matches[2],
      );
    }
  }
  return null;
}