You are here

class EasyRdf_Collection in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/easyrdf/easyrdf/lib/EasyRdf/Collection.php \EasyRdf_Collection

Sub-class of EasyRdf_Resource that represents an RDF collection (rdf:List)

This class can be used to iterate through a collection of items.

Note that items are numbered from 1 (not 0) for consistency with RDF Containers.

@package EasyRdf @link http://www.w3.org/TR/xmlschema-2/#date @copyright Copyright (c) 2013 Nicholas J Humfrey @license http://www.opensource.org/licenses/bsd-license.php

Hierarchy

Expanded class hierarchy of EasyRdf_Collection

2 string references to 'EasyRdf_Collection'
EasyRdf_Graph::classForResource in vendor/easyrdf/easyrdf/lib/EasyRdf/Graph.php
Work out the class to instantiate a resource as @ignore
TypeMapper.php in vendor/easyrdf/easyrdf/lib/EasyRdf/TypeMapper.php

File

vendor/easyrdf/easyrdf/lib/EasyRdf/Collection.php, line 50

View source
class EasyRdf_Collection extends EasyRdf_Resource implements ArrayAccess, Countable, SeekableIterator {
  private $position;
  private $current;

  /** Create a new collection - do not use this directly
   *
   * @ignore
   */
  public function __construct($uri, $graph) {
    $this->position = 1;
    $this->current = null;
    parent::__construct($uri, $graph);
  }

  /** Seek to a specific position in the container
   *
   * The first item is postion 1
   *
   * @param  integer  $position     The position in the container to seek to
   * @throws OutOfBoundsException
   */
  public function seek($position) {
    if (is_int($position) and $position > 0) {
      list($node, $actual) = $this
        ->getCollectionNode($position);
      if ($actual === $position) {
        $this->position = $actual;
        $this->current = $node;
      }
      else {
        throw new OutOfBoundsException("Unable to seek to position {$position} in the collection");
      }
    }
    else {
      throw new InvalidArgumentException("Collection position must be a positive integer");
    }
  }

  /** Rewind the iterator back to the start of the collection
   *
   */
  public function rewind() {
    $this->position = 1;
    $this->current = null;
  }

  /** Return the current item in the collection
   *
   * @return mixed The current item
   */
  public function current() {
    if ($this->position === 1) {
      return $this
        ->get('rdf:first');
    }
    elseif ($this->current) {
      return $this->current
        ->get('rdf:first');
    }
  }

  /** Return the key / current position in the collection
   *
   * Note: the first item is number 1
   *
   * @return int The current position
   */
  public function key() {
    return $this->position;
  }

  /** Move forward to next item in the collection
   *
   */
  public function next() {
    if ($this->position === 1) {
      $this->current = $this
        ->get('rdf:rest');
    }
    elseif ($this->current) {
      $this->current = $this->current
        ->get('rdf:rest');
    }
    $this->position++;
  }

  /** Checks if current position is valid
   *
   * @return bool True if the current position is valid
   */
  public function valid() {
    if ($this->position === 1 and $this
      ->hasProperty('rdf:first')) {
      return true;
    }
    elseif ($this->current !== null and $this->current
      ->hasProperty('rdf:first')) {
      return true;
    }
    else {
      return false;
    }
  }

  /** Get a node for a particular offset into the collection
   *
   * This function may not return the item you requested, if
   * it does not exist. Please check the $postion parameter
   * returned.
   *
   * If the offset is null, then the last node in the
   * collection (before rdf:nil) will be returned.
   *
   * @param  integer $offset          The offset into the collection (or null)
   * @return array   $node, $postion  The node object and postion of the node
   */
  public function getCollectionNode($offset) {
    $position = 1;
    $node = $this;
    $nil = $this->graph
      ->resource('rdf:nil');
    while ($rest = $node
      ->get('rdf:rest') and $rest !== $nil and (is_null($offset) or $position < $offset)) {
      $node = $rest;
      $position++;
    }
    return array(
      $node,
      $position,
    );
  }

  /** Counts the number of items in the collection
   *
   * Note that this is an slow method - it is more efficient to use
   * the iterator interface, if you can.
   *
   * @return integer The number of items in the collection
   */
  public function count() {

    // Find the end of the collection
    list($node, $position) = $this
      ->getCollectionNode(null);
    if (!$node
      ->hasProperty('rdf:first')) {
      return 0;
    }
    else {
      return $position;
    }
  }

  /** Append an item to the end of the collection
   *
   * @param  mixed $value      The value to append
   * @return integer           The number of values appended (1 or 0)
   */
  public function append($value) {

    // Find the end of the collection
    list($node, $position) = $this
      ->getCollectionNode(null);
    $rest = $node
      ->get('rdf:rest');
    if ($node === $this and is_null($rest)) {
      $node
        ->set('rdf:first', $value);
      $node
        ->addResource('rdf:rest', 'rdf:nil');
    }
    else {
      $new = $this->graph
        ->newBnode();
      $node
        ->set('rdf:rest', $new);
      $new
        ->add('rdf:first', $value);
      $new
        ->addResource('rdf:rest', 'rdf:nil');
    }
    return 1;
  }

  /** Array Access: check if a position exists in collection using array syntax
   *
   * Example: isset($list[2])
   */
  public function offsetExists($offset) {
    if (is_int($offset) and $offset > 0) {
      list($node, $position) = $this
        ->getCollectionNode($offset);
      return $node and $position === $offset and $node
        ->hasProperty('rdf:first');
    }
    else {
      throw new InvalidArgumentException("Collection offset must be a positive integer");
    }
  }

  /** Array Access: get an item at a specified position in collection using array syntax
   *
   * Example: $item = $list[2];
   */
  public function offsetGet($offset) {
    if (is_int($offset) and $offset > 0) {
      list($node, $position) = $this
        ->getCollectionNode($offset);
      if ($node and $position === $offset) {
        return $node
          ->get('rdf:first');
      }
    }
    else {
      throw new InvalidArgumentException("Collection offset must be a positive integer");
    }
  }

  /**
   * Array Access: set an item at a positon in collection using array syntax
   *
   * Example: $list[2] = $item;
   */
  public function offsetSet($offset, $value) {
    if (is_null($offset)) {

      // No offset - append to end of collection
      $this
        ->append($value);
    }
    elseif (is_int($offset) and $offset > 0) {
      list($node, $position) = $this
        ->getCollectionNode($offset);

      // Create nodes, if they are missing
      while ($position < $offset) {
        $new = $this->graph
          ->newBnode();
        $node
          ->set('rdf:rest', $new);
        $new
          ->addResource('rdf:rest', 'rdf:nil');
        $node = $new;
        $position++;
      }

      // Terminate the list
      if (!$node
        ->hasProperty('rdf:rest')) {
        $node
          ->addResource('rdf:rest', 'rdf:nil');
      }
      return $node
        ->set('rdf:first', $value);
    }
    else {
      throw new InvalidArgumentException("Collection offset must be a positive integer");
    }
  }

  /**
   * Array Access: delete an item at a specific postion using array syntax
   *
   * Example: unset($seq[2]);
   */
  public function offsetUnset($offset) {
    if (is_int($offset) and $offset > 0) {
      list($node, $position) = $this
        ->getCollectionNode($offset);
    }
    else {
      throw new InvalidArgumentException("Collection offset must be a positive integer");
    }

    // Does the item exist?
    if ($node and $position === $offset) {
      $nil = $this->graph
        ->resource('rdf:nil');
      if ($position === 1) {
        $rest = $node
          ->get('rdf:rest');
        if ($rest and $rest !== $nil) {

          // Move second value, so we can keep the head of list
          $node
            ->set('rdf:first', $rest
            ->get('rdf:first'));
          $node
            ->set('rdf:rest', $rest
            ->get('rdf:rest'));
          $rest
            ->delete('rdf:first');
          $rest
            ->delete('rdf:rest');
        }
        else {

          // Just remove the value
          $node
            ->delete('rdf:first');
          $node
            ->delete('rdf:rest');
        }
      }
      else {

        // Remove the value and re-link the list
        $node
          ->delete('rdf:first');
        $rest = $node
          ->get('rdf:rest');
        $previous = $node
          ->get('^rdf:rest');
        if (is_null($rest)) {
          $rest = $nil;
        }
        if ($previous) {
          $previous
            ->set('rdf:rest', $rest);
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EasyRdf_Collection::$current private property
EasyRdf_Collection::$position private property
EasyRdf_Collection::append public function Append an item to the end of the collection
EasyRdf_Collection::count public function Counts the number of items in the collection
EasyRdf_Collection::current public function Return the current item in the collection
EasyRdf_Collection::getCollectionNode public function Get a node for a particular offset into the collection
EasyRdf_Collection::key public function Return the key / current position in the collection
EasyRdf_Collection::next public function Move forward to next item in the collection
EasyRdf_Collection::offsetExists public function Array Access: check if a position exists in collection using array syntax
EasyRdf_Collection::offsetGet public function Array Access: get an item at a specified position in collection using array syntax
EasyRdf_Collection::offsetSet public function Array Access: set an item at a positon in collection using array syntax
EasyRdf_Collection::offsetUnset public function Array Access: delete an item at a specific postion using array syntax
EasyRdf_Collection::rewind public function Rewind the iterator back to the start of the collection
EasyRdf_Collection::seek public function Seek to a specific position in the container
EasyRdf_Collection::valid public function Checks if current position is valid
EasyRdf_Collection::__construct public function Create a new collection - do not use this directly Overrides EasyRdf_Resource::__construct
EasyRdf_Resource::$graph protected property The Graph that this resource belongs to
EasyRdf_Resource::$uri protected property The URI for this resource
EasyRdf_Resource::add public function Add values to for a property of the resource
EasyRdf_Resource::addLiteral public function Add a literal value as a property of the resource
EasyRdf_Resource::addResource public function Add a resource as a property of the resource
EasyRdf_Resource::addType public function Add one or more rdf:type properties to the resource
EasyRdf_Resource::all public function Get all values for a property
EasyRdf_Resource::allLiterals public function Get all literal values for a property of the resource
EasyRdf_Resource::allResources public function Get all resources for a property of the resource
EasyRdf_Resource::checkHasGraph protected function Throw can exception if the resource does not belong to a graph @ignore
EasyRdf_Resource::countValues public function Count the number of values for a property of a resource
EasyRdf_Resource::delete public function Delete a property (or optionally just a specific value)
EasyRdf_Resource::dump public function Return a human readable view of the resource and its properties
EasyRdf_Resource::dumpValue public function Return pretty-print view of the resource
EasyRdf_Resource::get public function Get a single value for a property
EasyRdf_Resource::getBNodeId public function Get the identifier for a blank node
EasyRdf_Resource::getGraph public function Return the graph that this resource belongs to
EasyRdf_Resource::getLiteral public function Get a single literal value for a property of the resource
EasyRdf_Resource::getResource public function Get a single resource value for a property of the resource
EasyRdf_Resource::getUri public function Returns the URI for the resource.
EasyRdf_Resource::hasProperty public function Check to see if a property exists for this resource.
EasyRdf_Resource::htmlLink public function Generates an HTML anchor tag, linking to this resource.
EasyRdf_Resource::isA public function Check if a resource is of the specified type
EasyRdf_Resource::isBNode public function Check to see if a resource is a blank node.
EasyRdf_Resource::join public function Concatenate all values for a property into a string.
EasyRdf_Resource::label public function Get a human readable label for this resource
EasyRdf_Resource::load public function Perform a load (download of remote URI) of the resource into the graph
EasyRdf_Resource::localName public function Gets the local name of the URI of this resource
EasyRdf_Resource::parseUri public function Parse the URI of the resource and return as a ParsedUri object
EasyRdf_Resource::prefix public function Get a the prefix of the namespace that this resource is part of
EasyRdf_Resource::primaryTopic public function Get the primary topic of this resource.
EasyRdf_Resource::properties public function Get a list of all the shortened property names (qnames) for a resource.
EasyRdf_Resource::propertyUris public function Get a list of the full URIs for the properties of this resource.
EasyRdf_Resource::reversePropertyUris public function Get a list of the full URIs for the properties that point to this resource.
EasyRdf_Resource::set public function Set value for a property
EasyRdf_Resource::setType public function Change the rdf:type property for the resource
EasyRdf_Resource::shorten public function Get a shortened version of the resources URI.
EasyRdf_Resource::toRdfPhp public function Returns the properties of the resource as an RDF/PHP associative array
EasyRdf_Resource::type public function Get a single type for a resource.
EasyRdf_Resource::typeAsResource public function Get a single type for a resource, as a resource.
EasyRdf_Resource::types public function Get a list of types for a resource.
EasyRdf_Resource::typesAsResources public function Get a list of types for a resource, as Resources.
EasyRdf_Resource::__get public function Magic method to get a property of a resource
EasyRdf_Resource::__isset public function Magic method to check if a property exists
EasyRdf_Resource::__set public function Magic method to set the value for a property of a resource
EasyRdf_Resource::__toString public function Magic method to return URI of resource when casted to string
EasyRdf_Resource::__unset public function Magic method to delete a property of the resource