new export options
This commit is contained in:
@@ -37,6 +37,30 @@ abstract class BaseExporter
|
||||
$this->columns = Column::columnsFromJson($table->revision->columns);
|
||||
}
|
||||
|
||||
protected function getHeaderComment($prefix = " ")
|
||||
{
|
||||
$table = $this->table;
|
||||
$owner = $this->table->owner;
|
||||
|
||||
$s =
|
||||
$prefix."Table \"$table->title\" by $owner->title\n".
|
||||
$prefix."exported ".date('M n, Y \\a\\t G:i')."\n".
|
||||
$prefix."\n".
|
||||
$prefix."License: ". str_replace("\n", "\n$prefix", $table->license ?: 'CC0') ."\n".
|
||||
$prefix."\n".
|
||||
$prefix."Upstream: https://datatable.directory/$owner->handle/$table->name\n";
|
||||
|
||||
$s .=
|
||||
$prefix."\n".
|
||||
$prefix."Columns:\n";
|
||||
|
||||
foreach ($this->columns as $c) {
|
||||
$s .= $prefix." - $c->name ... $c->title\n";
|
||||
}
|
||||
|
||||
return $s;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string - mime type for the downloaded file
|
||||
*/
|
||||
|
||||
@@ -36,6 +36,8 @@ class CStructArrayExporter extends BaseExporter
|
||||
*/
|
||||
protected function writeDocument()
|
||||
{
|
||||
$table = $this->table;
|
||||
|
||||
$any_bool = false;
|
||||
$fields = array_map(function (Column $c) use (&$any_bool) {
|
||||
$type = $c->type;
|
||||
@@ -50,13 +52,15 @@ class CStructArrayExporter extends BaseExporter
|
||||
return (object)['name' => $c->name, 'type' => $c->type, 'ctype' => $type, 'cname' => $name];
|
||||
}, $this->columns);
|
||||
|
||||
$ctablename = $this->cify($this->table->name);
|
||||
$ctablename = $this->cify($table->name);
|
||||
|
||||
// preamble
|
||||
if ($any_bool) {
|
||||
echo "#include <stdbool.h>\n\n";
|
||||
}
|
||||
|
||||
echo "/*\n" . $this->getHeaderComment(' ') . "*/\n\n";
|
||||
|
||||
echo "struct " . $ctablename . " {\n";
|
||||
foreach ($fields as $field) {
|
||||
echo " " . $field->ctype . " " . $field->cname . ";\n";
|
||||
@@ -74,7 +78,7 @@ class CStructArrayExporter extends BaseExporter
|
||||
echo ",\n";
|
||||
}
|
||||
|
||||
echo " {";
|
||||
echo " { ";
|
||||
|
||||
$firstf = true;
|
||||
foreach ($fields as $field) {
|
||||
@@ -115,7 +119,7 @@ class CStructArrayExporter extends BaseExporter
|
||||
break;
|
||||
}
|
||||
}
|
||||
echo "}";
|
||||
echo " }";
|
||||
}
|
||||
|
||||
echo "\n};\n";
|
||||
|
||||
@@ -6,6 +6,14 @@ namespace App\Tables;
|
||||
|
||||
class CsvExporter extends BaseExporter
|
||||
{
|
||||
private $delimiter = ',';
|
||||
|
||||
public function withDelimiter($delimiter)
|
||||
{
|
||||
$this->delimiter = $delimiter;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string - mime type for the downloaded file
|
||||
*/
|
||||
@@ -33,7 +41,7 @@ class CsvExporter extends BaseExporter
|
||||
return $c->title;
|
||||
}, $this->columns);
|
||||
|
||||
fputcsv($handle, $columnNames);
|
||||
fputcsv($handle, $columnNames, $this->delimiter);
|
||||
|
||||
foreach ($this->iterateRows() as $row) {
|
||||
$items = [];
|
||||
@@ -44,7 +52,7 @@ class CsvExporter extends BaseExporter
|
||||
$items[] = null;
|
||||
}
|
||||
}
|
||||
fputcsv($handle, $items);
|
||||
fputcsv($handle, $items, $this->delimiter);
|
||||
}
|
||||
|
||||
fclose($handle);
|
||||
|
||||
@@ -6,12 +6,20 @@ namespace App\Tables;
|
||||
|
||||
class JsonExporter extends BaseExporter
|
||||
{
|
||||
private $fullJsDoc = false;
|
||||
|
||||
public function withJsWrapper()
|
||||
{
|
||||
$this->fullJsDoc = true;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string - mime type for the downloaded file
|
||||
*/
|
||||
protected function getMimeType()
|
||||
{
|
||||
return 'application/json';
|
||||
return $this->fullJsDoc ? 'application/javascript' : 'application/json';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -27,6 +35,12 @@ class JsonExporter extends BaseExporter
|
||||
*/
|
||||
protected function writeDocument()
|
||||
{
|
||||
if ($this->fullJsDoc) {
|
||||
echo "/*\n" . $this->getHeaderComment(' ') . "*/\n\n";
|
||||
|
||||
echo 'module.exports = ';
|
||||
}
|
||||
|
||||
echo "[\n";
|
||||
|
||||
$first = true;
|
||||
@@ -36,7 +50,7 @@ class JsonExporter extends BaseExporter
|
||||
} else {
|
||||
echo ",\n";
|
||||
}
|
||||
echo json_encode($row, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
|
||||
echo ' '.json_encode($row, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
|
||||
}
|
||||
|
||||
echo "\n]\n";
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace App\Tables;
|
||||
|
||||
|
||||
class PhpExporter extends BaseExporter
|
||||
{
|
||||
/**
|
||||
* @return string - mime type for the downloaded file
|
||||
*/
|
||||
protected function getMimeType()
|
||||
{
|
||||
return $this->wantDownload ? 'application/x-php' : 'text/plain';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string - file extension for the downloaded file
|
||||
*/
|
||||
protected function getFileExtension()
|
||||
{
|
||||
return 'php';
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the document to stdout ('php://output')
|
||||
*/
|
||||
protected function writeDocument()
|
||||
{
|
||||
echo "<?php\n\n";
|
||||
|
||||
echo "/*\n" . $this->getHeaderComment(' ') . "*/\n\n";
|
||||
|
||||
echo "return [\n";
|
||||
|
||||
$first = true;
|
||||
foreach ($this->iterateRows() as $row) {
|
||||
if ($first) {
|
||||
$first = false;
|
||||
} else {
|
||||
echo ",\n";
|
||||
}
|
||||
|
||||
echo ' [';
|
||||
|
||||
$firstf = true;
|
||||
foreach ($this->columns as $column) {
|
||||
if ($firstf) {
|
||||
$firstf = false;
|
||||
} else {
|
||||
echo ", ";
|
||||
}
|
||||
|
||||
var_export($column->name);
|
||||
echo ' => ';
|
||||
var_export($row->{$column->name});
|
||||
}
|
||||
|
||||
echo ']';
|
||||
}
|
||||
|
||||
echo "\n];\n";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user