diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:38:04 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:38:04 +0000 |
commit | 1ff5c35de5dbd70a782875a91dd2232fd01b002b (patch) | |
tree | 77d9ce5e1bf78b3e6ef79f8f6e7861e2ced3c09b /vendor/ipl/orm/src/Common/SortUtil.php | |
parent | Initial commit. (diff) | |
download | icinga-php-library-upstream.tar.xz icinga-php-library-upstream.zip |
Adding upstream version 0.10.1.upstream/0.10.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/ipl/orm/src/Common/SortUtil.php')
-rw-r--r-- | vendor/ipl/orm/src/Common/SortUtil.php | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/vendor/ipl/orm/src/Common/SortUtil.php b/vendor/ipl/orm/src/Common/SortUtil.php new file mode 100644 index 0000000..0e014e9 --- /dev/null +++ b/vendor/ipl/orm/src/Common/SortUtil.php @@ -0,0 +1,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|null Sort column(s) and direction(s) suitable for {@link OrderByInterface::orderBy()} + */ + public static function createOrderBy($sort) + { + $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); + } +} |