diff options
author | Ignace Nyamagana Butera <nyamsprod@gmail.com> | 2014-08-07 08:46:27 +0200 |
---|---|---|
committer | Ignace Nyamagana Butera <nyamsprod@gmail.com> | 2014-08-07 08:46:27 +0200 |
commit | 781159e3167071eca3001420558dc1ebc8e7e22e (patch) | |
tree | e890ea08383db5ec5ddab0f0d80c823b19d169f7 /src | |
parent | 10bb871ed7c7287e746459ca190f771a1c2f00ae (diff) | |
download | csv-781159e3167071eca3001420558dc1ebc8e7e22e.zip csv-781159e3167071eca3001420558dc1ebc8e7e22e.tar.gz csv-781159e3167071eca3001420558dc1ebc8e7e22e.tar.bz2 |
adding createFromPath
Diffstat (limited to 'src')
-rw-r--r-- | src/AbstractCsv.php | 30 |
1 files changed, 27 insertions, 3 deletions
diff --git a/src/AbstractCsv.php b/src/AbstractCsv.php index 6c59aae..56a78e7 100644 --- a/src/AbstractCsv.php +++ b/src/AbstractCsv.php @@ -47,7 +47,7 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate /** * The constructor path * - * @var mixed can be a SplFileInfo object or the path to a file + * @var \SplFileInfo|string can be a SplFileInfo object or the path to a file */ protected $path; @@ -61,8 +61,8 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate /** * The constructor * - * @param mixed $path an SplFileInfo object or the path to a file - * @param string $open_mode the file open mode flag + * @param \SplFileInfo|string $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+') { @@ -90,6 +90,30 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate /** * Create a {@link AbstractCsv} from a string * + * @param \SplFileInfo|\SplFileObject|string $path an SplFileInfo object or the path to a file + * @param string $open_mode the file open mode flag + * + * @return self + * + * @throws \InvalidArgumentException If the data provided is invalid + */ + public static function createFromPath($path, $open_mode = 'r+') + { + if ($path instanceof SplTempFileObject) { + throw new InvalidArgumentException( + 'path must be a valid string or a `SplFileInfo` object' + ); + } elseif ($path instanceof SplFileInfo) { + $path = $path->getPath().'/'.$path->getBasename(); + } + $path = (string) $path; + + return new static($path, $open_mode); + } + + /** + * Create a {@link AbstractCsv} from a string + * * @param string $str The CSV data as string * * @return self |