added enum for sort

This commit is contained in:
Susann Sgorzaly
2018-02-22 20:41:03 +01:00
parent ef44ea3989
commit 657cec4b4e
2 changed files with 21 additions and 9 deletions

View File

@@ -1,6 +1,7 @@
<?php
namespace ParseCsv;
use ParseCsv\enums\SortEnum;
use ParseCsv\extensions\DatatypeTrait;
class Csv {
@@ -124,7 +125,7 @@ class Csv {
*
* @var string|null
*/
public $sort_type = null;
public $sort_type = SortEnum::SORT_TYPE_REGULAR;
/**
* Delimiter
@@ -726,14 +727,7 @@ class Csv {
$this->titles = $head;
if (!empty($this->sort_by)) {
$sort_type = SORT_REGULAR;
if ($this->sort_type == 'numeric') {
$sort_type = SORT_NUMERIC;
} elseif ($this->sort_type == 'string') {
$sort_type = SORT_STRING;
}
$this->sort_reverse ? krsort($rows, $sort_type) : ksort($rows, $sort_type);
$this->sort_reverse ? krsort($rows, $this->sort_type) : ksort($rows, $this->sort_type);
if ($this->offset !== null || $this->limit !== null) {
$rows = array_slice($rows, ($this->offset === null ? 0 : $this->offset), $this->limit, true);

18
src/enums/SortEnum.php Normal file
View File

@@ -0,0 +1,18 @@
<?php
/**
* Created by PhpStorm.
* User: sgorzaly
* Date: 22.02.18
* Time: 20:24
*/
namespace ParseCsv\enums;
class SortEnum {
const SORT_TYPE_REGULAR = SORT_REGULAR;
const SORT_TYPE_NUMERIC = SORT_NUMERIC;
const SORT_TYPE_STRING = SORT_STRING;
}