-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from bburnichon/ticket_86_stream_writer
Add An Abstract StreamWriter
- Loading branch information
Showing
8 changed files
with
554 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
|
||
namespace Ddeboer\DataImport\Writer; | ||
|
||
/** | ||
* Base class to write into streams | ||
* | ||
* @author Benoît Burnichon <bburnichon@gmail.com> | ||
*/ | ||
abstract class AbstractStreamWriter implements WriterInterface | ||
{ | ||
private $stream; | ||
private $closeStreamOnFinish = true; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param resource $stream | ||
*/ | ||
public function __construct($stream = null) | ||
{ | ||
if (null !== $stream) { | ||
$this->setStream($stream); | ||
} | ||
} | ||
|
||
/** | ||
* Set Stream Resource | ||
* | ||
* @param $stream | ||
* @throws \InvalidArgumentException | ||
* @return $this | ||
*/ | ||
public function setStream($stream) | ||
{ | ||
if (! is_resource($stream) || ! 'stream' == get_resource_type($stream)) { | ||
throw new \InvalidArgumentException(sprintf( | ||
'Expects argument to be a stream resource, got %s', | ||
is_resource($stream) ? get_resource_type($stream) : gettype($stream) | ||
)); | ||
} | ||
|
||
$this->stream = $stream; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Get underlying stream resource | ||
* | ||
* @return resource | ||
*/ | ||
public function getStream() | ||
{ | ||
if (null === $this->stream) { | ||
$this->setStream(fopen('php://temp', 'rb+')); | ||
$this->setCloseStreamOnFinish(false); | ||
} | ||
|
||
return $this->stream; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function prepare() | ||
{ | ||
return $this; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function finish() | ||
{ | ||
if (is_resource($this->stream) && $this->getCloseStreamOnFinish()) { | ||
fclose($this->stream); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Should underlying stream be closed on finish? | ||
* | ||
* @param bool $closeStreamOnFinish | ||
* | ||
* @return bool | ||
*/ | ||
public function setCloseStreamOnFinish($closeStreamOnFinish = true) | ||
{ | ||
$this->closeStreamOnFinish = (bool) $closeStreamOnFinish; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Is Stream closed on finish? | ||
* | ||
* @return bool | ||
*/ | ||
public function getCloseStreamOnFinish() | ||
{ | ||
return $this->closeStreamOnFinish; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<?php | ||
|
||
namespace Ddeboer\DataImport\Writer; | ||
|
||
/** | ||
* Class allowing multiple writers to write in same stream | ||
* | ||
* @author Benoît Burnichon <bburnichon@gmail.com> | ||
*/ | ||
class StreamMergeWriter extends AbstractStreamWriter | ||
{ | ||
/** @var string */ | ||
private $discriminantField = 'discr'; | ||
/** @var AbstractStreamWriter[] */ | ||
private $writers = array(); | ||
|
||
/** | ||
* Set Discriminant field | ||
* | ||
* @param string $discriminantField | ||
* @return $this | ||
*/ | ||
public function setDiscriminantField($discriminantField) | ||
{ | ||
$this->discriminantField = (string) $discriminantField; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Get Discriminant Field | ||
* | ||
* @return string | ||
*/ | ||
public function getDiscriminantField() | ||
{ | ||
return $this->discriminantField; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function writeItem(array $item) | ||
{ | ||
if ((isset($item[$this->discriminantField]) | ||
|| array_key_exists($this->discriminantField, $item)) | ||
&& $this->hasStreamWriter($key = $item[$this->discriminantField]) | ||
) { | ||
$writer = $this->getStreamWriter($key); | ||
|
||
$writer->writeItem($item); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Set Stream writers | ||
* @param AbstractStreamWriter[] $writers | ||
* | ||
* @return $this | ||
*/ | ||
public function setStreamWriters(array $writers) | ||
{ | ||
foreach ($writers as $key => $writer) { | ||
$this->setStreamWriter($key, $writer); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param string $key | ||
* @param AbstractStreamWriter $writer | ||
* @return $this | ||
*/ | ||
public function setStreamWriter($key, AbstractStreamWriter $writer) | ||
{ | ||
$writer->setStream($this->getStream()); | ||
$writer->setCloseStreamOnFinish(false); | ||
$this->writers[$key] = $writer; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Get a previously registered Writer | ||
* | ||
* @param string $key | ||
* @return AbstractStreamWriter | ||
*/ | ||
public function getStreamWriter($key) | ||
{ | ||
return $this->writers[$key]; | ||
} | ||
|
||
/** | ||
* Get list of registered Writers | ||
* | ||
* @return AbstractStreamWriter[] | ||
*/ | ||
public function getStreamWriters() | ||
{ | ||
return $this->writers; | ||
} | ||
|
||
/** | ||
* Is a writer registered for key? | ||
* | ||
* @param string $key | ||
* @return bool | ||
*/ | ||
public function hasStreamWriter($key) | ||
{ | ||
return isset($this->writers[$key]); | ||
} | ||
|
||
public function setStream($stream) | ||
{ | ||
parent::setStream($stream); | ||
foreach ($this->getStreamWriters() as $writer) { | ||
$writer->setStream($stream); | ||
} | ||
|
||
return $this; | ||
} | ||
} |
Oops, something went wrong.