-
Notifications
You must be signed in to change notification settings - Fork 2
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
Add ImportHelper::csvFromResource() method to import CSV from a resou… #21
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,33 +13,17 @@ | |
class ImportHelper | ||
{ | ||
/** | ||
* Import a CSV from a string or filename and return array. | ||
* | ||
* The filename can be either a resource from `fopen()` or a string containing the CSV data. Filenames will be wrapped through {{`Yii::getAlias()`}} method. | ||
* Import a CSV from a resource and return array. | ||
* | ||
* @param string $filename Can be either a filename which is parsed by {{luya\yii\helpers\FileHelper::getFileContent()}} or a string with the contained CSV data. | ||
* @param array $options Provide options to the CSV | ||
* + `removeHeader`: boolean, Whether the import CSV contains a header in the first row to skip or not. Default value is false. | ||
* + `delimiter`: string, The delimiter which is used to explode the columns. Default value is `,`. | ||
* + `enclosure`: string, The enclosure which is used between the columns. Default value is `"`. | ||
* + `fields`: array, An array with filenames (based on the array header if any, or position) which should be parsed into the final export. | ||
* ```php | ||
* 'fields' => ['firstname', 'lastname'] // will only parse those fields based on table header (row 0) | ||
* 'fields' => [0,1,3] // will only parse fields by those positions if no table header is present. Positions starts at 0. | ||
* ``` | ||
* @return array an array with the CSV data. | ||
* @param resource $resource | ||
* @param array $options See {{luya\yii\helpers\ExportHelper::csv()}} for all options. | ||
* @return array | ||
* @since 1.6.0 | ||
*/ | ||
public static function csv($filename, array $options = []) | ||
public static function csvFromResource($resource, array $options = []): array | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Method |
||
{ | ||
$filename = Yii::getAlias($filename); | ||
|
||
// check if a given file name is provided or a CSV based on the content | ||
if (FileHelper::getFileInfo($filename)->extension) { | ||
$resource = fopen($filename, 'r'); | ||
} else { | ||
$resource = fopen('php://memory', 'rw'); | ||
fwrite($resource, $filename); | ||
rewind($resource); | ||
if (!is_resource($resource)) { | ||
throw new \InvalidArgumentException("The provided resource is not a valid resource."); | ||
} | ||
$data = []; | ||
while (($row = fgetcsv($resource, 0, ArrayHelper::getValue($options, 'delimiter', ','), ArrayHelper::getValue($options, 'enclosure', '"'))) !== false) { | ||
|
@@ -74,4 +58,37 @@ public static function csv($filename, array $options = []) | |
|
||
return $data; | ||
} | ||
|
||
/** | ||
* Import a CSV from a string or filename and return array. | ||
* | ||
* The filename can be either a resource from `fopen()` or a string containing the CSV data. Filenames will be wrapped through {{`Yii::getAlias()`}} method. | ||
* | ||
* @param string $filename Can be either a filename which is parsed by {{luya\yii\helpers\FileHelper::getFileContent()}} or a string with the contained CSV data. | ||
* @param array $options Provide options to the CSV | ||
* + `removeHeader`: boolean, Whether the import CSV contains a header in the first row to skip or not. Default value is false. | ||
* + `delimiter`: string, The delimiter which is used to explode the columns. Default value is `,`. | ||
* + `enclosure`: string, The enclosure which is used between the columns. Default value is `"`. | ||
* + `fields`: array, An array with filenames (based on the array header if any, or position) which should be parsed into the final export. | ||
* ```php | ||
* 'fields' => ['firstname', 'lastname'] // will only parse those fields based on table header (row 0) | ||
* 'fields' => [0,1,3] // will only parse fields by those positions if no table header is present. Positions starts at 0. | ||
* ``` | ||
* @return array an array with the CSV data. | ||
*/ | ||
public static function csv($filename, array $options = []) | ||
{ | ||
$filename = Yii::getAlias($filename); | ||
|
||
// check if a given file name is provided or a CSV based on the content | ||
if (FileHelper::getFileInfo($filename)->extension) { | ||
$resource = fopen($filename, 'r'); | ||
} else { | ||
$resource = fopen('php://memory', 'rw'); | ||
fwrite($resource, $filename); | ||
rewind($resource); | ||
} | ||
|
||
return self::csvFromResource($resource, $options); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Function
csvFromResource
has a Cognitive Complexity of 17 (exceeds 5 allowed). Consider refactoring.