You are here

public function EasyRdf_Sparql_Result::dump in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/easyrdf/easyrdf/lib/EasyRdf/Sparql/Result.php \EasyRdf_Sparql_Result::dump()

Return a human readable view of the query result.

This method is intended to be a debugging aid and will return a pretty-print view of the query result.

Parameters

string $format Either 'text' or 'html':

1 call to EasyRdf_Sparql_Result::dump()
EasyRdf_Sparql_Result::__toString in vendor/easyrdf/easyrdf/lib/EasyRdf/Sparql/Result.php
Magic method to return value of the result to string

File

vendor/easyrdf/easyrdf/lib/EasyRdf/Sparql/Result.php, line 156

Class

EasyRdf_Sparql_Result
Class for returned for SPARQL SELECT and ASK query responses.

Code

public function dump($format = 'html') {
  if ($this->type == 'bindings') {
    $result = '';
    if ($format == 'html') {
      $result .= "<table class='sparql-results' style='border-collapse:collapse'>";
      $result .= "<tr>";
      foreach ($this->fields as $field) {
        $result .= "<th style='border:solid 1px #000;padding:4px;" . "vertical-align:top;background-color:#eee;'>" . "?{$field}</th>";
      }
      $result .= "</tr>";
      foreach ($this as $row) {
        $result .= "<tr>";
        foreach ($this->fields as $field) {
          if (isset($row->{$field})) {
            $result .= "<td style='border:solid 1px #000;padding:4px;" . "vertical-align:top'>" . $row->{$field}
              ->dumpValue($format) . "</td>";
          }
          else {
            $result .= "<td>&nbsp;</td>";
          }
        }
        $result .= "</tr>";
      }
      $result .= "</table>";
    }
    else {

      // First calculate the width of each comment
      $colWidths = array();
      foreach ($this->fields as $field) {
        $colWidths[$field] = strlen($field);
      }
      $textData = array();
      foreach ($this as $row) {
        $textRow = array();
        foreach ($row as $k => $v) {
          $textRow[$k] = $v
            ->dumpValue('text');
          $width = strlen($textRow[$k]);
          if ($colWidths[$k] < $width) {
            $colWidths[$k] = $width;
          }
        }
        $textData[] = $textRow;
      }

      // Create a horizontal rule
      $hr = "+";
      foreach ($colWidths as $k => $v) {
        $hr .= "-" . str_repeat('-', $v) . '-+';
      }

      // Output the field names
      $result .= "{$hr}\n|";
      foreach ($this->fields as $field) {
        $result .= ' ' . str_pad("?{$field}", $colWidths[$field]) . ' |';
      }

      // Output each of the rows
      $result .= "\n{$hr}\n";
      foreach ($textData as $textRow) {
        $result .= '|';
        foreach ($textRow as $k => $v) {
          $result .= ' ' . str_pad($v, $colWidths[$k]) . ' |';
        }
        $result .= "\n";
      }
      $result .= "{$hr}\n";
    }
    return $result;
  }
  elseif ($this->type == 'boolean') {
    $str = $this->boolean ? 'true' : 'false';
    if ($format == 'html') {
      return "<p>Result: <span style='font-weight:bold'>{$str}</span></p>";
    }
    else {
      return "Result: {$str}";
    }
  }
  else {
    throw new EasyRdf_Exception("Failed to dump SPARQL Query Results format, unknown type: " . $this->type);
  }
}