LICENSE000066600000002052152141760240005556 0ustar00Copyright (c) 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. phpunit-bootstrap.php000066600000001640152141760240010766 0ustar00host = $_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; } } src/Goodby/CSV/Import/Standard/Observer/PdoObserver.php000066600000003220152141760240016760 0ustar00table = $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); } } src/Goodby/CSV/Import/Standard/Observer/SqlObserver.php000066600000002725152141760240017006 0ustar00table = $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) . ');'; } } src/Goodby/CSV/Import/Standard/Lexer.php000066600000004032152141760240014020 0ustar00config = $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 } } src/Goodby/CSV/Import/Standard/Interpreter.php000066600000004653152141760240015255 0ustar00checkRowConsistency($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; } } src/Goodby/CSV/Import/Standard/LexerConfig.php000066600000006261152141760240015154 0ustar00delimiter = $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; } } src/Goodby/CSV/Import/Standard/StreamFilter/ConvertMbstringEncoding.php000066600000005320152141760240022140 0ustar00filtername, self::FILTER_NAMESPACE) !== 0 ) { return false; } $parameterString = substr($this->filtername, strlen(self::FILTER_NAMESPACE)); if ( ! preg_match('/^(?P[-\w]+)(:(?P[-\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; } } src/Goodby/CSV/Import/Standard/Exception/StrictViolationException.php000066600000000165152141760240021716 0ustar00config = $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; } } src/Goodby/CSV/Export/Standard/Collection/PdoCollection.php000066600000003524152141760240017606 0ustar00stmt = $stmt; $this->rowCount = $this->stmt->rowCount(); } /** * (PHP 5 >= 5.0.0)
* 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 >= 5.0.0)
* 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 >= 5.0.0)
* 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 >= 5.0.0)
* 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 >= 5.0.0)
* 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; } } src/Goodby/CSV/Export/Standard/Collection/CallbackCollection.php000066600000004452152141760240020561 0ustar00callable = $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 >= 5.0.0)
* 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 >= 5.0.0)
* 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 >= 5.0.0)
* 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 >= 5.0.0)
* 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 >= 5.0.0)
* 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(); } } src/Goodby/CSV/Export/Standard/ExporterConfig.php000066600000007254152141760240015717 0ustar00delimiter = $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; } } src/Goodby/CSV/Export/Standard/CsvFileObject.php000066600000004006152141760240015433 0ustar00newline = $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); } } src/Goodby/CSV/Export/Standard/Exception/StrictViolationException.php000066600000000165152141760240021725 0ustar00