You are here

protected function EasyRdf_Parser_Turtle::parseCollection in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/easyrdf/easyrdf/lib/EasyRdf/Parser/Turtle.php \EasyRdf_Parser_Turtle::parseCollection()

Parses a collection [16], e.g: ( item1 item2 item3 ) @ignore

2 calls to EasyRdf_Parser_Turtle::parseCollection()
EasyRdf_Parser_Turtle::parseObject in vendor/easyrdf/easyrdf/lib/EasyRdf/Parser/Turtle.php
Parse a object [12] @ignore
EasyRdf_Parser_Turtle::parseSubject in vendor/easyrdf/easyrdf/lib/EasyRdf/Parser/Turtle.php
Parse a subject [10] @ignore

File

vendor/easyrdf/easyrdf/lib/EasyRdf/Parser/Turtle.php, line 448

Class

EasyRdf_Parser_Turtle
Class to parse Turtle with no external dependancies.

Code

protected function parseCollection() {
  $this
    ->verifyCharacterOrFail($this
    ->read(), "(");
  $c = $this
    ->skipWSC();
  if ($c == ')') {

    // Empty list
    $this
      ->read();
    return array(
      'type' => 'uri',
      'value' => EasyRdf_Namespace::get('rdf') . 'nil',
    );
  }
  else {
    $listRoot = $this
      ->createBNode();

    // Remember current subject and predicate
    $oldSubject = $this->subject;
    $oldPredicate = $this->predicate;

    // generated bNode becomes subject, predicate becomes rdf:first
    $this->subject = $listRoot;
    $this->predicate = array(
      'type' => 'uri',
      'value' => EasyRdf_Namespace::get('rdf') . 'first',
    );
    $this
      ->parseObject();
    $bNode = $listRoot;
    while ($this
      ->skipWSC() != ')') {

      // Create another list node and link it to the previous
      $newNode = $this
        ->createBNode();
      $this
        ->addTriple($bNode['value'], EasyRdf_Namespace::get('rdf') . 'rest', $newNode);

      // New node becomes the current
      $this->subject = $bNode = $newNode;
      $this
        ->parseObject();
    }

    // Skip ')'
    $this
      ->read();

    // Close the list
    $this
      ->addTriple($bNode['value'], EasyRdf_Namespace::get('rdf') . 'rest', array(
      'type' => 'uri',
      'value' => EasyRdf_Namespace::get('rdf') . 'nil',
    ));

    // Restore previous subject and predicate
    $this->subject = $oldSubject;
    $this->predicate = $oldPredicate;
    return $listRoot;
  }
}