Skip to content

Commit

Permalink
change default open_mode to r+
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Apr 2, 2014
1 parent 48fe2cd commit 1b29cf3
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 75 deletions.
101 changes: 62 additions & 39 deletions src/AbstractCsv.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,42 +99,13 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate
*/
protected $encoding = 'UTF-8';

/**
* Set the CSV encoding charset
*
* @param string $str
*
* @return self
*/
public function setEncoding($str)
{
$str = str_replace('_', '-', $str);
$str = filter_var($str, FILTER_SANITIZE_STRING, ['flags' => FILTER_FLAG_STRIP_LOW|FILTER_FLAG_STRIP_HIGH]);
if (empty($str)) {
throw new InvalidArgumentException('you should use a valid charset');
}
$this->encoding = strtoupper($str);

return $this;
}

/**
* Get the CSV encoding charset
*
* @return string
*/
public function getEncoding()
{
return $this->encoding;
}

/**
* The constructor
*
* @param mixed $path an SplFileInfo object or the path to a file
* @param string $open_mode the file open mode flag
*/
public function __construct($path, $open_mode = 'r')
public function __construct($path, $open_mode = 'r+')
{
$this->setIterator($path, $open_mode);
}
Expand Down Expand Up @@ -388,26 +359,55 @@ public function getIterator()
}

/**
* JsonSerializable Interface
* Set the CSV encoding charset
*
* @return array
* @param string $str
*
* @return self
*/
public function jsonSerialize()
public function setEncoding($str)
{
return iterator_to_array($this->convert2Utf8(), false);
$str = str_replace('_', '-', $str);
$str = filter_var($str, FILTER_SANITIZE_STRING, ['flags' => FILTER_FLAG_STRIP_LOW|FILTER_FLAG_STRIP_HIGH]);
if (empty($str)) {
throw new InvalidArgumentException('you should use a valid charset');
}
$this->encoding = strtoupper($str);

return $this;
}

/**
* Retrieves the CSV content
* Get the CSV encoding charset
*
* @return string
*/
public function __toString()
public function getEncoding()
{
ob_start();
$this->output();
return $this->encoding;
}

return ob_get_clean();
/**
* Instantiate a {@link Writer} class from the current {@link Reader}
*
* @param string $open_mode the file open mode flag
*
* @return \League\Csv\AbstractCSv
*/
protected function getInstance($class_name, $open_mode = 'r+')
{
$obj = $this->csv;
if (! $obj instanceof SplTempFileObject && ($path = $obj->getRealPath()) !== false) {
$obj = new SplFileObject($path, $open_mode);
}
$csv = new $class_name($obj);
$csv->setDelimiter($this->delimiter);
$csv->setEnclosure($this->enclosure);
$csv->setEscape($this->escape);
$csv->setFlags($this->flags);
$csv->setEncoding($this->encoding);

return $csv;
}

/**
Expand All @@ -431,6 +431,16 @@ protected function convert2Utf8()
});
}

/**
* JsonSerializable Interface
*
* @return array
*/
public function jsonSerialize()
{
return iterator_to_array($this->convert2Utf8(), false);
}

/**
* Output all data on the CSV file
*
Expand All @@ -452,6 +462,19 @@ public function output($filename = null)
$iterator->fpassthru();
}

/**
* Retrieves the CSV content
*
* @return string
*/
public function __toString()
{
ob_start();
$this->output();

return ob_get_clean();
}

/**
* transform a CSV into a XML
*
Expand Down
18 changes: 2 additions & 16 deletions src/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@
*/
namespace League\Csv;

use SplFileObject;
use SplTempFileObject;

use InvalidArgumentException;

use CallbackFilterIterator;
Expand Down Expand Up @@ -223,19 +220,8 @@ public function fetchCol($columnIndex = 0, callable $callable = null)
*
* @return \League\Csv\Writer
*/
public function getWriter($open_mode = 'w')
public function getWriter($open_mode = 'r+')
{
$obj = $this->csv;
if (! $obj instanceof SplTempFileObject) {
$obj = new SplFileObject($obj->getRealPath(), $open_mode);
}
$csv = new Writer($obj);
$csv->setDelimiter($this->delimiter);
$csv->setEnclosure($this->enclosure);
$csv->setEscape($this->escape);
$csv->setFlags($this->flags);
$csv->setEncoding($this->encoding);

return $csv;
return $this->getInstance('\League\Csv\Writer', $open_mode);
}
}
24 changes: 4 additions & 20 deletions src/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,6 @@ class Writer extends AbstractCsv
*/
protected $null_handling_mode = self::NULL_AS_EXCEPTION;

/**
* The constructor
*
* @param mixed $path an SplFileInfo object or the path to a file
* @param string $open_mode the file open mode flag
*/
public function __construct($path, $open_mode = 'w')
{
parent::__construct($path, $open_mode);
}

/**
* Tell the class how to handle null value
*
Expand Down Expand Up @@ -198,17 +187,12 @@ public function insertAll($rows)
/**
* Instantiate a {@link Reader} class from the current {@link Writer}
*
* @param string $open_mode the file open mode flag
*
* @return \League\Csv\Reader
*/
public function getReader()
public function getReader($open_mode = 'r+')
{
$csv = new Reader($this->csv);
$csv->setDelimiter($this->delimiter);
$csv->setEnclosure($this->enclosure);
$csv->setEscape($this->escape);
$csv->setFlags($this->flags);
$csv->setEncoding($this->encoding);

return $csv;
return $this->getInstance('\League\Csv\Reader', $open_mode);
}
}

0 comments on commit 1b29cf3

Please sign in to comment.