1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of graze/data-file |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @license https://github.com/graze/data-file/blob/master/LICENSE.md |
11
|
|
|
* @link https://github.com/graze/data-file |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Graze\DataFile\IO; |
15
|
|
|
|
16
|
|
|
use Graze\DataFile\Format\Formatter\FormatterInterface; |
17
|
|
|
use Graze\DataFile\Helper\OptionalLoggerTrait; |
18
|
|
|
use InvalidArgumentException; |
19
|
|
|
use Psr\Http\Message\StreamInterface; |
20
|
|
|
use Psr\Log\LoggerAwareInterface; |
21
|
|
|
use Psr\Log\LogLevel; |
22
|
|
|
use Traversable; |
23
|
|
|
|
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) |
39
|
|
|
{ |
40
|
10 |
|
$this->stream = $stream; |
41
|
10 |
|
$this->formatter = $formatter; |
42
|
10 |
|
} |
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) |
56
|
|
|
{ |
57
|
4 |
|
$this->writeBlock($rows); |
58
|
|
|
|
59
|
4 |
|
$this->stream->close(); |
60
|
|
|
|
61
|
4 |
|
return $this; |
62
|
|
|
} |
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
|
|
|
} |
78
|
7 |
|
$this->stream->write($this->formatter->format($row)); |
79
|
7 |
|
$first = false; |
80
|
|
|
} |
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
|
4 |
|
} 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
|
2 |
|
} elseif (strlen($this->formatter->getRowSeparator()) > 0) { |
103
|
2 |
|
$this->stream->write($this->formatter->getRowSeparator()); |
104
|
|
|
} |
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) |
115
|
|
|
{ |
116
|
4 |
|
$this->writeBlock([$row]); |
117
|
4 |
|
return $this; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|