diff options
-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 |