blob: a14ea2bd6f0835c1e308b4832809af8117e9dd74 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
<?php
namespace ipl\Orm\Common;
use ipl\Stdlib\Str;
class SortUtil
{
/**
* Create the sort column(s) and direction(s) from the given sort spec
*
* @param array|string $sort
*
* @return array<int, mixed> Sort column(s) and direction(s) suitable for {@link OrderByInterface::orderBy()}
*/
public static function createOrderBy($sort): array
{
$columnsAndDirections = static::explodeSortSpec($sort);
$orderBy = [];
foreach ($columnsAndDirections as $columnAndDirection) {
list($column, $direction) = static::splitColumnAndDirection($columnAndDirection);
$orderBy[] = [$column, $direction];
}
return $orderBy;
}
/**
* Explode the given sort spec into its sort parts
*
* @param array|string $sort
*
* @return array
*/
public static function explodeSortSpec($sort)
{
return Str::trimSplit(implode(',', (array) $sort));
}
/**
* Normalize the given sort spec to a sort string
*
* @param array|string $sort
*
* @return string
*/
public static function normalizeSortSpec($sort)
{
return implode(',', static::explodeSortSpec($sort));
}
/**
* Explode the given sort part into its sort column and direction
*
* @param string $sort
*
* @return array
*/
public static function splitColumnAndDirection($sort)
{
return Str::symmetricSplit($sort, ' ', 2);
}
}
|