You are here

public function EasyRdf_Collection::offsetSet in Zircon Profile 8.0

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

Array Access: set an item at a positon in collection using array syntax

Example: $list[2] = $item;

File

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

Class

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

Code

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");
  }
}