-
Notifications
You must be signed in to change notification settings - Fork 3
549 remove SpoonCSV #428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
549 remove SpoonCSV #428
Changes from all commits
63fb22f
fd284cb
1c9e8b8
e7e6e49
2209190
8af0026
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,15 +2,48 @@ | |
|
|
||
| namespace ForkCMS\Utility\Csv; | ||
|
|
||
| use Backend\Core\Engine\User; | ||
| use ForkCMS\Utility\PhpSpreadsheet\Reader\Filter\ChunkReadFilter; | ||
| use PhpOffice\PhpSpreadsheet\IOFactory; | ||
| use ForkCMS\Utility\PhpSpreadsheet\Reader\Filter\ColumnsFilter; | ||
| use PhpOffice\PhpSpreadsheet\Reader\Csv; | ||
| use PhpOffice\PhpSpreadsheet\Worksheet\Row; | ||
|
|
||
| class Reader | ||
| { | ||
| public function findColumnIndexes(string $path, array $columns): array | ||
| private function getUserOptions(User $user): array | ||
| { | ||
| $reader = IOFactory::createReader('Csv'); | ||
| $options['Delimiter'] = $user->getSetting('csv_split_character'); | ||
|
|
||
| $lineEnding = $user->getSetting('csv_line_ending'); | ||
| if ($lineEnding === '\n') { | ||
| $options['LineEnding'] = "\n"; | ||
| } | ||
| if ($lineEnding === '\r\n') { | ||
| $options['LineEnding'] = "\r\n"; | ||
| } | ||
|
|
||
| return $options; | ||
|
Comment on lines
+13
to
+25
|
||
| } | ||
tijsverkoyen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private function getReader(array $options): Csv | ||
| { | ||
| $reader = new Csv(); | ||
|
|
||
| if (!empty($options)) { | ||
| foreach ($options as $option => $value) { | ||
| $methodName = 'set' . $option; | ||
| if (method_exists($reader, $methodName)) { | ||
| $reader->$methodName($value); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return $reader; | ||
| } | ||
|
|
||
| public function findColumnIndexes(string $path, array $columns, User $user): array | ||
| { | ||
| $reader = $this->getReader($this->getUserOptions($user)); | ||
| $reader->setReadDataOnly(true); | ||
| $reader->setReadFilter(new ChunkReadFilter(1, 1)); | ||
| $spreadSheet = $reader->load($path); | ||
|
|
@@ -28,6 +61,30 @@ public function findColumnIndexes(string $path, array $columns): array | |
| return $indexes; | ||
| } | ||
|
|
||
| public function convertFileToArray(string $path, array $mapping, User $user): array | ||
| { | ||
| $data = []; | ||
|
|
||
| $reader = $this->getReader($this->getUserOptions($user)); | ||
| $reader->setReadDataOnly(true); | ||
| $reader->setReadFilter(new ColumnsFilter(array_keys($mapping))); | ||
| $spreadSheet = $reader->load($path); | ||
|
|
||
| foreach ($spreadSheet->getActiveSheet()->getRowIterator() as $row) { | ||
| // skip the first row as it contains the headers | ||
| if ($row->getRowIndex() === 1) { | ||
| continue; | ||
| } | ||
|
|
||
| $data[] = $this->convertRowIntoMappedArray( | ||
| $row, | ||
| $mapping | ||
| ); | ||
| } | ||
|
|
||
| return $data; | ||
| } | ||
|
|
||
| public function convertRowIntoMappedArray(Row $row, array $mapping): array | ||
| { | ||
| $data = array_fill_keys( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation comment has a grammatical error. "display to import" should be "display a form to import" for proper English grammar.