1 | <?php |
||
27 | class Writer extends AbstractCsv |
||
28 | { |
||
29 | /** |
||
30 | * callable collection to format the record before insertion |
||
31 | * |
||
32 | * @var callable[] |
||
33 | */ |
||
34 | protected $formatters = []; |
||
35 | |||
36 | /** |
||
37 | * callable collection to validate the record before insertion |
||
38 | * |
||
39 | * @var callable[] |
||
40 | */ |
||
41 | protected $validators = []; |
||
42 | |||
43 | /** |
||
44 | * newline character |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $newline = "\n"; |
||
49 | |||
50 | /** |
||
51 | * Insert records count for flushing |
||
52 | * |
||
53 | * @var int |
||
54 | */ |
||
55 | protected $flush_counter = 0; |
||
56 | |||
57 | /** |
||
58 | * Buffer flush threshold |
||
59 | * |
||
60 | * @var int|null |
||
61 | */ |
||
62 | protected $flush_threshold; |
||
63 | |||
64 | /** |
||
65 | * {@inheritdoc} |
||
66 | */ |
||
67 | protected $stream_filter_mode = STREAM_FILTER_WRITE; |
||
68 | |||
69 | /** |
||
70 | * Returns the current newline sequence characters |
||
71 | * |
||
72 | * @return string |
||
73 | */ |
||
74 | 2 | public function getNewline(): string |
|
78 | |||
79 | /** |
||
80 | * Get the flush threshold |
||
81 | * |
||
82 | * @return int|null |
||
83 | */ |
||
84 | 2 | public function getFlushThreshold() |
|
88 | |||
89 | /** |
||
90 | * Adds multiple records to the CSV document |
||
91 | * |
||
92 | * @see Writer::insertOne |
||
93 | * |
||
94 | * @param Traversable|array $records a multidimensional array or a Traversable object |
||
95 | * |
||
96 | * @return int |
||
97 | */ |
||
98 | 6 | public function insertAll($records): int |
|
114 | |||
115 | /** |
||
116 | * Adds a single record to a CSV document |
||
117 | * |
||
118 | * @param array $record An array containing |
||
119 | * - scalar types values, |
||
120 | * - NULL values, |
||
121 | * - or objects implementing the __toString() method. |
||
122 | * |
||
123 | * @throws CannotInsertRecord If the record can not be inserted |
||
124 | * |
||
125 | * @return int |
||
126 | */ |
||
127 | 16 | public function insertOne(array $record): int |
|
138 | |||
139 | /** |
||
140 | * Format a record |
||
141 | * |
||
142 | * The returned array must contain |
||
143 | * - scalar types values, |
||
144 | * - NULL values, |
||
145 | * - or objects implementing the __toString() method. |
||
146 | * |
||
147 | * @param array $record An array containing |
||
148 | * - scalar types values, |
||
149 | * - NULL values, |
||
150 | * - implementing the __toString() method. |
||
151 | * |
||
152 | * @param callable $formatter |
||
153 | * |
||
154 | * @return array |
||
155 | */ |
||
156 | 2 | protected function formatRecord(array $record, callable $formatter): array |
|
160 | |||
161 | /** |
||
162 | * Validate a record |
||
163 | * |
||
164 | * @param array $record An array containing |
||
165 | * - scalar types values, |
||
166 | * - NULL values |
||
167 | * - or objects implementing __toString() method. |
||
168 | * |
||
169 | * @throws CannotInsertRecord If the validation failed |
||
170 | */ |
||
171 | 8 | protected function validateRecord(array $record) |
|
179 | |||
180 | /** |
||
181 | * Apply post insertion actions |
||
182 | * |
||
183 | * @return int |
||
184 | */ |
||
185 | 8 | protected function consolidate(): int |
|
205 | |||
206 | /** |
||
207 | * Adds a record formatter |
||
208 | * |
||
209 | * @param callable $formatter |
||
210 | * |
||
211 | * @return static |
||
212 | */ |
||
213 | 2 | public function addFormatter(callable $formatter): self |
|
219 | |||
220 | /** |
||
221 | * Adds a record validator |
||
222 | * |
||
223 | * @param callable $validator |
||
224 | * @param string $validator_name the validator name |
||
225 | * |
||
226 | * @return static |
||
227 | */ |
||
228 | 2 | public function addValidator(callable $validator, string $validator_name): self |
|
234 | |||
235 | /** |
||
236 | * Sets the newline sequence |
||
237 | * |
||
238 | * @param string $newline |
||
239 | * |
||
240 | * @return static |
||
241 | */ |
||
242 | 2 | public function setNewline(string $newline): self |
|
248 | |||
249 | /** |
||
250 | * Set the flush threshold |
||
251 | * |
||
252 | * @param int|null $threshold |
||
253 | * |
||
254 | * @throws Exception if the threshold is a integer lesser than 1 |
||
255 | * |
||
256 | * @return static |
||
257 | */ |
||
258 | 6 | public function setFlushThreshold($threshold): self |
|
278 | } |
||
279 |