open_mode = strtolower($open_mode); $this->path = $path; $this->initStreamFilter($this->path); } /** * The destructor */ public function __destruct() { $this->path = null; } /** * Return a new {@link AbstractCsv} from a SplFileObject * * @param SplFileObject $file * * @return static */ public static function createFromFileObject(SplFileObject $file) { $csv = new static($file); $controls = $file->getCsvControl(); $csv->setDelimiter($controls[0]); $csv->setEnclosure($controls[1]); if (isset($controls[2])) { $csv->setEscape($controls[2]); } return $csv; } /** * Return a new {@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 * * @return static */ public static function createFromString($str) { $file = new SplTempFileObject(); $file->fwrite(static::validateString($str)); return new static($file); } /** * validate a string * * @param mixed $str the value to evaluate as a string * * @throws InvalidArgumentException if the submitted data can not be converted to string * * @return string */ protected static function validateString($str) { if (is_string($str) || (is_object($str) && method_exists($str, '__toString'))) { return (string) $str; } throw new InvalidArgumentException('Expected data must be a string or stringable'); } /** * Return a new {@link AbstractCsv} from a string * * @param mixed $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'); } if ($path instanceof SplFileInfo) { $path = $path->getPath().'/'.$path->getBasename(); } return new static(static::validateString($path), $open_mode); } /** * Return a new {@link AbstractCsv} instance from another {@link AbstractCsv} object * * @param string $class the class to be instantiated * @param string $open_mode the file open mode flag * * @return static */ protected function newInstance($class, $open_mode) { $csv = new $class($this->path, $open_mode); $csv->delimiter = $this->delimiter; $csv->enclosure = $this->enclosure; $csv->escape = $this->escape; $csv->input_encoding = $this->input_encoding; $csv->input_bom = $this->input_bom; $csv->output_bom = $this->output_bom; $csv->newline = $this->newline; return $csv; } /** * Return a new {@link Writer} instance from a {@link AbstractCsv} object * * @param string $open_mode the file open mode flag * * @return Writer */ public function newWriter($open_mode = 'r+') { return $this->newInstance(Writer::class, $open_mode); } /** * Return a new {@link Reader} instance from a {@link AbstractCsv} object * * @param string $open_mode the file open mode flag * * @return Reader */ public function newReader($open_mode = 'r+') { return $this->newInstance(Reader::class, $open_mode); } /** * Returns the inner SplFileObject * * @return SplFileObject */ public function getIterator() { $iterator = $this->path; if (!$iterator instanceof SplFileObject) { $iterator = new SplFileObject($this->getStreamFilterPath(), $this->open_mode); } $iterator->setCsvControl($this->delimiter, $this->enclosure, $this->escape); $iterator->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY); return $iterator; } }