1 | <?php |
||
29 | class Writer extends AbstractCsv |
||
30 | { |
||
31 | /** |
||
32 | * callable collection to format the record before insertion |
||
33 | * |
||
34 | * @var callable[] |
||
35 | */ |
||
36 | protected $formatters = []; |
||
37 | |||
38 | /** |
||
39 | * callable collection to validate the record before insertion |
||
40 | * |
||
41 | * @var callable[] |
||
42 | */ |
||
43 | protected $validators = []; |
||
44 | |||
45 | /** |
||
46 | * newline character |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $newline = "\n"; |
||
51 | |||
52 | /** |
||
53 | * Insert records count for flushing |
||
54 | * |
||
55 | * @var int |
||
56 | */ |
||
57 | protected $flush_counter = 0; |
||
58 | |||
59 | /** |
||
60 | * Buffer flush threshold |
||
61 | * |
||
62 | * @var int|null |
||
63 | */ |
||
64 | protected $flush_threshold; |
||
65 | |||
66 | /** |
||
67 | * @inheritdoc |
||
68 | */ |
||
69 | protected $stream_filter_mode = STREAM_FILTER_WRITE; |
||
70 | |||
71 | /** |
||
72 | * Returns the current newline sequence characters |
||
73 | * |
||
74 | * @return string |
||
75 | */ |
||
76 | 2 | public function getNewline(): string |
|
80 | |||
81 | /** |
||
82 | * Get the flush threshold |
||
83 | * |
||
84 | * @return int|null |
||
85 | */ |
||
86 | 2 | public function getFlushThreshold() |
|
90 | |||
91 | /** |
||
92 | * Adds multiple lines to the CSV document |
||
93 | * |
||
94 | * a simple wrapper method around insertOne |
||
95 | * |
||
96 | * @param Traversable|array $records a multidimensional array or a Traversable object |
||
97 | * |
||
98 | * @return int |
||
99 | */ |
||
100 | 6 | public function insertAll($records): int |
|
116 | |||
117 | /** |
||
118 | * Adds a single line to a CSV document |
||
119 | * |
||
120 | * @param string[] $record an array |
||
121 | * |
||
122 | * @throws InsertionException If the record can not be inserted |
||
123 | * |
||
124 | * @return int |
||
125 | */ |
||
126 | 16 | public function insertOne(array $record): int |
|
137 | |||
138 | /** |
||
139 | * Format the given record |
||
140 | * |
||
141 | * @param string[] $record |
||
142 | * @param callable $formatter |
||
143 | * |
||
144 | * @return string[] |
||
145 | */ |
||
146 | 2 | protected function formatRecord(array $record, callable $formatter): array |
|
150 | |||
151 | /** |
||
152 | * Validate a record |
||
153 | * |
||
154 | * @param string[] $record |
||
155 | * |
||
156 | * @throws InsertionException If the validation failed |
||
157 | */ |
||
158 | 8 | protected function validateRecord(array $record) |
|
166 | |||
167 | /** |
||
168 | * Apply post insertion actions |
||
169 | * |
||
170 | * @return int |
||
171 | */ |
||
172 | 8 | protected function consolidate(): int |
|
192 | |||
193 | /** |
||
194 | * add a formatter to the collection |
||
195 | * |
||
196 | * @param callable $formatter |
||
197 | * |
||
198 | * @return static |
||
199 | */ |
||
200 | 2 | public function addFormatter(callable $formatter): self |
|
206 | |||
207 | /** |
||
208 | * add a Validator to the collection |
||
209 | * |
||
210 | * @param callable $validator |
||
211 | * @param string $validator_name the validator name |
||
212 | * |
||
213 | * @return static |
||
214 | */ |
||
215 | 2 | public function addValidator(callable $validator, string $validator_name): self |
|
221 | |||
222 | /** |
||
223 | * Sets the newline sequence characters |
||
224 | * |
||
225 | * @param string $newline |
||
226 | * |
||
227 | * @return static |
||
228 | */ |
||
229 | 2 | public function setNewline(string $newline): self |
|
235 | |||
236 | /** |
||
237 | * Set the automatic flush threshold on write |
||
238 | * |
||
239 | * @param int|null $threshold |
||
240 | * |
||
241 | * @return static |
||
242 | */ |
||
243 | 6 | public function setFlushThreshold($threshold): self |
|
258 | } |
||
259 |