summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorIgnace Nyamagana Butera <nyamsprod@gmail.com>2015-01-23 12:18:45 +0100
committerIgnace Nyamagana Butera <nyamsprod@gmail.com>2015-01-23 12:18:45 +0100
commit4d87e2437cec7e9cf5803e9599929e9b13a2e0d4 (patch)
tree202bd6a35b2f35922bccf853bdaf756dc123da56 /src
parent5ffb2b42ed5d7ae20d80d4480b34743f5086d4a8 (diff)
downloadcsv-4d87e2437cec7e9cf5803e9599929e9b13a2e0d4.zip
csv-4d87e2437cec7e9cf5803e9599929e9b13a2e0d4.tar.gz
csv-4d87e2437cec7e9cf5803e9599929e9b13a2e0d4.tar.bz2
adding Factory trait
Diffstat (limited to 'src')
-rw-r--r--src/AbstractCsv.php153
-rw-r--r--src/Config/Factory.php159
2 files changed, 168 insertions, 144 deletions
diff --git a/src/AbstractCsv.php b/src/AbstractCsv.php
index 5b332bf..c29135d 100644
--- a/src/AbstractCsv.php
+++ b/src/AbstractCsv.php
@@ -12,17 +12,14 @@
*/
namespace League\Csv;
-use InvalidArgumentException;
use IteratorAggregate;
use JsonSerializable;
use League\Csv\Config\Controls;
+use League\Csv\Config\Factory;
use League\Csv\Config\Output;
use League\Csv\Config\StreamFilter;
-use League\Csv\Iterator\MapIterator;
use SplFileInfo;
use SplFileObject;
-use SplTempFileObject;
-use Traversable;
/**
* An abstract class to enable basic CSV manipulation
@@ -80,6 +77,11 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate
use Controls;
/**
+ * Csv Factory Trait
+ */
+ use Factory;
+
+ /**
* Csv Ouputting Trait
*/
use Output;
@@ -101,9 +103,9 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate
*/
public function __construct($path, $open_mode = 'r+')
{
- ini_set("auto_detect_line_endings", '1');
+ ini_set('auto_detect_line_endings', '1');
- $this->path = $this->normalizePath($path);
+ $this->path = $this->normalizePath($path);
$this->open_mode = strtolower($open_mode);
$this->initStreamFilter($this->path);
}
@@ -124,10 +126,7 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate
return $path->getPath().'/'.$path->getBasename();
}
- $path = (string) $path;
- $path = trim($path);
-
- return $path;
+ return trim($path);
}
/**
@@ -140,140 +139,6 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate
}
/**
- * Create a {@link AbstractCsv} from a string
- *
- * The path can be:
- * - an SplFileInfo,
- * - a SplFileObject,
- * - an object that implements the `__toString` method,
- * - a string
- *
- * BUT NOT a SplTempFileObject
- *
- * <code>
- *<?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');
- *
- * ?>
- * </code>
- *
- * @param object|string $path file path
- * @param string $open_mode the file open mode flag
- *
- * @throws \InvalidArgumentException If $path is a \SplTempFileObject object
- *
- * @return static
- */
- public static function createFromPath($path, $open_mode = 'r+')
- {
- if ($path instanceof SplTempFileObject) {
- throw new InvalidArgumentException('an `SplTempFileObject` object does not contain a valid path');
- } elseif ($path instanceof SplFileInfo) {
- $path = $path->getPath().'/'.$path->getBasename();
- }
-
- $path = (string) $path;
- $path = trim($path);
-
- return new static($path, $open_mode);
- }
-
- /**
- * Create a {@link AbstractCsv} from a SplFileObject
- *
- * The path can be:
- * - a SplFileObject,
- * - a SplTempFileObject
- *
- * <code>
- *<?php
- * $csv = new Writer::createFromFileObject(new SplFileInfo('/path/to/file.csv'));
- * $csv = new Writer::createFromFileObject(new SplTempFileObject);
- *
- * ?>
- * </code>
- *
- * @param SplFileObject $obj
- *
- * @return static
- */
- public static function createFromFileObject(SplFileObject $obj)
- {
- return new static($obj);
- }
-
- /**
- * Create a {@link AbstractCsv} from a string
- *
- * The string must be an object that implements the `__toString` method,
- * or a string
- *
- * @param string|object $str the string
- *
- * @throws \InvalidArgumentException If the data provided is invalid
- *
- * @return static
- */
- public static function createFromString($str)
- {
- if (! self::isValidString($str)) {
- throw new InvalidArgumentException(
- 'the submitted data must be a string or an object implementing the `__toString` method'
- );
- }
- $obj = new SplTempFileObject();
- $obj->fwrite(rtrim($str)."\n");
-
- return static::createFromFileObject($obj);
- }
-
- /**
- * Create a {@link AbstractCsv} instance from another {@link AbstractCsv} object
- *
- * @param string $class_name the class to be instantiated
- * @param string $open_mode the file open mode flag
- *
- * @return static
- */
- protected function newInstance($class_name, $open_mode)
- {
- $csv = new $class_name($this->path, $open_mode);
- $csv->delimiter = $this->delimiter;
- $csv->enclosure = $this->enclosure;
- $csv->escape = $this->escape;
- $csv->encodingFrom = $this->encodingFrom;
- $csv->bom = $this->bom;
-
- return $csv;
- }
-
- /**
- * Create a {@link Writer} instance from a {@link AbstractCsv} object
- *
- * @param string $open_mode the file open mode flag
- *
- * @return \League\Csv\Writer
- */
- public function newWriter($open_mode = 'r+')
- {
- return $this->newInstance('\League\Csv\Writer', $open_mode);
- }
-
- /**
- * Create a {@link Reader} instance from a {@link AbstractCsv} object
- *
- * @param string $open_mode the file open mode flag
- *
- * @return \League\Csv\Reader
- */
- public function newReader($open_mode = 'r+')
- {
- return $this->newInstance('\League\Csv\Reader', $open_mode);
- }
-
- /**
* Return the CSV Iterator
*
* @return \SplFileObject
diff --git a/src/Config/Factory.php b/src/Config/Factory.php
new file mode 100644
index 0000000..53e039b
--- /dev/null
+++ b/src/Config/Factory.php
@@ -0,0 +1,159 @@
+<?php
+/**
+* This file is part of the League.csv library
+*
+* @license http://opensource.org/licenses/MIT
+* @link https://github.com/thephpleague/csv/
+* @version 6.X.X
+* @package League.csv
+*
+* For the full copyright and license information, please view the LICENSE
+* file that was distributed with this source code.
+*/
+namespace League\Csv\Config;
+
+use InvalidArgumentException;
+use SplFileInfo;
+use SplFileObject;
+use SplTempFileObject;
+
+/**
+ * A trait to facilate class instantiation
+ *
+ * @package League.csv
+ * @since 6.X.X
+ *
+ */
+trait Factory
+{
+ /**
+ * Create a {@link AbstractCsv} from a string
+ *
+ * The path can be:
+ * - an SplFileInfo,
+ * - a SplFileObject,
+ * - an object that implements the `__toString` method,
+ * - a string
+ *
+ * BUT NOT a SplTempFileObject
+ *
+ * <code>
+ *<?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');
+ *
+ * ?>
+ * </code>
+ *
+ * @param object|string $path file path
+ * @param string $open_mode the file open mode flag
+ *
+ * @throws \InvalidArgumentException If $path is a \SplTempFileObject object
+ *
+ * @return static
+ */
+ public static function createFromPath($path, $open_mode = 'r+')
+ {
+ if ($path instanceof SplTempFileObject) {
+ throw new InvalidArgumentException('an `SplTempFileObject` object does not contain a valid path');
+ } elseif ($path instanceof SplFileInfo) {
+ $path = $path->getPath().'/'.$path->getBasename();
+ }
+
+ return new static(trim($path), $open_mode);
+ }
+
+ /**
+ * Create a {@link AbstractCsv} from a SplFileObject
+ *
+ * The path can be:
+ * - a SplFileObject,
+ * - a SplTempFileObject
+ *
+ * <code>
+ *<?php
+ * $csv = new Writer::createFromFileObject(new SplFileInfo('/path/to/file.csv'));
+ * $csv = new Writer::createFromFileObject(new SplTempFileObject);
+ *
+ * ?>
+ * </code>
+ *
+ * @param SplFileObject $obj
+ *
+ * @return static
+ */
+ public static function createFromFileObject(SplFileObject $obj)
+ {
+ return new static($obj);
+ }
+
+ /**
+ * Create a {@link AbstractCsv} from a string
+ *
+ * The string must be an object that implements the `__toString` method,
+ * or a string
+ *
+ * @param string|object $str the string
+ *
+ * @throws \InvalidArgumentException If the data provided is invalid
+ *
+ * @return static
+ */
+ public static function createFromString($str)
+ {
+ if (! self::isValidString($str)) {
+ throw new InvalidArgumentException(
+ 'the submitted data must be a string or an object implementing the `__toString` method'
+ );
+ }
+ $obj = new SplTempFileObject();
+ $obj->fwrite(rtrim($str)."\n");
+
+ return static::createFromFileObject($obj);
+ }
+
+ /**
+ * Create a {@link AbstractCsv} instance from another {@link AbstractCsv} object
+ *
+ * @param string $class_name the class to be instantiated
+ * @param string $open_mode the file open mode flag
+ *
+ * @return static
+ */
+ protected function newInstance($class_name, $open_mode)
+ {
+ $csv = new $class_name($this->path, $open_mode);
+ $csv->delimiter = $this->delimiter;
+ $csv->enclosure = $this->enclosure;
+ $csv->escape = $this->escape;
+ $csv->encodingFrom = $this->encodingFrom;
+ $csv->bom = $this->bom;
+
+ return $csv;
+ }
+
+ /**
+ * Create a {@link Writer} instance from a {@link AbstractCsv} object
+ *
+ * @param string $open_mode the file open mode flag
+ *
+ * @return \League\Csv\Writer
+ */
+ public function newWriter($open_mode = 'r+')
+ {
+ return $this->newInstance('\League\Csv\Writer', $open_mode);
+ }
+
+ /**
+ * Create a {@link Reader} instance from a {@link AbstractCsv} object
+ *
+ * @param string $open_mode the file open mode flag
+ *
+ * @return \League\Csv\Reader
+ */
+ public function newReader($open_mode = 'r+')
+ {
+ return $this->newInstance('\League\Csv\Reader', $open_mode);
+ }
+}