public static function 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 drupal/core/vendor/easyrdf/easyrdf/lib/EasyRdf/Namespace.php
Return the prefix namespace that a URI belongs to.
EasyRdf_Namespace::shorten in drupal/core/vendor/easyrdf/easyrdf/lib/EasyRdf/Namespace.php
Shorten a URI by substituting in the namespace prefix.

File

drupal/core/vendor/easyrdf/easyrdf/lib/EasyRdf/Namespace.php, line 253

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) {
      return array(
        $prefix,
        substr($uri, strlen($long)),
      );
    }
  }
  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;
}