Completed
Push — master ( 5107e5...5a0a86 )
by Jared
02:13
created

OrderStatement::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * @author Jared King <[email protected]>
5
 *
6
 * @link http://jaredtking.com
7
 *
8
 * @copyright 2015 Jared King
9
 * @license MIT
10
 */
11
namespace JAQB\Statement;
12
13
class OrderStatement extends Statement
14
{
15
    protected $groupBy;
16
    protected $fields = [];
17
18
    /**
19
     * @param bool $groupBy when true, statement becomes a group by statement
20
     */
21
    public function __construct($groupBy = false)
22
    {
23
        $this->groupBy = $groupBy;
24
    }
25
26
    /**
27
     * Tells whether this statement is a GROUP BY statement.
28
     *
29
     * @return bool true: is group by, false: is order by
30
     */
31
    public function isGroupBy()
32
    {
33
        return $this->groupBy;
34
    }
35
36
    /**
37
     * Adds fields to this statement
38
     * Support input styles:
39
     * - addFields('field ASC,field2')
40
     * - addFields('field', 'ASC')
41
     * - addFields(['field','field2'], 'DESC')
42
     * - addFields([['field','ASC'], ['field2','ASC']]).
43
     *
44
     * @param string|array $fields
45
     * @param string       $direction direction for fields where direction is unspecified (optional)
46
     *
47
     * @return self
48
     */
49
    public function addFields($fields, $direction = false)
50
    {
51
        if (!is_array($fields)) {
52
            $fields = array_map(function ($f) {
53
                return trim($f);
54
            }, explode(',', $fields));
55
        }
56
57
        foreach ($fields as $field) {
58
            if (!is_array($field)) {
59
                $field = explode(' ', trim($field));
60
            }
61
62
            if (count($field) === 1 && $direction !== false) {
63
                $field[] = $direction;
64
            }
65
66
            // validate the direction
67
            if (count($field) === 1 || in_array(strtolower($field[1]), ['asc', 'desc'])) {
68
                $this->fields[] = $field;
69
            }
70
        }
71
72
        return $this;
73
    }
74
75
    /**
76
     * Gets the fields associated with this statement.
77
     *
78
     * @return array fields i.e. [['field1','ASC'], ['field2']]
79
     */
80
    public function getFields()
81
    {
82
        return $this->fields;
83
    }
84
85
    /**
86
     * Generates the raw SQL string for the statement.
87
     *
88
     * @return string
89
     */
90
    public function build()
91
    {
92
        $fields = $this->fields;
93
        foreach ($fields as &$field) {
94
            $field[0] = $this->escapeIdentifier($field[0]);
95
            $field = implode(' ', $field);
96
        }
97
98
        // remove empty values
99
        $fields = array_filter($fields);
100
101
        if (count($fields) == 0) {
102
            return '';
103
        }
104
105
        return ((!$this->groupBy) ? 'ORDER BY ' : 'GROUP BY ').
106
            implode(',', $fields);
107
    }
108
}
109