Completed
Push — master ( 46dbb7...9c8538 )
by Hong
02:48
created

OrderTrait::orderRaw()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Query
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Query\Traits\Clause;
16
17
use Phossa2\Query\Misc\Template;
18
use Phossa2\Query\Interfaces\Clause\OrderInterface;
19
20
/**
21
 * OrderTrait
22
 *
23
 * Implementation of OrderInterface
24
 *
25
 * @package Phossa2\Query
26
 * @author  Hong Zhang <[email protected]>
27
 * @see     OrderInterface
28
 * @version 2.0.0
29
 * @since   2.0.0 added
30
 */
31
trait OrderTrait
32
{
33
    use AbstractTrait;
34
35
    /**
36
     * {@inheritDoc}
37
     */
38
    public function order($col)
39
    {
40
        return $this->realOrder($col, 'ASC');
41
    }
42
43
    /**
44
     * {@inheritDoc}
45
     */
46
    public function orderDesc($col)
47
    {
48
        return $this->realOrder($col, 'DESC');
49
    }
50
51
    /**
52
     * {@inheritDoc}
53
     */
54
    public function orderTpl(/*# string */ $template, $col)
55
    {
56
        return $this->realOrder(new Template($template, $col), '', true);
57
    }
58
59
    /**
60
     * {@inheritDoc}
61
     */
62
    public function orderRaw(/*# string */ $rawString)
63
    {
64
        $rawString = $this->positionedParam($rawString, func_get_args(), 1);
65
        return $this->realOrder($rawString, '',  true);
66
    }
67
68
    /**
69
     * Real orderby
70
     *
71
     * @param  string|string[]|Template $col
72
     * @param  string $suffix 'ASC' or 'DESC'
73
     * @param  bool $rawMode
74
     * @return $this
75
     * @access protected
76
     */
77 View Code Duplication
    protected function realOrder(
78
        $col,
79
        /*# sting */ $suffix = 'ASC',
80
        /*# bool */ $rawMode = false
81
    ) {
82
        if (is_array($col)) {
83
            $this->multipleOrder($col, $suffix);
84
        } else {
85
            $clause = &$this->getClause('ORDER BY');
86
            $part = [$col, $this->isRaw($col, $rawMode)];
87
            if (!empty($suffix)) {
88
                $part[] = $suffix;
89
            }
90
            $clause[] = $part;
91
        }
92
        return $this;
93
    }
94
95
    /**
96
     * Multitple orderbys
97
     *
98
     * @param  array $cols
99
     * @param  string $suffix 'ASC' or 'DESC'
100
     * @access protected
101
     */
102
    protected function multipleOrder(array $cols, /*# sting */ $suffix)
103
    {
104
        foreach ($cols as $col) {
105
            $this->realOrder($col, $suffix);
106
        }
107
    }
108
109
    /**
110
     * Build ORDER BY
111
     *
112
     * @param  string $prefix
113
     * @param  array $settings
114
     * @return string
115
     * @access protected
116
     */
117
    protected function buildOrder(
118
        /*# string */ $prefix,
119
        array $settings
120
    )/*# : string */ {
121
        return $this->buildClause('ORDER BY', $prefix, $settings);
122
    }
123
}
124