1 | <?php |
||
30 | class Writer extends AbstractCsv |
||
31 | { |
||
32 | /** |
||
33 | * callable collection to format the record before insertion |
||
34 | * |
||
35 | * @var callable[] |
||
36 | */ |
||
37 | protected $formatters = []; |
||
38 | |||
39 | /** |
||
40 | * callable collection to validate the record before insertion |
||
41 | * |
||
42 | * @var callable[] |
||
43 | */ |
||
44 | protected $validators = []; |
||
45 | |||
46 | /** |
||
47 | * newline character |
||
48 | * |
||
49 | * @var string |
||
50 | */ |
||
51 | protected $newline = "\n"; |
||
52 | |||
53 | /** |
||
54 | * Insert records count for flushing |
||
55 | * |
||
56 | * @var int |
||
57 | */ |
||
58 | protected $flush_counter = 0; |
||
59 | |||
60 | /** |
||
61 | * Buffer flush threshold |
||
62 | * |
||
63 | * @var int|null |
||
64 | */ |
||
65 | protected $flush_threshold; |
||
66 | |||
67 | /** |
||
68 | * @inheritdoc |
||
69 | */ |
||
70 | protected $stream_filter_mode = STREAM_FILTER_WRITE; |
||
71 | |||
72 | /** |
||
73 | * Returns the current newline sequence characters |
||
74 | * |
||
75 | * @return string |
||
76 | */ |
||
77 | 2 | public function getNewline(): string |
|
78 | { |
||
79 | 2 | return $this->newline; |
|
80 | } |
||
81 | |||
82 | /** |
||
83 | * Get the flush threshold |
||
84 | * |
||
85 | * @return int|null |
||
86 | */ |
||
87 | 2 | public function getFlushThreshold() |
|
88 | { |
||
89 | 2 | return $this->flush_threshold; |
|
90 | } |
||
91 | |||
92 | /** |
||
93 | * Adds multiple lines to the CSV document |
||
94 | * |
||
95 | * a simple wrapper method around insertOne |
||
96 | * |
||
97 | * @param Traversable|array $records a multidimensional array or a Traversable object |
||
98 | * |
||
99 | * @return int |
||
100 | */ |
||
101 | 6 | public function insertAll($records): int |
|
102 | { |
||
103 | 6 | if (!is_iterable($records)) { |
|
104 | 2 | throw new TypeError(sprintf('%s() expects argument passed to be iterable, %s given', __METHOD__, gettype($records))); |
|
105 | } |
||
106 | |||
107 | 4 | $bytes = 0; |
|
108 | 4 | foreach ($records as $record) { |
|
109 | 4 | $bytes += $this->insertOne($record); |
|
110 | } |
||
111 | |||
112 | 4 | $this->flush_counter = 0; |
|
113 | 4 | $this->document->fflush(); |
|
114 | |||
115 | 4 | return $bytes; |
|
116 | } |
||
117 | |||
118 | /** |
||
119 | * Adds a single line to a CSV document |
||
120 | * |
||
121 | * @param string[] $record an array |
||
122 | * |
||
123 | * @throws InsertionException If the record can not be inserted |
||
124 | * |
||
125 | * @return int |
||
126 | */ |
||
127 | 16 | public function insertOne(array $record): int |
|
128 | { |
||
129 | 16 | $record = array_reduce($this->formatters, [$this, 'formatRecord'], $record); |
|
130 | 16 | $this->validateRecord($record); |
|
131 | 14 | $bytes = $this->document->fputcsv($record, ...$this->document->getCsvControl()); |
|
132 | 14 | if (!$bytes) { |
|
133 | 2 | throw InsertionException::createFromStream($record); |
|
134 | } |
||
135 | |||
136 | 12 | return $bytes + $this->consolidate(); |
|
137 | } |
||
138 | |||
139 | /** |
||
140 | * Format the given record |
||
141 | * |
||
142 | * @param string[] $record |
||
143 | * @param callable $formatter |
||
144 | * |
||
145 | * @return string[] |
||
146 | */ |
||
147 | 2 | protected function formatRecord(array $record, callable $formatter): array |
|
151 | |||
152 | /** |
||
153 | * Validate a record |
||
154 | * |
||
155 | * @param string[] $record |
||
156 | * |
||
157 | * @throws InsertionException If the validation failed |
||
158 | */ |
||
159 | 8 | protected function validateRecord(array $record) |
|
167 | |||
168 | /** |
||
169 | * Apply post insertion actions |
||
170 | * |
||
171 | * @return int |
||
172 | */ |
||
173 | 8 | protected function consolidate(): int |
|
193 | |||
194 | /** |
||
195 | * add a formatter to the collection |
||
196 | * |
||
197 | * @param callable $formatter |
||
198 | * |
||
199 | * @return static |
||
200 | */ |
||
201 | 2 | public function addFormatter(callable $formatter): self |
|
207 | |||
208 | /** |
||
209 | * add a Validator to the collection |
||
210 | * |
||
211 | * @param callable $validator |
||
212 | * @param string $validator_name the validator name |
||
213 | * |
||
214 | * @return static |
||
215 | */ |
||
216 | 2 | public function addValidator(callable $validator, string $validator_name): self |
|
222 | |||
223 | /** |
||
224 | * Sets the newline sequence characters |
||
225 | * |
||
226 | * @param string $newline |
||
227 | * |
||
228 | * @return static |
||
229 | */ |
||
230 | 2 | public function setNewline(string $newline): self |
|
236 | |||
237 | /** |
||
238 | * Set the automatic flush threshold on write |
||
239 | * |
||
240 | * @param int|null $threshold |
||
241 | * |
||
242 | * @throws OutOfRangeException if the $threshold is not valid |
||
243 | * |
||
244 | * @return static |
||
245 | */ |
||
246 | 4 | public function setFlushThreshold($threshold): self |
|
264 | } |
||
265 |