Skip to content
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

sort option #2067

Merged
merged 4 commits into from
Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
26 changes: 19 additions & 7 deletions core/helpers/ExportHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down Expand Up @@ -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.");
Expand All @@ -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
Expand All @@ -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);
Expand Down
15 changes: 15 additions & 0 deletions tests/core/helpers/ExportHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}