Skip to content

Commit

Permalink
Add delimiter to fgetcsv (#3)
Browse files Browse the repository at this point in the history
* Add delimiter to fgetcsv

* Use declared properties and sync content with examples

---------

Co-authored-by: Niklan <[email protected]>
  • Loading branch information
alekskn and Niklan authored Jul 9, 2024
1 parent f89344f commit 5ac65b1
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
skip_first_line: 1
delimiter: ';'
enclosure: ','
enclosure: '"'
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class CSVBatchImport {
/**
* {@inheritdoc}
*/
public function __construct($fid, $skip_first_line = FALSE, $delimiter = ';', $enclosure = ',', $batch_name = 'Custom CSV import') {
public function __construct($fid, $skip_first_line = FALSE, $delimiter = ';', $enclosure = '"', $batch_name = 'Custom CSV import') {
$this->fid = $fid;
$this->file = File::load($fid);
$this->skip_first_line = $skip_first_line;
Expand Down Expand Up @@ -65,9 +65,9 @@ public function parseCSV() {
# Если необходимо пропустить первую строку csv файла, то мы просто в
# холостую грузим и ничего не делаем с ней.
if ($this->skip_first_line) {
fgetcsv($handle, 0, ';');
fgetcsv($handle, 0, $this->delimiter, $this->enclosure);
}
while (($data = fgetcsv($handle, 0, ';')) !== FALSE) {
while (($data = fgetcsv($handle, 0, $this->delimiter, $this->enclosure)) !== FALSE) {
$this->setOperation($data);
}
fclose($handle);
Expand Down
6 changes: 3 additions & 3 deletions blog/2016/09/11/drupal-8-custom-csv-import/index.ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private $enclosure;
/**
* {@inheritdoc}
*/
public function __construct($fid, $skip_first_line = FALSE, $delimiter = ';', $enclosure = ',', $batch_name = 'Custom CSV import') {
public function __construct($fid, $skip_first_line = FALSE, $delimiter = ';', $enclosure = '"', $batch_name = 'Custom CSV import') {
$this->fid = $fid;
$this->file = File::load($fid);
$this->skip_first_line = $skip_first_line;
Expand Down Expand Up @@ -362,7 +362,7 @@ class CSVBatchImport {
/**
* {@inheritdoc}
*/
public function __construct($fid, $skip_first_line = FALSE, $delimiter = ';', $enclosure = ',', $batch_name = 'Custom CSV import') {
public function __construct($fid, $skip_first_line = FALSE, $delimiter = ';', $enclosure = '"', $batch_name = 'Custom CSV import') {
$this->fid = $fid;
$this->file = File::load($fid);
$this->skip_first_line = $skip_first_line;
Expand Down Expand Up @@ -874,7 +874,7 @@ class ImportForm extends ConfigFormBase {
```yaml {"header":"Листинг config/install/custom_csv_import.import.yml"}
skip_first_line: 1
delimiter: ';'
enclosure: ','
enclosure: '"'
```
## Добавление страницы для формы
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
skip_first_line: 1
delimiter: ';'
enclosure: ','
enclosure: '"'
chunk_size: 20
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class CSVBatchImport {
/**
* {@inheritdoc}
*/
public function __construct($plugin_id, $fid, $skip_first_line = FALSE, $delimiter = ';', $enclosure = ',', $chunk_size = 20, $batch_name = 'Custom CSV import') {
public function __construct($plugin_id, $fid, $skip_first_line = FALSE, $delimiter = ';', $enclosure = '"', $chunk_size = 20, $batch_name = 'Custom CSV import') {
$this->importPluginId = $plugin_id;
$this->importPlugin = \Drupal::service('plugin.manager.custom_csv_import')
->createInstance($plugin_id);
Expand All @@ -61,9 +61,9 @@ public function parseCSV() {
$items = [];
if (($handle = fopen($this->file->getFileUri(), 'r')) !== FALSE) {
if ($this->skip_first_line) {
fgetcsv($handle, 0, ';');
fgetcsv($handle, 0, $this->delimiter, $this->enclosure);
}
while (($data = fgetcsv($handle, 0, ';')) !== FALSE) {
while (($data = fgetcsv($handle, 0, $this->delimiter, $this->enclosure)) !== FALSE) {
$items[] = $data;
}
fclose($handle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function startImport(array &$form, FormStateInterface $form_state) {
```yaml {"header":"/config/install/custom_csv_import.import.yml"}
skip_first_line: 1
delimiter: ';'
enclosure: ','
enclosure: '"'
# new
chunk_size: 20
```
Expand All @@ -147,7 +147,7 @@ CSVBatchImport в котором происходит парсиснг файл
private $chunk_size;
# В конструкторе добавляем новый аргумент $chunk_size.
public function __construct($plugin_id, $fid, $skip_first_line = FALSE, $delimiter = ';', $enclosure = ',', $chunk_size = 20, $batch_name = 'Custom CSV import') {
public function __construct($plugin_id, $fid, $skip_first_line = FALSE, $delimiter = ';', $enclosure = '"', $chunk_size = 20, $batch_name = 'Custom CSV import') {
# …
# И пишем его в переменную.
$this->chunk_size = $chunk_size;
Expand All @@ -164,9 +164,9 @@ public function parseCSV() {
$items = [];
if (($handle = fopen($this->file->getFileUri(), 'r')) !== FALSE) {
if ($this->skip_first_line) {
fgetcsv($handle, 0, ';');
fgetcsv($handle, 0, $this->delimiter, $this->enclosure);
}
while (($data = fgetcsv($handle, 0, ';')) !== FALSE) {
while (($data = fgetcsv($handle, 0, $this->delimiter, $this->enclosure)) !== FALSE) {
# Данные мы теперь не устанавливаем на операцию,
# а сохраняем в массив для дальнейшего дробления.
$items[] = $data;
Expand Down

0 comments on commit 5ac65b1

Please sign in to comment.