Skip to content

Commit

Permalink
Adding createFromStream #202 (#203)
Browse files Browse the repository at this point in the history
* Adding createFromStream #202

This commit adds the ability to work with resource stream by
adding a `StreamIterator` object that mimick SplFileObject and takes
a stream resource as its unique constructor argument. To work
as intended the resource stream needs to be seekable.
  • Loading branch information
nyamsprod authored Jan 24, 2017
1 parent 35706b9 commit 37c2257
Show file tree
Hide file tree
Showing 15 changed files with 630 additions and 72 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@

All Notable changes to `Csv` will be documented in this file

## Next

### Added

- `AbstractCsv::createFromStream` to enable working with resource stream [issue #202](https://github.com/thephpleague/csv/issues/202)

### Deprecated

- None

### Fixed

- None

### Removed

- None

## 8.1.2 - 2016-10-27

### Added
Expand Down
48 changes: 41 additions & 7 deletions src/AbstractCsv.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use League\Csv\Config\Output;
use League\Csv\Modifier\QueryFilter;
use League\Csv\Modifier\StreamFilter;
use League\Csv\Modifier\StreamIterator;
use SplFileInfo;
use SplFileObject;
use SplTempFileObject;
Expand Down Expand Up @@ -88,8 +89,8 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate
* an object that implements the `__toString` method
* a path to a file
*
* @param SplFileObject|string $path The file path
* @param string $open_mode The file open mode flag
* @param StreamIterator|SplFileObject|string $path The file path
* @param string $open_mode The file open mode flag
*/
protected function __construct($path, $open_mode = 'r+')
{
Expand Down Expand Up @@ -126,6 +127,28 @@ public static function createFromFileObject(SplFileObject $file)
return $csv;
}

/**
* Return a new {@link AbstractCsv} from a PHP resource stream or a StreamIterator
*
* @param StreamIterator|resource $stream
*
* @return static
*/
public static function createFromStream($stream)
{
if (!$stream instanceof StreamIterator) {
$stream = new StreamIterator($stream);
}

$csv = new static($stream);
$controls = $stream->getCsvControl();
$csv->setDelimiter($controls[0]);
$csv->setEnclosure($controls[1]);
$csv->setEscape($controls[2]);

return $csv;
}

/**
* Return a new {@link AbstractCsv} from a string
*
Expand Down Expand Up @@ -233,17 +256,28 @@ public function newReader($open_mode = 'r+')
/**
* Returns the inner SplFileObject
*
* @return SplFileObject
* @return StreamIterator|SplFileObject
*/
public function getIterator()
{
$iterator = $this->path;
if (!$iterator instanceof SplFileObject) {
$iterator = new SplFileObject($this->getStreamFilterPath(), $this->open_mode);
}
$iterator = $this->setIterator();
$iterator->setCsvControl($this->delimiter, $this->enclosure, $this->escape);
$iterator->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY);

return $iterator;
}

/**
* Set the Inner Iterator
*
* @return StreamIterator|SplFileObject
*/
protected function setIterator()
{
if ($this->path instanceof StreamIterator || $this->path instanceof SplFileObject) {
return $this->path;
}

return new SplFileObject($this->getStreamFilterPath(), $this->open_mode);
}
}
Loading

0 comments on commit 37c2257

Please sign in to comment.