summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/AbstractCsv.php60
-rw-r--r--src/Config/StreamFilter.php63
-rw-r--r--src/Writer.php32
3 files changed, 89 insertions, 66 deletions
diff --git a/src/AbstractCsv.php b/src/AbstractCsv.php
index bb1a7bb..32087ce 100644
--- a/src/AbstractCsv.php
+++ b/src/AbstractCsv.php
@@ -72,15 +72,11 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate
*/
public function __construct($path, $open_mode = 'r+')
{
- if (! $path instanceof SplFileInfo) {
- $path = (string) $path;
- $path = trim($path);
- }
ini_set("auto_detect_line_endings", '1');
- $this->path = $path;
+ $this->path = $this->normalizePath($path);
$this->open_mode = strtolower($open_mode);
- $this->initStreamFilter($path);
+ $this->initStreamFilter($this->path);
}
/**
@@ -95,11 +91,23 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate
/**
* Create a {@link AbstractCsv} from a string
*
- * The path must be an SplFileInfo, or a SplFileObject,
- * or an object that implements the `__toString` method,
- * or a string
+ * The path can be:
+ * - an SplFileInfo,
+ * - a SplFileObject,
+ * - an object that implements the `__toString` method,
+ * - a string
+ *
* BUT NOT a SplTempFileObject
*
+ * ```php
+ *<?php
+ * $csv = new Reader::createFromPath('/path/to/file.csv', 'a+');
+ * $csv = new Reader::createFromPath(new SplFileInfo('/path/to/file.csv'));
+ * $csv = new Reader::createFromPath(new SplFileObject('/path/to/file.csv'), 'rb');
+ *
+ * ?>
+ * ```
+ *
* @param \SplFileInfo|\SplFileObject|object|string $path file path
* @param string $open_mode the file open mode flag
*
@@ -124,6 +132,18 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate
/**
* Create a {@link AbstractCsv} from a SplFileObject
*
+ * The path can be:
+ * - a SplFileObject,
+ * - a SplTempFileObject
+ *
+ * ```php
+ *<?php
+ * $csv = new Writer::createFromFileObject(new SplFileInfo('/path/to/file.csv'));
+ * $csv = new Writer::createFromFileObject(new SplTempFileObject);
+ *
+ * ?>
+ * ```
+ *
* @param SplFileObject $obj
*
* @return static
@@ -159,6 +179,28 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate
}
/**
+ * Return a normalize path which could be a SplFileObject
+ * or a string path
+ *
+ * @param \SplFileInfo|object|string $path the filepath
+ *
+ * @return \SplFileObject|string
+ */
+ protected function normalizePath($path)
+ {
+ if ($path instanceof SplFileObject) {
+ return $path;
+ } elseif ($path instanceof SplFileInfo) {
+ return $path->getPath().'/'.$path->getBasename();
+ }
+
+ $path = (string) $path;
+ $path = trim($path);
+
+ return $path;
+ }
+
+ /**
* Create a {@link AbstractCsv} instance from another {@link AbstractCsv} object
*
* @param string $class_name the class to be instantiated
diff --git a/src/Config/StreamFilter.php b/src/Config/StreamFilter.php
index c46c970..90c2cff 100644
--- a/src/Config/StreamFilter.php
+++ b/src/Config/StreamFilter.php
@@ -15,7 +15,6 @@ namespace League\Csv\Config;
use LogicException;
use OutOfBoundsException;
use SplFileInfo;
-use SplTempFileObject;
/**
* A Trait to ease PHP Stream Filters manipulation
@@ -56,24 +55,20 @@ trait StreamFilter
* an object that implements the `__toString` method
* a path to a file
*
- * @param \SplFileInfo|object|string $path The file path
+ * @param \SplFileObject|string $path The file path
*
* @return void
*/
protected function initStreamFilter($path)
{
- if ($path instanceof SplTempFileObject) {
- $this->stream_real_path = null;
-
- return $this;
-
- } elseif ($path instanceof SplFileInfo) {
- //$path->getRealPath() returns false for php stream wrapper
- $path = $path->getPath().'/'.$path->getBasename();
+ $this->stream_filters = [];
+ $this->stream_real_path = null;
+ if (! is_string($path)) {
+ return;
}
- $path = (string) $path;
- $path = trim($path);
+ $this->stream_real_path = $path;
+
//if we are submitting a filter meta wrapper
//we extract and inject the mode, the filter and the path
if (preg_match(
@@ -91,12 +86,8 @@ trait StreamFilter
$this->stream_filter_mode = $mode;
$this->stream_real_path = $matches['resource'];
$this->stream_filters = explode('|', $matches['filters']);
-
- return $this;
}
- $this->stream_real_path = $path;
- $this->stream_filters = [];
}
/**
@@ -108,12 +99,22 @@ trait StreamFilter
*/
protected function checkStreamApiAvailability()
{
- if (is_null($this->stream_real_path)) {
+ if (! is_string($this->stream_real_path)) {
throw new LogicException('The stream filter API can not be used');
}
}
/**
+ * Tells whether the stream filter capabilities can be used
+ *
+ * @return boolean
+ */
+ public function isActiveStreamFilter()
+ {
+ return is_string($this->stream_real_path);
+ }
+
+ /**
* stream filter mode Setter
*
* Set the new Stream Filter mode and remove all
@@ -121,9 +122,9 @@ trait StreamFilter
*
* @param integer $mode
*
- * @return self
+ * @return static
*
- * @throws \LogicException If the API can not be use
+ * @throws \OutOfBoundsException If the mode is invalid
*/
public function setStreamFilterMode($mode)
{
@@ -142,8 +143,6 @@ trait StreamFilter
* stream filter mode getter
*
* @return integer
- *
- * @throws \LogicException If the API can not be use
*/
public function getStreamFilterMode()
{
@@ -158,8 +157,6 @@ trait StreamFilter
* @param string $filter_name the stream filter name
*
* @return string
- *
- * @throws \LogicException If the API can not be use
*/
protected function sanitizeStreamFilter($filter_name)
{
@@ -174,9 +171,7 @@ trait StreamFilter
*
* @param string $filter_name a string or an object that implements the '__toString' method
*
- * @return self
- *
- * @throws \LogicException If the API can not be use
+ * @return static
*/
public function appendStreamFilter($filter_name)
{
@@ -191,9 +186,7 @@ trait StreamFilter
*
* @param string $filter_name a string or an object that implements the '__toString' method
*
- * @return self
- *
- * @throws \LogicException If the API can not be use
+ * @return static
*/
public function prependStreamFilter($filter_name)
{
@@ -209,8 +202,6 @@ trait StreamFilter
* @param string $filter_name
*
* @return boolean
- *
- * @throws \LogicException If the API can not be use
*/
public function hasStreamFilter($filter_name)
{
@@ -224,9 +215,7 @@ trait StreamFilter
*
* @param string $filter_name
*
- * @return self
- *
- * @throws \LogicException If the API can not be use
+ * @return static
*/
public function removeStreamFilter($filter_name)
{
@@ -242,9 +231,7 @@ trait StreamFilter
/**
* Remove all registered stream filter
*
- * @return self
- *
- * @throws \LogicException If the API can not be use
+ * @return static
*/
public function clearStreamFilter()
{
@@ -258,8 +245,6 @@ trait StreamFilter
* Return the filter path
*
* @return string
- *
- * @throws \LogicException If the API can not be use
*/
protected function getStreamFilterPath()
{
diff --git a/src/Writer.php b/src/Writer.php
index 532f2f1..0ae9154 100644
--- a/src/Writer.php
+++ b/src/Writer.php
@@ -114,6 +114,16 @@ class Writer extends AbstractCsv
}
/**
+ * Tells whether the stream filter capabilities can be used
+ *
+ * @return boolean
+ */
+ public function isActiveStreamFilter()
+ {
+ return parent::isActiveStreamFilter() && is_null($this->csv);
+ }
+
+ /**
* Format the row according to the null handling behavior
*
* @param array $row
@@ -201,7 +211,7 @@ class Writer extends AbstractCsv
/**
* Is the submitted row valid
*
- * @param \Traversable|array|string $row
+ * @param string[]|string $row
*
* @return array
*
@@ -209,17 +219,10 @@ class Writer extends AbstractCsv
*/
protected function validateRow($row)
{
- //convert input string row into a proper array
- if (self::isValidString($row)) {
+ if (! is_array($row)) {
$row = str_getcsv((string) $row, $this->delimiter, $this->enclosure, $this->escape);
}
- if (!is_array($row)) {
- throw new InvalidArgumentException(
- 'the data provided must be convertible into arrays'
- );
- }
-
foreach ($row as $value) {
if (!$this->isConvertibleContent($value)) {
throw new InvalidArgumentException(
@@ -252,20 +255,13 @@ class Writer extends AbstractCsv
* avoid loosing the cursor position
*
* @return SplFileObject
- *
- * @throws \RuntimeException If the file could not be created and/or opened
*/
protected function getCsv()
{
if (! is_null($this->csv)) {
return $this->csv;
- } elseif ($this->path instanceof SplFileObject) {
- $this->csv = $this->path;
-
- return $this->csv;
}
-
- $this->csv = new SplFileObject($this->getStreamFilterPath(), $this->open_mode);
+ $this->csv = $this->getIterator();
return $this->csv;
}
@@ -273,7 +269,7 @@ class Writer extends AbstractCsv
/**
* Add a new CSV row to the generated CSV
*
- * @param array|string $data a string, an array or an object implementing to '__toString' method
+ * @param string[]|string $data a string, an array or an object implementing to '__toString' method
*
* @return self
*