コード例 #1
0
ファイル: CSVFormatter.php プロジェクト: netsensei/core
 public static function getBody($dataObj)
 {
     // Only tabular data is allowed
     if (!is_array($dataObj->data)) {
         \App::abort(400, "You can only request a CSV formatter on a tabular datastructure.");
     }
     // Build the body
     $body = '';
     $header_printed = false;
     foreach ($dataObj->data as $row) {
         if (is_object($row)) {
             $row = get_object_vars($row);
         } elseif (!is_array($row)) {
             $body .= $row . "\n";
             continue;
         }
         // Print header
         if (!$header_printed) {
             $i = 0;
             foreach ($row as $key => $value) {
                 $body .= CSVFormatter::enclose($key);
                 $body .= sizeof($row) - 1 != $i ? ";" : "\n";
                 $i++;
             }
             $header_printed = true;
         }
         $i = 0;
         foreach ($row as $element) {
             if (is_object($element)) {
                 if (isset($element->id)) {
                     $body .= $element->id;
                 } elseif (isset($element->name)) {
                     $body .= $element->name;
                 } else {
                     $body .= "n/a";
                 }
             } elseif (is_array($element)) {
                 if (isset($element["id"])) {
                     $body .= $element["id"];
                 } elseif (isset($element["name"])) {
                     $body .= $element["name"];
                 } else {
                     $body .= "n/a";
                 }
             } else {
                 $body .= CSVFormatter::enclose($element);
             }
             $body .= sizeof($row) - 1 != $i ? ";" : "\n";
             $i++;
         }
     }
     return $body;
 }
コード例 #2
0
ファイル: CSVFormatter.php プロジェクト: tdt/core
 public static function getBody($dataObj)
 {
     // Check if its a result of a SPARQL select query
     if ($dataObj->source_definition['type'] == 'SPARQL' && $dataObj->source_definition['query_type'] == 'select') {
         $dataObj->data = self::buildTableFromSparqlResult($dataObj->data);
     }
     if (!is_array($dataObj->data)) {
         \App::abort(400, "You can only request a CSV formatter on a tabular data structure.");
     }
     // Build the body
     $body = '';
     $header_printed = false;
     foreach ($dataObj->data as $row) {
         if (is_object($row)) {
             $row = get_object_vars($row);
         } elseif (!is_array($row)) {
             $body .= $row . "\n";
             continue;
         }
         // Print header
         if (!$header_printed) {
             $i = 0;
             foreach ($row as $key => $value) {
                 $body .= CSVFormatter::enclose($key);
                 $body .= sizeof($row) - 1 != $i ? ";" : "\n";
                 $i++;
             }
             $header_printed = true;
         }
         $i = 0;
         foreach ($row as $element) {
             if (is_object($element)) {
                 \App::abort(400, "You can only request a CSV formatter on a tabular datastructure.");
             } elseif (is_array($element)) {
                 \App::abort(400, "You can only request a CSV formatter on a tabular datastructure.");
             } else {
                 $body .= CSVFormatter::enclose($element);
             }
             $body .= sizeof($row) - 1 != $i ? ";" : "\n";
             $i++;
         }
     }
     return $body;
 }