diff options
-rw-r--r-- | CHANGELOG.md | 3 | ||||
-rw-r--r-- | src/AbstractCsv.php | 95 | ||||
-rw-r--r-- | src/Config/Factory.php | 121 | ||||
-rw-r--r-- | test/FactoryTest.php | 7 |
4 files changed, 89 insertions, 137 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 08ae398..0295c00 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ All Notable changes to `League\Csv` will be documented in this file - `Writer::useValidation` to enable/disabled complete validation when inserting new data. ### Deprecated -- Using `Writer` and `Reader` default constructor is deprecated you should favor the use of named constructors. The default constructor won't be accessible anymore in the next MAJOR version. +- Nothing ### Fixed - `jsonSerialize`, `toXML` and `toHTML` output can be modified using `Reader` query options methods. @@ -23,6 +23,7 @@ All Notable changes to `League\Csv` will be documented in this file ### Remove - Setting `ini_set("auto_detect_line_endings", true);` is no longer set in the class constructor. Mac OS X users must explicitly set this ini options in their script. +- Using `Writer` and `Reader` default constructor removed in favor of the use of named constructors. ## 6.3.0 - 2015-01-21 diff --git a/src/AbstractCsv.php b/src/AbstractCsv.php index 4f6765c..cc0c065 100644 --- a/src/AbstractCsv.php +++ b/src/AbstractCsv.php @@ -12,11 +12,13 @@ */ namespace League\Csv; +use InvalidArgumentException; use IteratorAggregate; use JsonSerializable; use League\Csv\Config; use SplFileInfo; use SplFileObject; +use SplTempFileObject; /** * An abstract class to enable basic CSV manipulation @@ -74,11 +76,6 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate use Config\Controls; /** - * Csv Factory Trait - */ - use Config\Factory; - - /** * Csv Ouputting Trait */ use Config\Output; @@ -100,7 +97,7 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate * @param object|string $path The file path * @param string $open_mode the file open mode flag */ - public function __construct($path, $open_mode = 'r+') + protected function __construct($path, $open_mode = 'r+') { $this->flags = SplFileObject::READ_CSV|SplFileObject::DROP_NEW_LINE; $this->open_mode = strtolower($open_mode); @@ -120,8 +117,6 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate { if ($path instanceof SplFileObject) { return $path; - } elseif ($path instanceof SplFileInfo) { - return $path->getPath().'/'.$path->getBasename(); } return trim($path); @@ -136,6 +131,90 @@ 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(); + } + + 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 + * @param string $newline the newline character + * + * @return static + */ + public static function createFromString($str, $newline = "\n") + { + $file = new SplTempFileObject(); + $file->fwrite(rtrim($str).$newline); + + $obj = static::createFromFileObject($file); + $obj->setNewline($newline); + + return $obj; + } + + /** * Return the CSV Iterator * * @return \SplFileObject diff --git a/src/Config/Factory.php b/src/Config/Factory.php deleted file mode 100644 index 4a10481..0000000 --- a/src/Config/Factory.php +++ /dev/null @@ -1,121 +0,0 @@ -<?php -/** -* This file is part of the League.csv library -* -* @license http://opensource.org/licenses/MIT -* @link https://github.com/thephpleague/csv/ -* @version 7.0.0 -* @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 7.0.0 - * - */ -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 - * @param string $newline the newline character - * - * @return static - */ - public static function createFromString($str, $newline = "\n") - { - $file = new SplTempFileObject(); - $file->fwrite(rtrim($str).$newline); - - $obj = static::createFromFileObject($file); - $obj->setNewline($newline); - - return $obj; - } - - /** - * set the newline sequence characters - * - * @param string $newline - * - * @return static - */ - abstract public function setNewline($newline); -} diff --git a/test/FactoryTest.php b/test/FactoryTest.php index 178149a..7a85f92 100644 --- a/test/FactoryTest.php +++ b/test/FactoryTest.php @@ -31,13 +31,6 @@ class FactoryTest extends PHPUnit_Framework_TestCase $this->assertSame($path, $csv->getIterator()->getRealPath()); } - public function testConstructorWithSplFileInfo() - { - $path = __DIR__.'/foo.csv'; - $csv = new Reader(new SplFileInfo($path)); - $this->assertSame($path, $csv->getIterator()->getRealPath()); - } - public function testCreateFromPathWithPHPWrapper() { $path = __DIR__.'/foo.csv'; |