From 901a7bd5a23cc5b723392eb672c8b9ee75e01b6f Mon Sep 17 00:00:00 2001 From: Basil Suter Date: Tue, 10 Nov 2020 16:37:28 +0000 Subject: [PATCH 1/4] sort option --- core/CHANGELOG.md | 4 ++++ core/helpers/ExportHelper.php | 26 +++++++++++++++++++------- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index d3acc8d36..0e88a201b 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). In order to read more about upgrading and BC breaks have a look at the [UPGRADE Document](UPGRADE.md). +## 1.8.0 (10. Novemeber 2020) + ++ [#]() Added new `['sort' => false]` option to ExportHelper::csv() and ExportHelper::xls(). + ## 1.7.1 (23. September 2020) + [#2057](https://github.com/luyadev/luya/issues/2057) Fixed a issue with the root option of the IntersectionObserver. diff --git a/core/helpers/ExportHelper.php b/core/helpers/ExportHelper.php index b073878f5..40added03 100644 --- a/core/helpers/ExportHelper.php +++ b/core/helpers/ExportHelper.php @@ -20,13 +20,15 @@ class ExportHelper * @param array|QueryInterface $input The data to export into a csv * @param array $keys Defines which keys should be packed into the generated CSV. The defined keys does not change the sort behavior of the generated csv. * @param string $header Whether the column name should be set as header inside the csv or not. + * @param array $options Options + * + `sort`: boolean, whether they row should be sorted by its keys, default is true. * @return string The generated CSV as string. */ - public static function csv($input, array $keys = [], $header = true) + public static function csv($input, array $keys = [], $header = true, array $options = []) { $delimiter = ","; $input = self::transformInput($input); - $array = self::generateContentArray($input, $keys, $header); + $array = self::generateContentArray($input, $keys, $header, $options); return self::generateOutputString($array, $delimiter); } @@ -37,15 +39,17 @@ public static function csv($input, array $keys = [], $header = true) * @param array|QueryInterface $input * @param array $keys Defines which keys should be packed into the generated xlsx. The defined keys does not change the sort behavior of the generated xls. * @param bool $header + * @param array $options Options + * + `sort`: boolean, whether they row should be sorted by its keys, default is true. * @return mixed * @throws Exception * @since 1.0.4 */ - public static function xlsx($input, array $keys = [], $header = true) + public static function xlsx($input, array $keys = [], $header = true, array $options = []) { $input = self::transformInput($input); - $array = self::generateContentArray($input, $keys, $header); + $array = self::generateContentArray($input, $keys, $header, $options); $writer = new XLSXWriter(); $writer->writeSheet($array); @@ -76,11 +80,13 @@ protected static function transformInput($input) * @param string $delimiter * @param array $keys * @param bool $generateHeader + * @param array $options Options + * + `sort`: boolean, whether they row should be sorted by its keys, default is true. * @return array * @throws Exception * @since 1.0.4 */ - protected static function generateContentArray($contentRows, array $keys, $generateHeader = true) + protected static function generateContentArray($contentRows, array $keys, $generateHeader = true, $options = []) { if (is_scalar($contentRows)) { throw new Exception("Content must be either an array, object or traversable."); @@ -102,7 +108,11 @@ protected static function generateContentArray($contentRows, array $keys, $gener $attributeKeys[get_class($content)] = $keys; } $row = ArrayHelper::toArray($content, $attributeKeys, false); - ksort($row); + + if (ArrayHelper::getValue($options, 'sort', true)) { + ksort($row); + } + $rows[$i] = $row; // handle header @@ -120,7 +130,9 @@ protected static function generateContentArray($contentRows, array $keys, $gener $header = array_keys($rows[0]); } - ksort($header); + if (ArrayHelper::getValue($options, 'sort', true)) { + ksort($header); + } } unset($row); From 04c889d1f2f2a86529eebced35aeefa60c780f1b Mon Sep 17 00:00:00 2001 From: Basil Suter Date: Tue, 10 Nov 2020 16:57:03 +0000 Subject: [PATCH 2/4] add test --- tests/core/helpers/ExportHelperTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/core/helpers/ExportHelperTest.php b/tests/core/helpers/ExportHelperTest.php index 473b82dd7..c92fb61fc 100644 --- a/tests/core/helpers/ExportHelperTest.php +++ b/tests/core/helpers/ExportHelperTest.php @@ -141,4 +141,19 @@ public function testSpecialCharsEncoding() $this->assertSameTrimmed('"&","\'","a""b""c"', $content); } + + public function testSortingDisabled() + { + $content = ExportHelper::csv([ + ['c' => 'c', 'b' => 'b', 'a' => 'a'], + ], [], false); + + $this->assertSameTrimmed('"a","b","c"', $content); + + $content = ExportHelper::csv([ + ['c' => 'c', 'b' => 'b', 'a' => 'a'], + ], [], false, ['sort' => false]); + + $this->assertSameTrimmed('"c","b","a"', $content); + } } From 079ab1553877d1a118fbc4159073bed3c9e00eb0 Mon Sep 17 00:00:00 2001 From: Basil Suter Date: Tue, 10 Nov 2020 16:57:35 +0000 Subject: [PATCH 3/4] add since --- core/helpers/ExportHelper.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/helpers/ExportHelper.php b/core/helpers/ExportHelper.php index 40added03..65fdb9a16 100644 --- a/core/helpers/ExportHelper.php +++ b/core/helpers/ExportHelper.php @@ -20,7 +20,7 @@ class ExportHelper * @param array|QueryInterface $input The data to export into a csv * @param array $keys Defines which keys should be packed into the generated CSV. The defined keys does not change the sort behavior of the generated csv. * @param string $header Whether the column name should be set as header inside the csv or not. - * @param array $options Options + * @param array $options Options {@since 1.8.0} * + `sort`: boolean, whether they row should be sorted by its keys, default is true. * @return string The generated CSV as string. */ @@ -39,7 +39,7 @@ public static function csv($input, array $keys = [], $header = true, array $opti * @param array|QueryInterface $input * @param array $keys Defines which keys should be packed into the generated xlsx. The defined keys does not change the sort behavior of the generated xls. * @param bool $header - * @param array $options Options + * @param array $options Options {@since 1.8.0} * + `sort`: boolean, whether they row should be sorted by its keys, default is true. * @return mixed * @throws Exception @@ -80,7 +80,7 @@ protected static function transformInput($input) * @param string $delimiter * @param array $keys * @param bool $generateHeader - * @param array $options Options + * @param array $options Options {@since 1.8.0} * + `sort`: boolean, whether they row should be sorted by its keys, default is true. * @return array * @throws Exception From 8713645a676a756d99c5d9dc1f32662fc3b412c4 Mon Sep 17 00:00:00 2001 From: Basil Suter Date: Tue, 10 Nov 2020 16:57:49 +0000 Subject: [PATCH 4/4] ref --- core/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index 0e88a201b..ce60144e2 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -5,7 +5,7 @@ In order to read more about upgrading and BC breaks have a look at the [UPGRADE ## 1.8.0 (10. Novemeber 2020) -+ [#]() Added new `['sort' => false]` option to ExportHelper::csv() and ExportHelper::xls(). ++ [#2067](https://github.com/luyadev/luya/pull/2067) Added new `['sort' => false]` option to ExportHelper::csv() and ExportHelper::xls(). ## 1.7.1 (23. September 2020)