1 | <?php |
||
24 | class StreamWriter implements WriterInterface, LoggerAwareInterface |
||
25 | { |
||
26 | use OptionalLoggerTrait; |
||
27 | |||
28 | /** @var FormatterInterface */ |
||
29 | private $formatter; |
||
30 | /** @var StreamInterface */ |
||
31 | private $stream; |
||
32 | |||
33 | /** |
||
34 | * @param StreamInterface $stream |
||
35 | * @param FormatterInterface $formatter Optional formatter, if not specified shall be determined based on the |
||
36 | * format of the file |
||
37 | */ |
||
38 | 10 | public function __construct(StreamInterface $stream, FormatterInterface $formatter) |
|
43 | |||
44 | /** |
||
45 | * Adds multiple lines to the CSV document |
||
46 | * |
||
47 | * a simple wrapper method around insertOne |
||
48 | * |
||
49 | * @param Traversable|array $rows a multidimensional array or a Traversable object |
||
50 | * |
||
51 | * @throws InvalidArgumentException If the given rows format is invalid |
||
52 | * |
||
53 | * @return static |
||
54 | */ |
||
55 | 4 | public function insertAll($rows) |
|
63 | |||
64 | /** |
||
65 | * @param Traversable|array $rows |
||
66 | */ |
||
67 | 7 | private function writeBlock($rows) |
|
68 | { |
||
69 | 7 | $this->log(LogLevel::INFO, 'Writing {count} rows to file', ['count' => count($rows)]); |
|
70 | |||
71 | 7 | $this->initialiseForWriting(); |
|
72 | |||
73 | 7 | $first = true; |
|
74 | 7 | foreach ($rows as $row) { |
|
75 | 7 | if (!$first) { |
|
76 | 4 | $this->stream->write($this->formatter->getRowSeparator()); |
|
77 | 4 | } |
|
78 | 7 | $this->stream->write($this->formatter->format($row)); |
|
79 | 7 | $first = false; |
|
80 | 7 | } |
|
81 | 7 | $this->stream->write($this->formatter->getClosingBlock()); |
|
82 | 7 | } |
|
83 | |||
84 | /** |
||
85 | * Initialise the resource for writing. |
||
86 | * |
||
87 | * If we are at 0, then write initial block, otherwise, remove closing block and add a row separator |
||
88 | * |
||
89 | * This is so we can append a file with special characters at the beginning and end |
||
90 | */ |
||
91 | 7 | private function initialiseForWriting() |
|
92 | { |
||
93 | // move to the end of the file to always append |
||
94 | 7 | $this->stream->seek(0, SEEK_END); |
|
95 | |||
96 | 7 | if ($this->stream->tell() === 0) { |
|
97 | 7 | $this->stream->write($this->formatter->getInitialBlock()); |
|
98 | 7 | } elseif (strlen($this->formatter->getClosingBlock()) > 0) { |
|
99 | 2 | $endBlock = $this->formatter->getClosingBlock(); |
|
100 | 2 | $this->stream->seek(strlen($endBlock) * -1, SEEK_CUR); |
|
101 | 2 | $this->stream->write($this->formatter->getRowSeparator()); |
|
102 | 4 | } elseif (strlen($this->formatter->getRowSeparator()) > 0) { |
|
103 | 2 | $this->stream->write($this->formatter->getRowSeparator()); |
|
104 | 2 | } |
|
105 | 7 | } |
|
106 | |||
107 | /** |
||
108 | * Adds a single line |
||
109 | * |
||
110 | * @param mixed $row an item to insert |
||
111 | * |
||
112 | * @return static |
||
113 | */ |
||
114 | 4 | public function insertOne($row) |
|
119 | } |
||
120 |