From 406e1e415faa109ccc4463806847bf045ba6b331 Mon Sep 17 00:00:00 2001 From: William Knauss Date: Mon, 19 Mar 2018 12:24:40 -0400 Subject: [PATCH 1/2] added getCollection method that returns a Illuminate\Support\Collection object - useful for using the mapping functions --- composer.json | 3 +++ src/Csv.php | 43 +++++++++++++++++++++++++++++++++---------- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/composer.json b/composer.json index 2af8b63..d11de87 100644 --- a/composer.json +++ b/composer.json @@ -26,5 +26,8 @@ }, "require-dev": { "phpunit/phpunit": "4.1.*" + }, + "suggest": { + "illuminate/support": "Fluent array interface for map functions" } } diff --git a/src/Csv.php b/src/Csv.php index fdb34ab..3d59b00 100644 --- a/src/Csv.php +++ b/src/Csv.php @@ -2,6 +2,7 @@ namespace ParseCsv; +use Illuminate\Support\Collection; use ParseCsv\enums\FileProcessingModeEnum; use ParseCsv\enums\SortEnum; use ParseCsv\extensions\DatatypeTrait; @@ -17,7 +18,6 @@ class Csv { Based on the concept of Ming Hong Ng's CsvFileParser class: - http://minghong.blogspot.com/2006/07/csv-parser-for-php.html - (The MIT license) Copyright (c) 2014 Jim Myhrberg. @@ -372,7 +372,6 @@ class Csv { $this->init($offset, $limit, $conditions); - if (strlen($input) <= PHP_MAXPATHLEN && is_readable($input)) { $this->file = $input; $this->data = $this->parse_file(); @@ -560,7 +559,6 @@ class Csv { - substr_count($data, "\r\n") - $headingRow; - return $count; } @@ -813,7 +811,7 @@ class Csv { // create data foreach ($data as $key => $row) { - foreach (array_keys($fieldOrder) as $index){ + foreach (array_keys($fieldOrder) as $index) { $cell_value = $row[$index]; $entry[] = $this->_enclose_value($cell_value, $delimiter); } @@ -829,19 +827,19 @@ class Csv { return $string; } - private function _validate_fields_for_unparse($fields){ + private function _validate_fields_for_unparse($fields) { // this is needed because sometime titles property is overwritten instead of using fields parameter! $titlesOnParse = !empty($this->data) ? array_keys($this->data[0]) : array(); - if (empty($fields)){ + if (empty($fields)) { $fields = $this->titles; } - if (empty($fields)){ + if (empty($fields)) { return array(); } // both are identical, also in ordering - if (array_values($fields) === array_values($titlesOnParse)){ + if (array_values($fields) === array_values($titlesOnParse)) { return array_combine($fields, $fields); } @@ -1196,7 +1194,7 @@ class Csv { * first line containing only "sep=;", where the last character is the * separator. Microsoft Excel is able to open such files. * - * @param string $data    file data + * @param string $data file data * * @return string|false detected delimiter, or false if none found */ @@ -1214,7 +1212,7 @@ class Csv { /** * Support for Excel-compatible sep=? row. * - * @param string $data_string    file data to be updated + * @param string $data_string file data to be updated * * @return bool TRUE if sep= line was found at the very beginning of the file */ @@ -1310,4 +1308,29 @@ class Csv { ksort($filtered); $this->delimiter = reset($filtered); } + + /** + * getCollection + * Returns a Illuminate/Collection object + * This may prove to be helpful to people who want to + * create macros, and or use map functions + * + * @access public + * @link https://laravel.com/docs/5.6/collections + * + * @throws \ErrorException - If the Illuminate\Support\Collection class is not found + * + * @return Collection + */ + public function getCollection() { + //does the Illuminate\Support\Collection class exists? + //this uses the autoloader to try to determine + //@see http://php.net/manual/en/function.class-exists.php + if (class_exists('Illuminate\Support\Collection', true) == false) { + throw new \ErrorException('It would appear you have not installed the illuminate/support package!'); + } + + //return the collection + return new Collection($this->data); + } } From 60d64580809b763bc6ca3155c0a6a756b26d27a1 Mon Sep 17 00:00:00 2001 From: Fonata Date: Mon, 2 Apr 2018 14:38:03 +0200 Subject: [PATCH 2/2] PHPUnit: prevent output from download.php to leak --- tests/methods/ConstructTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/methods/ConstructTest.php b/tests/methods/ConstructTest.php index c9d9db4..35ddd33 100644 --- a/tests/methods/ConstructTest.php +++ b/tests/methods/ConstructTest.php @@ -58,8 +58,9 @@ class ConstructTest extends TestCase { ob_start(); /** @noinspection PhpIncludeInspection */ require $script_file; + $ob_get_clean = ob_get_clean(); if ($script_file != 'download.php') { - $this->assertContains('', ob_get_clean()); + $this->assertContains('', $ob_get_clean); } } chdir('..');