コード例 #1
0
ファイル: CsvHandler.php プロジェクト: sulu/sulu
 /**
  * Handles response for csv-request.
  *
  * @param ViewHandler $handler
  * @param View $view
  * @param Request $request
  * @param string $format
  *
  * @return Response
  *
  * @throws ObjectNotSupportedException
  */
 public function createResponse(ViewHandler $handler, View $view, Request $request, $format)
 {
     if (!$view->getData() instanceof ListRepresentation) {
         throw new ObjectNotSupportedException($view);
     }
     $viewData = $view->getData();
     $data = new CallbackCollection($viewData->getData(), [$this, 'prepareData']);
     $fileName = sprintf('%s.csv', $viewData->getRel());
     $config = new ExporterConfig();
     $exporter = new Exporter($config);
     $data->rewind();
     if ($row = $data->current()) {
         $config->setColumnHeaders(array_keys($row));
     }
     $config->setDelimiter($this->convertValue($request->get('delimiter', ';'), self::$delimiterMap));
     $config->setNewline($this->convertValue($request->get('newLine', '\\n'), self::$newLineMap));
     $config->setEnclosure($request->get('enclosure', '"'));
     $config->setEscape($request->get('escape', '\\'));
     $response = new StreamedResponse();
     $disposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $fileName, $fileName);
     $response->headers->set('Content-Type', 'text/csv');
     $response->headers->set('Content-Disposition', $disposition);
     $response->setCallback(function () use($data, $exporter) {
         $exporter->export('php://output', $data);
     });
     $response->send();
     return $response;
 }
コード例 #2
0
ファイル: Csv.php プロジェクト: alternatex/store
 /**
  * Encode to format
  *
  * @method encode
  * @param {String} $datastore context identifier
  * @void
  */
 public static function Encode(Resource $resource)
 {
     // TODO: a lot. hnd err, etc.
     ob_start(function ($data) {
         return $data;
     });
     $exporter = new Exporter(new ExporterConfig());
     $exporter->export('php://output', $resource->content());
     $content = ob_get_contents();
     ob_end_clean();
     return $content;
 }
コード例 #3
0
 /**
  * @param array  $attributes array( '中文描述' => 'key' );
  * @param string $title
  * @param array  $data
  * @param string $fromCharSet
  * @param string $toCharSet
  * @return \Symfony\Component\HttpFoundation\StreamedResponse
  */
 public function export_csv(array $attributes, $title = 'csv-data-dump', $data = array(), $fromCharSet = 'UTF-8', $toCharSet = 'UTF-8')
 {
     $title .= '-' . date('Ymd-His');
     $config = new ExporterConfig();
     $config->setDelimiter(';')->setEnclosure("'")->setEscape("\\")->setFromCharset($fromCharSet)->setToCharset($toCharSet);
     $head = array([], []);
     foreach ($attributes as $key => $val) {
         $head[0][] = $key;
         $head[1][] = $val;
     }
     $data = array_merge($head, $data);
     $headers = array('Content-type' => "application/csv; filename=\"{$title}.csv\"", 'Content-Disposition' => "attachement; filename=\"{$title}.csv\"", 'Cache-Control' => "no-cache");
     $response = \Response::stream(function () use($config, $data) {
         $exporter = new Exporter($config);
         $exporter->export('php://output', $data);
     }, 200, $headers);
     return $response;
 }
コード例 #4
0
ファイル: UsageTest.php プロジェクト: Exquance/csv
 public function testUsageWithCallbackCollection()
 {
     $this->assertFileNotExists('vfs://output/data.csv');
     $data = array();
     $data[] = array(1, 'name1');
     $data[] = array(2, 'name2');
     $data[] = array(3, 'name3');
     $collection = new CallbackCollection($data, function ($row) {
         $row[1] = $row[1] . '!';
         return $row;
     });
     $config = new ExporterConfig();
     $exporter = new Exporter($config);
     $exporter->export('vfs://output/data.csv', $collection);
     $expectedContents = "1,name1!\r\n";
     $expectedContents .= "2,name2!\r\n";
     $expectedContents .= "3,name3!\r\n";
     $this->assertSame($expectedContents, file_get_contents('vfs://output/data.csv'));
 }
 /**
  * Constructor.
  *
  * @param mixed   $data    The response data or callback
  * @param int     $status  The response status code
  * @param array   $headers An array of response headers
  */
 public function __construct($data = array(), $status = 200, $headers = array())
 {
     if (!isset($headers['Content-Type'])) {
         $headers['Content-Type'] = 'text/csv';
     }
     parent::__construct(null, $status, $headers);
     $this->exporterConfig = new ExporterConfig();
     // If a callback is passed
     if (is_callable($data)) {
         $this->setCallback($data);
     } else {
         $self = $this;
         $this->setCallback(function () use($self, $data) {
             $exporter = new Exporter($self->exporterConfig);
             $exporter->export('php://output', $data);
         });
     }
     $this->streamed = false;
 }
コード例 #6
0
ファイル: sample.php プロジェクト: Exquance/csv
<?php

require_once __DIR__ . '/../vendor/autoload.php';
// load composer
use Goodby\CSV\Import\Standard\Lexer;
use Goodby\CSV\Import\Standard\Interpreter;
use Goodby\CSV\Import\Standard\LexerConfig;
use Goodby\CSV\Export\Standard\Exporter;
use Goodby\CSV\Export\Standard\ExporterConfig;
$pdo = new PDO('mysql:host=localhost;dbname=test', 'root', 'root', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
$pdo->query('CREATE TABLE IF NOT EXISTS user (id INT, `name` VARCHAR(255), email VARCHAR(255))');
// Importing
$config = new LexerConfig();
$lexer = new Lexer($config);
$interpreter = new Interpreter();
$interpreter->addObserver(function (array $columns) use($pdo) {
    $stmt = $pdo->prepare('INSERT INTO user (id, name, email) VALUES (?, ?, ?)');
    $stmt->execute($columns);
});
$lexer->parse('user.csv', $interpreter);
// Exporting
$config = new ExporterConfig();
$exporter = new Exporter($config);
$exporter->export('php://output', array(array('1', 'alice', '*****@*****.**'), array('2', 'bob', '*****@*****.**'), array('3', 'carol', '*****@*****.**')));
コード例 #7
0
ファイル: Goodby.php プロジェクト: nyamsprod/csv-benchmarks
 /**
  * {@inheritdoc}
  */
 public function writerTest()
 {
     $exporter = new Exporter(new ExporterConfig());
     $exporter->export($this->path, $this->generateRawData());
 }
 public function testSetCallback()
 {
     $response = CsvResponse::create(array(array('0', '----', '*****@*****.**'), array('0', '----', '*****@*****.**'), array('0', '----', '*****@*****.**')));
     $response->setCallback(function () {
         $config = new ExporterConfig();
         $exporter = new Exporter($config);
         $exporter->export('php://output', array(array('1', 'alice', '*****@*****.**'), array('2', 'bob', '*****@*****.**'), array('3', 'carol', '*****@*****.**')));
     });
     ob_start();
     $response->sendContent();
     $content = ob_get_contents();
     ob_end_clean();
     $this->assertContains("1,alice,alice@example.com", $content);
     $this->assertContains("2,bob,bob@example.com", $content);
     $this->assertContains("3,carol,carol@example.com", $content);
 }
コード例 #9
0
<?php

require_once __DIR__ . '/../vendor/autoload.php';
// load composer
use Goodby\CSV\Export\Standard\Exporter;
use Goodby\CSV\Export\Standard\ExporterConfig;
use Goodby\CSV\Export\Standard\Collection\PdoCollection;
$pdo = new PDO('mysql:host=localhost;dbname=test', 'root', 'root');
$pdo->query('CREATE TABLE IF NOT EXISTS user (id INT, `name` VARCHAR(255), email VARCHAR(255))');
$pdo->query("INSERT INTO user VALUES(1, 'alice', '*****@*****.**')");
$pdo->query("INSERT INTO user VALUES(2, 'bob', '*****@*****.**')");
$pdo->query("INSERT INTO user VALUES(3, 'carol', '*****@*****.**')");
$config = new ExporterConfig();
$exporter = new Exporter($config);
$stmt = $pdo->prepare("SELECT * FROM user");
$stmt->execute();
$exporter->export('php://output', new PdoCollection($stmt));
コード例 #10
0
ファイル: ExporterTest.php プロジェクト: Exquance/csv
 public function test_multiple_line_columns()
 {
     $csv = 'vfs://output/multiple-lines.csv';
     $this->assertFileNotExists($csv);
     $config = new ExporterConfig();
     $config->setNewline("\r\n");
     $exporter = new Exporter($config);
     $exporter->export($csv, array(array("line1\r\nline2\r\nline3", "single-line"), array("line1\r\nline2\r\nline3", "single-line"), array("line1\r\nline2\r\nline3", "single-line")));
     $this->assertFileEquals(__DIR__ . '/csv_files/multiple-lines.csv', $csv);
 }
コード例 #11
0
ファイル: ExporterTest.php プロジェクト: nouphet/csv
 public function test_unseekable_wrapper_and_custom_newline_code()
 {
     $config = new ExporterConfig();
     $config->setNewline("\r\n");
     $exporter = new Exporter($config);
     ob_start();
     $exporter->export('php://output', array(array('a', 'b', 'c'), array('1', '2', '3')));
     $output = ob_get_clean();
     $expectedCount = "a,b,c\r\n1,2,3\r\n";
     $this->assertSame($expectedCount, $output);
 }