diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index d3acc8d36..ce60144e2 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) + ++ [#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) + [#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..65fdb9a16 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 {@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. */ - 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 {@since 1.8.0} + * + `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 {@since 1.8.0} + * + `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); 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); + } }