1 | <?php |
||
39 | class Writer extends AbstractCsv |
||
40 | { |
||
41 | const MODE_PHP = 'MODE_PHP'; |
||
42 | |||
43 | const MODE_RFC4180 = 'MODE_RFC4180'; |
||
44 | |||
45 | /** |
||
46 | * callable collection to format the record before insertion. |
||
47 | * |
||
48 | * @var callable[] |
||
49 | */ |
||
50 | protected $formatters = []; |
||
51 | |||
52 | /** |
||
53 | * callable collection to validate the record before insertion. |
||
54 | * |
||
55 | * @var callable[] |
||
56 | */ |
||
57 | protected $validators = []; |
||
58 | |||
59 | /** |
||
60 | * newline character. |
||
61 | * |
||
62 | * @var string |
||
63 | */ |
||
64 | protected $newline = "\n"; |
||
65 | |||
66 | /** |
||
67 | * Insert records count for flushing. |
||
68 | * |
||
69 | * @var int |
||
70 | */ |
||
71 | protected $flush_counter = 0; |
||
72 | |||
73 | /** |
||
74 | * Buffer flush threshold. |
||
75 | * |
||
76 | * @var int|null |
||
77 | */ |
||
78 | protected $flush_threshold; |
||
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | protected $stream_filter_mode = STREAM_FILTER_WRITE; |
||
84 | |||
85 | /** |
||
86 | * Regular expression used to detect if enclosure are necessary or not. |
||
87 | * |
||
88 | * @var string |
||
89 | */ |
||
90 | protected $rfc4180_regexp; |
||
91 | |||
92 | /** |
||
93 | * Returns the current newline sequence characters. |
||
94 | */ |
||
95 | 3 | public function getNewline(): string |
|
99 | |||
100 | /** |
||
101 | * Get the flush threshold. |
||
102 | * |
||
103 | * @return int|null |
||
104 | */ |
||
105 | 3 | public function getFlushThreshold() |
|
109 | |||
110 | /** |
||
111 | * Adds multiple records to the CSV document. |
||
112 | * |
||
113 | * @see Writer::insertOne |
||
114 | * |
||
115 | * @param Traversable|array $records |
||
116 | */ |
||
117 | 9 | public function insertAll($records, string $mode = self::MODE_PHP): int |
|
133 | |||
134 | /** |
||
135 | * Adds a single record to a CSV document. |
||
136 | * |
||
137 | * A record is an array that can contains scalar types values, NULL values |
||
138 | * or objects implementing the __toString method. |
||
139 | * |
||
140 | * @throws CannotInsertRecord If the record can not be inserted |
||
141 | */ |
||
142 | 48 | public function insertOne(array $record, string $mode = self::MODE_PHP): int |
|
158 | |||
159 | /** |
||
160 | * Adds a single record to a CSV Document using PHP algorithm. |
||
161 | */ |
||
162 | 9 | protected function fputcsvPHP(array $record) |
|
166 | |||
167 | /** |
||
168 | * Adds a single record to a CSV Document using RFC4180 algorithm. |
||
169 | */ |
||
170 | 18 | protected function fputcsvRFC4180(array $record) |
|
174 | |||
175 | /** |
||
176 | * Converts and Format a record field to be inserted into a CSV Document. |
||
177 | * |
||
178 | * @see https://tools.ietf.org/html/rfc4180 |
||
179 | */ |
||
180 | 18 | protected function convertField($field): string |
|
192 | |||
193 | /** |
||
194 | * Format a record. |
||
195 | * |
||
196 | * The returned array must contain |
||
197 | * - scalar types values, |
||
198 | * - NULL values, |
||
199 | * - or objects implementing the __toString() method. |
||
200 | */ |
||
201 | 3 | protected function formatRecord(array $record, callable $formatter): array |
|
205 | |||
206 | /** |
||
207 | * Validate a record. |
||
208 | * |
||
209 | * @throws CannotInsertRecord If the validation failed |
||
210 | */ |
||
211 | 12 | protected function validateRecord(array $record) |
|
219 | |||
220 | /** |
||
221 | * Apply post insertion actions. |
||
222 | */ |
||
223 | 12 | protected function consolidate(): int |
|
243 | |||
244 | /** |
||
245 | * Adds a record formatter. |
||
246 | */ |
||
247 | 3 | public function addFormatter(callable $formatter): self |
|
253 | |||
254 | /** |
||
255 | * Adds a record validator. |
||
256 | */ |
||
257 | 3 | public function addValidator(callable $validator, string $validator_name): self |
|
263 | |||
264 | /** |
||
265 | * Sets the newline sequence. |
||
266 | */ |
||
267 | 3 | public function setNewline(string $newline): self |
|
273 | |||
274 | /** |
||
275 | * Reset dynamic object properties to improve performance. |
||
276 | */ |
||
277 | 15 | protected function resetProperties() |
|
285 | |||
286 | /** |
||
287 | * Set the flush threshold. |
||
288 | * |
||
289 | * @param int|null $threshold |
||
290 | * |
||
291 | * @throws Exception if the threshold is a integer lesser than 1 |
||
292 | */ |
||
293 | 12 | public function setFlushThreshold($threshold): self |
|
313 | } |
||
314 |