first commit

This commit is contained in:
2023-09-12 21:41:04 +02:00
commit 3361a7f053
13284 changed files with 2116755 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
Copyright (c) <year> <copyright holders>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -0,0 +1,28 @@
<?php
// For composer
require_once 'vendor/autoload.php';
/**
* You can overwrite GOODBY_CSV_TEST_DB_* by `export` command.
* @see http://manpages.ubuntu.com/manpages/hardy/man5/exports.5.html
*
* Example:
*
* ```
* export GOODBY_CSV_TEST_DB_USER=alice
* export GOODBY_CSV_TEST_DB_PASS=passwd
* phpunit
* ```
*/
if ( isset($_SERVER['GOODBY_CSV_TEST_DB_HOST']) === false ) {
$_SERVER['GOODBY_CSV_TEST_DB_HOST'] = $_SERVER['GOODBY_CSV_TEST_DB_HOST_DEFAULT'];
}
if ( isset($_SERVER['GOODBY_CSV_TEST_DB_NAME']) === false ) {
$_SERVER['GOODBY_CSV_TEST_DB_NAME'] = $_SERVER['GOODBY_CSV_TEST_DB_NAME_DEFAULT'];
}
if ( isset($_SERVER['GOODBY_CSV_TEST_DB_USER']) === false ) {
$_SERVER['GOODBY_CSV_TEST_DB_USER'] = $_SERVER['GOODBY_CSV_TEST_DB_USER_DEFAULT'];
}
if ( isset($_SERVER['GOODBY_CSV_TEST_DB_PASS']) === false ) {
$_SERVER['GOODBY_CSV_TEST_DB_PASS'] = $_SERVER['GOODBY_CSV_TEST_DB_PASS_DEFAULT'];
}

View File

@@ -0,0 +1,10 @@
<?php
namespace Goodby\CSV\Export\Protocol\Exception;
/**
* Throws if it is unable to write CSV file
*/
class IOException extends \RuntimeException
{
}

View File

@@ -0,0 +1,22 @@
<?php
namespace Goodby\CSV\Export\Protocol;
use Traversable;
use Goodby\CSV\Export\Protocol\Exception\IOException;
/**
* Interface of the Exporter
*/
interface ExporterInterface
{
/**
* Export data as CSV file
*
* @param string $filename
* @param array|Traversable $rows
* @throws IOException
* @return void
*/
public function export($filename, $rows);
}

View File

@@ -0,0 +1,88 @@
<?php
namespace Goodby\CSV\Export\Standard\Collection;
use Iterator;
use IteratorAggregate;
class CallbackCollection implements Iterator
{
private $callable;
private $data;
public function __construct($data, $callable)
{
$this->callable = $callable;
if (!is_callable($callable)) {
throw new \InvalidArgumentException('the second argument must be callable');
}
if (is_array($data)) {
$ao = new \ArrayObject($data);
$this->data = $ao->getIterator();
} elseif ($data instanceof Iterator) {
$this->data = $data;
} elseif ($data instanceof IteratorAggregate) {
$this->data = $data->getIterator();
} else {
throw new \InvalidArgumentException('data must be an array or an Iterator/IteratorAggregate');
}
}
/**
* (PHP 5 &gt;= 5.0.0)<br/>
* Return the current element
* @link http://php.net/manual/en/iterator.current.php
* @return mixed Can return any type.
*/
public function current()
{
return call_user_func($this->callable, $this->data->current());
}
/**
* (PHP 5 &gt;= 5.0.0)<br/>
* Move forward to next element
* @link http://php.net/manual/en/iterator.next.php
* @return void Any returned value is ignored.
*/
public function next()
{
$this->data->next();
}
/**
* (PHP 5 &gt;= 5.0.0)<br/>
* Return the key of the current element
* @link http://php.net/manual/en/iterator.key.php
* @return mixed scalar on success, or null on failure.
*/
public function key()
{
return $this->data->key();
}
/**
* (PHP 5 &gt;= 5.0.0)<br/>
* Checks if current position is valid
* @link http://php.net/manual/en/iterator.valid.php
* @return boolean The return value will be casted to boolean and then evaluated.
* Returns true on success or false on failure.
*/
public function valid()
{
return $this->data->valid();
}
/**
* (PHP 5 &gt;= 5.0.0)<br/>
* Rewind the Iterator to the first element
* @link http://php.net/manual/en/iterator.rewind.php
* @return void Any returned value is ignored.
*/
public function rewind()
{
$this->data->rewind();
}
}

View File

@@ -0,0 +1,82 @@
<?php
namespace Goodby\CSV\Export\Standard\Collection;
use Iterator;
use PDO;
class PdoCollection implements Iterator
{
/**
* @var \PDOStatement
*/
private $stmt;
private $rowCount;
private $current = 0;
public function __construct(\PDOStatement $stmt)
{
$this->stmt = $stmt;
$this->rowCount = $this->stmt->rowCount();
}
/**
* (PHP 5 &gt;= 5.0.0)<br/>
* Return the current element
* @link http://php.net/manual/en/iterator.current.php
* @return mixed Can return any type.
*/
public function current()
{
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
/**
* (PHP 5 &gt;= 5.0.0)<br/>
* Move forward to next element
* @link http://php.net/manual/en/iterator.next.php
* @return void Any returned value is ignored.
*/
public function next()
{
$this->current++;
}
/**
* (PHP 5 &gt;= 5.0.0)<br/>
* Return the key of the current element
* @link http://php.net/manual/en/iterator.key.php
* @return mixed scalar on success, or null on failure.
*/
public function key()
{
$this->current;
}
/**
* (PHP 5 &gt;= 5.0.0)<br/>
* Checks if current position is valid
* @link http://php.net/manual/en/iterator.valid.php
* @return boolean The return value will be casted to boolean and then evaluated.
* Returns true on success or false on failure.
*/
public function valid()
{
return ($this->rowCount > $this->current);
}
/**
* (PHP 5 &gt;= 5.0.0)<br/>
* Rewind the Iterator to the first element
* @link http://php.net/manual/en/iterator.rewind.php
* @return void Any returned value is ignored.
*/
public function rewind()
{
$this->stmt->execute();
$this->current = 0;
}
}

View File

@@ -0,0 +1,85 @@
<?php
namespace Goodby\CSV\Export\Standard;
use SplFileObject;
class CsvFileObject extends SplFileObject
{
const FILE_MODE_WRITE = 'w';
const FILE_MODE_APPEND = 'a';
/**
* newline character
* @var string
*/
private $newline = "\n";
/**
* CSV filter
* @var callable
*/
private $csvFilter;
/**
* @param string $newline
* @return void
*/
public function setNewline($newline)
{
$this->newline = $newline;
}
/**
* Set csv filter
* @param callable $filter
*/
public function setCsvFilter($filter)
{
$this->csvFilter = $filter;
}
/**
* Write a field array as a CSV line
* @param array $fields
* @param string $delimiter
* @param string $enclosure
* @param useless $escape THIS PARAM IS UNSED, BUT REQUIRED EXISTS, see https://bugs.php.net/bug.php?id=68479 and https://github.com/goodby/csv/issues/56
* @return int|void
*/
public function fputcsv($fields, $delimiter = null, $enclosure = null, $escape = null)
{
// Temporary output a line to memory to get line as string
$fp = fopen('php://temp', 'w+');
$arguments = func_get_args();
array_unshift($arguments, $fp);
call_user_func_array('fputcsv', $arguments);
rewind($fp);
$line = '';
while ( feof($fp) === false ) {
$line .= fgets($fp);
}
fclose($fp);
/**
* Because the php_fputcsv() implementation in PHP´s source code
* has a hardcoded "\n", this method replaces the last LF code
* with what the client code wishes.
*/
$line = rtrim($line, "\n"). $this->newline;
// if the enclosure was '' | false
if (empty($enclosure)) {
$line = str_replace("\0", '', $line);
}
if ( is_callable($this->csvFilter) ) {
$line = call_user_func($this->csvFilter, $line);
}
return $this->fwrite($line);
}
}

View File

@@ -0,0 +1,7 @@
<?php
namespace Goodby\CSV\Export\Standard\Exception;
class StrictViolationException extends \RuntimeException
{
}

View File

@@ -0,0 +1,110 @@
<?php
namespace Goodby\CSV\Export\Standard;
use Goodby\CSV\Export\Protocol\ExporterInterface;
use Goodby\CSV\Export\Protocol\Exception\IOException;
use Goodby\CSV\Export\Standard\Exception\StrictViolationException;
/**
* Standard exporter class
*/
class Exporter implements ExporterInterface
{
/**
* @var ExporterConfig
*/
private $config;
/**
* @var int
*/
private $rowConsistency = null;
/**
* @var bool
*/
private $strict = true;
/**
* Return new Exporter object
* @param ExporterConfig $config
*/
public function __construct(ExporterConfig $config)
{
$this->config = $config;
}
/**
* Disable strict mode
*/
public function unstrict()
{
$this->strict = false;
}
/**
* {@inherit}
* @throws StrictViolationException
*/
public function export($filename, $rows)
{
$delimiter = $this->config->getDelimiter();
$enclosure = $this->config->getEnclosure();
$enclosure = empty($enclosure) ? "\0" : $enclosure;
$newline = $this->config->getNewline();
$fromCharset = $this->config->getFromCharset();
$toCharset = $this->config->getToCharset();
$fileMode = $this->config->getFileMode();
$columnHeaders = $this->config->getColumnHeaders();
try {
$csv = new CsvFileObject($filename, $fileMode);
} catch ( \Exception $e ) {
throw new IOException($e->getMessage(), null, $e);
}
$csv->setNewline($newline);
if ( $toCharset ) {
$csv->setCsvFilter(function($line) use($toCharset, $fromCharset) {
return mb_convert_encoding($line, $toCharset, $fromCharset);
});
}
if (count($columnHeaders) > 0) {
$this->checkRowConsistency($columnHeaders);
$csv->fputcsv($columnHeaders, $delimiter, $enclosure);
}
foreach ( $rows as $row ) {
$this->checkRowConsistency($row);
$csv->fputcsv($row, $delimiter, $enclosure);
}
$csv->fflush();
}
/**
* Check if the column count is consistent with comparing other rows
* @param array|\Countable $row
* @throws Exception\StrictViolationException
*/
private function checkRowConsistency($row)
{
if ( $this->strict === false ) {
return;
}
$current = count($row);
if ( $this->rowConsistency === null ) {
$this->rowConsistency = $current;
}
if ( $current !== $this->rowConsistency ) {
throw new StrictViolationException();
}
$this->rowConsistency = $current;
}
}

View File

@@ -0,0 +1,218 @@
<?php
namespace Goodby\CSV\Export\Standard;
/**
* Config for Exporter object
*/
class ExporterConfig
{
/**
* Delimiter
* @var string
*/
private $delimiter = ',';
/**
* Enclosure
* @var string
*/
private $enclosure = '"';
/**
* Escape
* @var string
*/
private $escape = '\\';
/**
* Newline code
* @var string
*/
private $newline = "\r\n";
/**
* From charset
* @var string
*/
private $fromCharset = 'auto';
/**
* To charset
* @var string
*/
private $toCharset = null;
/**
* File mode
* @var string
*/
private $fileMode = CsvFileObject::FILE_MODE_WRITE;
/**
* The column headers.
* @var array
*/
private $columnHeaders = array();
/**
* Set delimiter
* @param string $delimiter
* @return ExporterConfig
*/
public function setDelimiter($delimiter)
{
$this->delimiter = $delimiter;
return $this;
}
/**
* Return delimiter
* @return string
*/
public function getDelimiter()
{
return $this->delimiter;
}
/**
* Set enclosure
* @param string $enclosure
* @return ExporterConfig
*/
public function setEnclosure($enclosure)
{
$this->enclosure = $enclosure;
return $this;
}
/**
* Return enclosure
* @return string
*/
public function getEnclosure()
{
return $this->enclosure;
}
/**
* Set escape
* @param string $escape
* @return ExporterConfig
*/
public function setEscape($escape)
{
$this->escape = $escape;
return $this;
}
/**
* Return escape
* @return string
*/
public function getEscape()
{
return $this->escape;
}
/**
* Set newline
* @param string $newline
* @return ExporterConfig
*/
public function setNewline($newline)
{
$this->newline = $newline;
return $this;
}
/**
* Return newline
* @return string
*/
public function getNewline()
{
return $this->newline;
}
/**
* Set from-character set
* @param string $fromCharset
* @return ExporterConfig
*/
public function setFromCharset($fromCharset)
{
$this->fromCharset = $fromCharset;
return $this;
}
/**
* Return from-character set
* @return string
*/
public function getFromCharset()
{
return $this->fromCharset;
}
/**
* Set to-character set
* @param string $toCharset
* @return ExporterConfig
*/
public function setToCharset($toCharset)
{
$this->toCharset = $toCharset;
return $this;
}
/**
* Return to-character set
* @return string
*/
public function getToCharset()
{
return $this->toCharset;
}
/**
* Set file mode
* @param string $fileMode
* @return ExporterConfig
*/
public function setFileMode($fileMode)
{
$this->fileMode = $fileMode;
return $this;
}
/**
* Return file mode
* @return string
*/
public function getFileMode()
{
return $this->fileMode;
}
/**
* Set the column headers.
* @param array $columnHeaders
* @return ExporterConfig
*/
public function setColumnHeaders(array $columnHeaders)
{
$this->columnHeaders = $columnHeaders;
return $this;
}
/**
* Get the column headers.
* @return array
*/
public function getColumnHeaders()
{
return $this->columnHeaders;
}
}

View File

@@ -0,0 +1,10 @@
<?php
namespace Goodby\CSV\Import\Protocol\Exception;
/**
* Throws if csv file not found
*/
class CsvFileNotFoundException extends \RuntimeException
{
}

View File

@@ -0,0 +1,10 @@
<?php
namespace Goodby\CSV\Import\Protocol\Exception;
/**
* Invalid lexical Exception
*/
class InvalidLexicalException extends \RuntimeException
{
}

View File

@@ -0,0 +1,15 @@
<?php
namespace Goodby\CSV\Import\Protocol;
/**
* Interface of the Interpreter
*/
interface InterpreterInterface
{
/**
* @param $line
* @return void
*/
public function interpret($line);
}

View File

@@ -0,0 +1,20 @@
<?php
namespace Goodby\CSV\Import\Protocol;
use Goodby\CSV\Import\Protocol\Exception\CsvFileNotFoundException;
/**
* Interface of Lexer
*/
interface LexerInterface
{
/**
* Parse csv file
* @param string $filename
* @param InterpreterInterface $interpreter
* @return boolean
* @throws CsvFileNotFoundException
*/
public function parse($filename, InterpreterInterface $interpreter);
}

View File

@@ -0,0 +1,7 @@
<?php
namespace Goodby\CSV\Import\Standard\Exception;
class StrictViolationException extends \RuntimeException
{
}

View File

@@ -0,0 +1,120 @@
<?php
namespace Goodby\CSV\Import\Standard;
use Goodby\CSV\Import\Protocol\InterpreterInterface;
use Goodby\CSV\Import\Protocol\Exception\InvalidLexicalException;
use Goodby\CSV\Import\Standard\Exception\StrictViolationException;
/**
* standard interpreter
*/
class Interpreter implements InterpreterInterface
{
/**
* @var array
*/
private $observers = array();
/**
* @var int
*/
private $rowConsistency = null;
/**
* @var bool
*/
private $strict = true;
/**
* interpret line
*
* @param $line
* @return void
* @throws \Goodby\CSV\Import\Protocol\Exception\InvalidLexicalException
*/
public function interpret($line)
{
$this->checkRowConsistency($line);
if (!is_array($line)) {
throw new InvalidLexicalException('line is must be array');
}
$this->notify($line);
}
public function unstrict()
{
$this->strict = false;
}
/**
* add observer
*
* @param callable $observer
*/
public function addObserver($observer)
{
$this->checkCallable($observer);
$this->observers[] = $observer;
}
/**
* notify to observers
*
* @param $line
*/
private function notify($line)
{
$observers = $this->observers;
foreach ($observers as $observer) {
$this->delegate($observer, $line);
}
}
/**
* delegate to observer
*
* @param $observer
* @param $line
*/
private function delegate($observer, $line)
{
call_user_func($observer, $line);
}
/**
* check observer is callable
*
* @param $observer
* @throws \InvalidArgumentException
*/
private function checkCallable($observer)
{
if (!is_callable($observer)) {
throw new \InvalidArgumentException('observer must be callable');
}
}
private function checkRowConsistency($line)
{
if (!$this->strict) {
return;
}
$current = count($line);
if ($this->rowConsistency === null) {
$this->rowConsistency = $current;
}
if ($current !== $this->rowConsistency) {
throw new StrictViolationException(sprintf('Column size should be %u, but %u columns given', $this->rowConsistency, $current));
}
$this->rowConsistency = $current;
}
}

View File

@@ -0,0 +1,69 @@
<?php
namespace Goodby\CSV\Import\Standard;
use Goodby\CSV\Import\Protocol\LexerInterface;
use Goodby\CSV\Import\Protocol\InterpreterInterface;
use Goodby\CSV\Import\Standard\StreamFilter\ConvertMbstringEncoding;
use SplFileObject;
class Lexer implements LexerInterface
{
/**
* @var LexerConfig
*/
private $config;
/**
* Return new Lexer object
* @param LexerConfig $config
*/
public function __construct(LexerConfig $config = null)
{
if (!$config) {
$config = new LexerConfig();
}
$this->config = $config;
ConvertMbstringEncoding::register();
}
/**
* {@inherit}
*/
public function parse($filename, InterpreterInterface $interpreter)
{
ini_set('auto_detect_line_endings', true); // For mac's office excel csv
$delimiter = $this->config->getDelimiter();
$enclosure = $this->config->getEnclosure();
$escape = $this->config->getEscape();
$fromCharset = $this->config->getFromCharset();
$toCharset = $this->config->getToCharset();
$flags = $this->config->getFlags();
$ignoreHeader = $this->config->getIgnoreHeaderLine();
if ( $fromCharset === null ) {
$url = $filename;
} else {
$url = ConvertMbstringEncoding::getFilterURL($filename, $fromCharset, $toCharset);
}
$csv = new SplFileObject($url);
$csv->setCsvControl($delimiter, $enclosure, $escape);
$csv->setFlags($flags);
$originalLocale = setlocale(LC_ALL, '0'); // Backup current locale
setlocale(LC_ALL, 'en_US.UTF-8');
foreach ( $csv as $lineNumber => $line ) {
if ($ignoreHeader && $lineNumber == 0 || (count($line) === 1 && trim($line[0]) === '')) {
continue;
}
$interpreter->interpret($line);
}
parse_str(str_replace(';', '&', $originalLocale), $locale_array);
setlocale(LC_ALL, $locale_array); // Reset locale
}
}

View File

@@ -0,0 +1,185 @@
<?php
namespace Goodby\CSV\Import\Standard;
use SplFileObject;
/**
* Config for Lexer object
*/
class LexerConfig
{
/**
* @var string
*/
private $delimiter = ',';
/**
* @var string
*/
private $enclosure = '"';
/**
* @var string
*/
private $escape = '\\';
/**
* @var string
*/
private $fromCharset;
/**
* @var string
*/
private $toCharset;
/**
* @var integer
*/
private $flags = SplFileObject::READ_CSV;
/**
* @var bool
*/
private $ignoreHeaderLine = false;
/**
* Set delimiter
* @param string $delimiter
* @return LexerConfig
*/
public function setDelimiter($delimiter)
{
$this->delimiter = $delimiter;
return $this;
}
/**
* Return delimiter
* @return string
*/
public function getDelimiter()
{
return $this->delimiter;
}
/**
* Set enclosure
* @param string $enclosure
* @return LexerConfig
*/
public function setEnclosure($enclosure)
{
$this->enclosure = $enclosure;
return $this;
}
/**
* Return enclosure
* @return string
*/
public function getEnclosure()
{
return $this->enclosure;
}
/**
* Set escape
* @param string $escape
* @return LexerConfig
*/
public function setEscape($escape)
{
$this->escape = $escape;
return $this;
}
/**
* Return escape
* @return string
*/
public function getEscape()
{
return $this->escape;
}
/**
* Set from-character set
* @param string $fromCharset
* @return LexerConfig
*/
public function setFromCharset($fromCharset)
{
$this->fromCharset = $fromCharset;
return $this;
}
/**
* Return from-character set
* @return string
*/
public function getFromCharset()
{
return $this->fromCharset;
}
/**
* Set to-character set
* @param string $toCharset
* @return LexerConfig
*/
public function setToCharset($toCharset)
{
$this->toCharset = $toCharset;
return $this;
}
/**
* Return to-character set
* @return string
*/
public function getToCharset()
{
return $this->toCharset;
}
/**
* Set flags
* @param integer $flags Bit mask of the flags to set. See SplFileObject constants for the available flags.
* @return LexerConfig
* @see http://php.net/manual/en/class.splfileobject.php#splfileobject.constants
*/
public function setFlags($flags)
{
$this->flags = $flags;
return $this;
}
/**
* Return flags
* @return integer
*/
public function getFlags()
{
return $this->flags;
}
/**
* @param $ignoreHeaderLine
* @return $this
*/
public function setIgnoreHeaderLine($ignoreHeaderLine)
{
$this->ignoreHeaderLine = $ignoreHeaderLine;
return $this;
}
/**
* @return boolean
*/
public function getIgnoreHeaderLine()
{
return $this->ignoreHeaderLine;
}
}

View File

@@ -0,0 +1,71 @@
<?php
namespace Goodby\CSV\Import\Standard\Observer;
class PdoObserver
{
private $table;
private $columns;
private $dsn;
private $options = null;
private $pdo = null;
public function __construct($table, $columns, $dsn, $options)
{
$this->table = $table;
$this->columns = $columns;
$this->dsn = $dsn;
$this->options = $options;
}
public function notify($line)
{
if ($this->pdo === null) {
$this->pdo = new \PDO($this->dsn, $this->options['user'], $this->options['password']);
}
$this->execute($line);
}
private function execute($line)
{
$line = array_map(function($value) {
$number = filter_var($value, FILTER_VALIDATE_INT);
if ($number !== false) {
return $number;
}
if (is_string($value)) {
if (strtolower($value) === 'null') {
return 'NULL';
}
if (strtolower($value) === 'true') {
return 1;
}
if (strtolower($value) === 'false') {
return 0;
}
return $value;
}
throw new \InvalidArgumentException('value is invalid: ' . var_export($value, 1));
}, $line);
$prepare = array_map(function() {
return '?';
}, $line);
$sql = 'INSERT INTO ' . $this->table . '(' . join(', ', $this->columns) . ')' .
' VALUES(' . join(',', $prepare) . ')';
$stmt = $this->pdo->prepare($sql);
$stmt->execute($line);
}
}

View File

@@ -0,0 +1,62 @@
<?php
namespace Goodby\CSV\Import\Standard\Observer;
class SqlObserver
{
private $table;
private $columns;
private $path;
private $file = null;
public function __construct($table, $columns, $path)
{
$this->table = $table;
$this->columns = $columns;
$this->path = $path;
}
public function notify($line)
{
$sql = $this->buildSql($line);
if ($this->file === null) {
$this->file = new \SplFileObject($this->path, 'a');
}
$this->file->fwrite($sql);
}
private function buildSql($line)
{
$line = array_map(function($value) {
$number = filter_var($value, FILTER_VALIDATE_INT);
if ($number !== false) {
return $number;
}
if (is_string($value)) {
if (strtolower($value) === 'null') {
return 'NULL';
}
if (strtolower($value) === 'true') {
return 'true';
}
if (strtolower($value) === 'false') {
return 'false';
}
return '"' . addslashes($value) . '"';
}
throw new \InvalidArgumentException('value is invalid: ' . var_export($value, 1));
}, $line);
return 'INSERT INTO ' . $this->table . '(' . join(', ', $this->columns) . ')' .
' VALUES(' . join(', ', $line) . ');';
}
}

View File

@@ -0,0 +1,110 @@
<?php
namespace Goodby\CSV\Import\Standard\StreamFilter;
use php_user_filter;
use RuntimeException;
class ConvertMbstringEncoding extends php_user_filter
{
/**
* @var string
*/
const FILTER_NAMESPACE = 'convert.mbstring.encoding.';
/**
* @var bool
*/
private static $hasBeenRegistered = false;
/**
* @var string
*/
private $fromCharset;
/**
* @var string
*/
private $toCharset;
/**
* Return filter name
* @return string
*/
public static function getFilterName()
{
return self::FILTER_NAMESPACE.'*';
}
/**
* Register this class as a stream filter
* @throws \RuntimeException
*/
public static function register()
{
if ( self::$hasBeenRegistered === true ) {
return;
}
if ( stream_filter_register(self::getFilterName(), __CLASS__) === false ) {
throw new RuntimeException('Failed to register stream filter: '.self::getFilterName());
}
self::$hasBeenRegistered = true;
}
/**
* Return filter URL
* @param string $filename
* @param string $fromCharset
* @param string $toCharset
* @return string
*/
public static function getFilterURL($filename, $fromCharset, $toCharset = null)
{
if ( $toCharset === null ) {
return sprintf('php://filter/convert.mbstring.encoding.%s/resource=%s', $fromCharset, $filename);
} else {
return sprintf('php://filter/convert.mbstring.encoding.%s:%s/resource=%s', $fromCharset, $toCharset, $filename);
}
}
/**
* @return bool
*/
public function onCreate()
{
if ( strpos($this->filtername, self::FILTER_NAMESPACE) !== 0 ) {
return false;
}
$parameterString = substr($this->filtername, strlen(self::FILTER_NAMESPACE));
if ( ! preg_match('/^(?P<from>[-\w]+)(:(?P<to>[-\w]+))?$/', $parameterString, $matches) ) {
return false;
}
$this->fromCharset = isset($matches['from']) ? $matches['from'] : 'auto';
$this->toCharset = isset($matches['to']) ? $matches['to'] : mb_internal_encoding();
return true;
}
/**
* @param string $in
* @param string $out
* @param string $consumed
* @param $closing
* @return int
*/
public function filter($in, $out, &$consumed, $closing)
{
while ( $bucket = stream_bucket_make_writeable($in) ) {
$bucket->data = mb_convert_encoding($bucket->data, $this->toCharset, $this->fromCharset);
$consumed += $bucket->datalen;
stream_bucket_append($out, $bucket);
}
return PSFS_PASS_ON;
}
}

View File

@@ -0,0 +1,54 @@
<?php
namespace Goodby\CSV\TestHelper;
class DbManager
{
private $pdo;
private $host;
private $db;
private $user;
private $pass;
public function __construct()
{
$this->host = $_SERVER['GOODBY_CSV_TEST_DB_HOST'];
$this->db = $_SERVER['GOODBY_CSV_TEST_DB_NAME_DEFAULT'];
$this->user = $_SERVER['GOODBY_CSV_TEST_DB_USER'];
$this->pass = $_SERVER['GOODBY_CSV_TEST_DB_PASS'];
$dsn = 'mysql:host=' . $this->host;
$this->pdo = new \PDO($dsn, $this->user, $this->pass);
$stmt = $this->pdo->prepare("CREATE DATABASE " . $this->db);
$stmt->execute();
}
public function __destruct()
{
$stmt = $this->pdo->prepare("DROP DATABASE " . $this->db);
$stmt->execute();
}
public function getPdo()
{
return new \PDO($this->getDsn(), $this->user, $this->pass);
}
public function getDsn()
{
return 'mysql:dbname=' . $this->db . ';host=' . $this->host;
}
public function getUser()
{
return $this->user;
}
public function getPassword()
{
return $this->pass;
}
}