From 9295156aab15e340b1f6e17923378d96842ef88b Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 6 Feb 2021 19:58:29 +0100 Subject: [PATCH] Documented support to import from a CSV file --- README.md | 11 +++++++++-- src/Sources/Csv/CsvImporter.php | 7 ++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 61f7db8..0569e75 100644 --- a/README.md +++ b/README.md @@ -16,8 +16,15 @@ This module can be installed using composer: ## Supported import sources -* Bit.ly -* Standard CSV +#### Bit.ly + +It imports using the API v4. The only required param is an [access token](https://bitly.is/accesstoken). + +#### Standard CSV + +It parses a CSV file with the `Long URL` and `Short code` columns. It can optionally contain `Domain`, `Title` and `Tags`, being the latter a pipe-separated list of items (`foo|bar|baz`). + +Column names can have spaces and have any combination of upper and lowercase. ## Usage diff --git a/src/Sources/Csv/CsvImporter.php b/src/Sources/Csv/CsvImporter.php index dda6cc9..53c29e0 100644 --- a/src/Sources/Csv/CsvImporter.php +++ b/src/Sources/Csv/CsvImporter.php @@ -40,7 +40,7 @@ public function import(array $rawParams): iterable yield new ImportedShlinkUrl( ImportSources::CSV, $record['longurl'], - array_filter(explode(self::TAG_SEPARATOR, $this->nonEmptyValueOrNull($record, 'tags') ?? '')), + $this->parseTags($record), $now, $this->nonEmptyValueOrNull($record, 'domain'), $record['shortcode'], @@ -73,4 +73,9 @@ private function nonEmptyValueOrNull(array $record, string $key): ?string return $trimmedValue; } + + private function parseTags(array $record): array + { + return array_filter(explode(self::TAG_SEPARATOR, $this->nonEmptyValueOrNull($record, 'tags') ?? '')); + } }