defaultFlags = SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY; $this->flags = $this->defaultFlags; $this->open_mode = strtolower($open_mode); $this->path = $path; $this->initStreamFilter($this->path); } /** * The destructor */ public function __destruct() { $this->path = null; } /** * Returns the CSV Iterator * * @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($this->flags); return $iterator; } /** * Returns the CSV Iterator for conversion * * @return Iterator */ protected function getConversionIterator() { $iterator = $this->getIterator(); $iterator->setFlags($this->defaultFlags); $iterator = $this->applyBomStripping($iterator); $iterator = $this->applyIteratorFilter($iterator); $iterator = $this->applyIteratorSortBy($iterator); return $this->applyIteratorInterval($iterator); } /** * Creates 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 * * * * * * @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); } /** * 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'); } /** * Creates a {@link AbstractCsv} from a SplFileObject * * The path can be: * - a SplFileObject, * - a SplTempFileObject * * * * * * @param SplFileObject $file * * @return static */ public static function createFromFileObject(SplFileObject $file) { return new static($file); } /** * Creates a {@link AbstractCsv} from a string * * The string must be an object that implements the `__toString` method, * or a string * * @param string $str the string * @param string $newline the newline character * * @return static */ public static function createFromString($str, $newline = "\n") { $file = new SplTempFileObject(); $file->fwrite(static::validateString($str)); $csv = static::createFromFileObject($file); $csv->setNewline($newline); return $csv; } /** * Creates 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->flags = $this->flags; $csv->input_bom = $this->input_bom; $csv->output_bom = $this->output_bom; $csv->newline = $this->newline; return $csv; } /** * Creates a {@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('\League\Csv\Writer', $open_mode); } /** * Creates a {@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('\League\Csv\Reader', $open_mode); } /** * Validate the submitted integer * * @param int $int * @param int $minValue * @param string $errorMessage * * @throws InvalidArgumentException If the value is invalid * * @return int */ protected function filterInteger($int, $minValue, $errorMessage) { if (false === ($int = filter_var($int, FILTER_VALIDATE_INT, ['options' => ['min_range' => $minValue]]))) { throw new InvalidArgumentException($errorMessage); } return $int; } }