Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
Order
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
5
100.00% covered (success)
100.00%
1 / 1
 parse
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Db\Sql\Parser;
15
16/**
17 * Order parser class
18 *
19 * @category   Pop
20 * @package    Pop\Db
21 * @author     Nick Sagona, III <dev@nolainteractive.com>
22 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
23 * @license    http://www.popphp.org/license     New BSD License
24 * @version    6.5.0
25 */
26class Order
27{
28
29    /**
30     * Get the order by values
31     *
32     * @param  string $orderBy
33     * @return array
34     */
35    public static function parse(string $orderBy): array
36    {
37        $by    = null;
38        $order = null;
39
40        if (stripos($orderBy, 'ASC') !== false) {
41            $order = 'ASC';
42            $by    = trim(str_replace('ASC', '', $orderBy));
43        } else if (stripos($orderBy, 'DESC') !== false) {
44            $order = 'DESC';
45            $by    = trim(str_replace('DESC', '', $orderBy));
46        } else if (stripos($orderBy, 'RAND()') !== false) {
47            $order = 'RAND()';
48            $by    = trim(str_replace('RAND()', '', $orderBy));
49        } else {
50            $order = null;
51            $by    = $orderBy;
52        }
53
54        if (str_contains($by, ',')) {
55            $by = array_map('trim', explode(',', $by));
56        }
57
58        return ['by' => $by, 'order' => $order];
59    }
60
61}