Completed
Push — master ( 73c72f...f56a01 )
by Hong
02:17
created

WhereTrait   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 224
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 224
c 0
b 0
f 0
wmc 23
lcom 1
cbo 1
rs 10

18 Methods

Rating   Name   Duplication   Size   Complexity  
A where() 0 7 1
A whereTpl() 0 5 1
A orWhereTpl() 0 5 1
A whereRaw() 0 5 1
A orWhereRaw() 0 5 1
A andWhere() 0 7 1
A orWhere() 0 7 1
A whereNot() 0 7 1
A orWhereNot() 0 7 1
A realWhere() 0 23 3
A multipleWhere() 0 16 3
A buildWhere() 0 20 4
A buildWhereClause() 0 21 4
isRaw() 0 1 ?
processValue() 0 1 ?
getClause() 0 1 ?
quoteItem() 0 1 ?
joinClause() 0 6 ?
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\WhereInterface;
19
20
/**
21
 * WhereTrait
22
 *
23
 * Implementation of WhereInterface
24
 *
25
 * @package Phossa2\Query
26
 * @author  Hong Zhang <[email protected]>
27
 * @see     WhereInterface
28
 * @version 2.0.0
29
 * @since   2.0.0 added
30
 */
31
trait WhereTrait
32
{
33
    /**
34
     * {@inheritDoc}
35
     */
36
    public function where(
37
        $col,
38
        $operator = WhereInterface::NO_OPERATOR,
39
        $value = WhereInterface::NO_VALUE
40
    ) {
41
        return $this->realWhere($col, $operator, $value);
42
    }
43
44
    /**
45
     * {@inheritDoc}
46
     */
47
    public function whereTpl(/*# string */ $template, $col)
48
    {
49
        return $this->realWhere(new Template($template, $col),
50
            WhereInterface::NO_OPERATOR, WhereInterface::NO_VALUE, 'AND', '');
51
    }
52
53
    /**
54
     * {@inheritDoc}
55
     */
56
    public function orWhereTpl(/*# string */ $template, $col)
57
    {
58
        return $this->realWhere(new Template($template, $col),
59
            WhereInterface::NO_OPERATOR, WhereInterface::NO_VALUE, 'OR', '');
60
    }
61
62
    /**
63
     * {@inheritDoc}
64
     */
65
    public function whereRaw(/*# string */ $rawString)
66
    {
67
        return $this->realWhere($rawString, WhereInterface::NO_OPERATOR,
68
            WhereInterface::NO_VALUE, 'AND', '', true);
69
    }
70
71
    /**
72
     * {@inheritDoc}
73
     */
74
    public function orWhereRaw(/*# string */ $rawString)
75
    {
76
        return $this->realWhere($rawString, WhereInterface::NO_OPERATOR,
77
            WhereInterface::NO_VALUE, 'OR', '', true);
78
    }
79
80
    /**
81
     * {@inheritDoc}
82
     */
83
    public function andWhere(
84
        $col,
85
        $operator = WhereInterface::NO_OPERATOR,
86
        $value = WhereInterface::NO_VALUE
87
    ) {
88
        return $this->realWhere($col, $operator, $value);
89
    }
90
91
    /**
92
     * {@inheritDoc}
93
     */
94
    public function orWhere(
95
        $col,
96
        $operator = WhereInterface::NO_OPERATOR,
97
        $value = WhereInterface::NO_VALUE
98
    ) {
99
        return $this->realWhere($col, $operator, $value, 'OR');
100
    }
101
102
    /**
103
     * {@inheritDoc}
104
     */
105
    public function whereNot(
106
        $col,
107
        $operator = WhereInterface::NO_OPERATOR,
108
        $value = WhereInterface::NO_VALUE
109
    ) {
110
        return $this->realWhere($col, $operator, $value, 'AND', 'NOT');
111
    }
112
113
    /**
114
     * {@inheritDoc}
115
     */
116
    public function orWhereNot(
117
        $col,
118
        $operator = WhereInterface::NO_OPERATOR,
119
        $value = WhereInterface::NO_VALUE
120
    ) {
121
        return $this->realWhere($col, $operator, $value, 'OR', 'NOT');
122
    }
123
124
    /**
125
     * Real where
126
     *
127
     * @param  string|string[]|Template $col col or cols
128
     * @param  mixed $operator
129
     * @param  mixed $value
130
     * @param  string $logicAnd 'AND'
131
     * @param  string $whereNot 'WHERE NOT'
132
     * @param  bool $rawMode
133
     * @param  string $clause 'where' or 'having'
134
     * @return $this
135
     * @access protected
136
     */
137
    protected function realWhere(
138
        $col,
139
        $operator = WhereInterface::NO_OPERATOR,
140
        $value    = WhereInterface::NO_VALUE,
141
        /*# string */ $logicAnd = 'AND',
142
        /*# string */ $whereNot = '',
143
        /*# bool */ $rawMode = false,
144
        /*# string */ $clause = 'WHERE'
145
    ) {
146
        $clause = &$this->getClause($clause);
147
        if (is_array($col)) {
148
            $this->multipleWhere($col, $logicAnd, $whereNot, $rawMode);
149
            return $this;
150
        }
151
152
        if (WhereInterface::NO_VALUE === $value) {
153
            $value = $operator;
154
            $operator = '=';
155
        }
156
157
        $clause[] = [$rawMode, $whereNot, $logicAnd, $col, $operator, $value];
158
        return $this;
159
    }
160
161
    /**
162
     * @param  array $cols
163
     * @param  string $logicAnd
164
     * @param  string $whereNot
165
     * @param  bool $rawMode
166
     * @access protected
167
     */
168
    protected function multipleWhere(
169
        array $cols,
170
        /*# string */ $logicAnd = 'AND',
171
        /*# string */ $whereNot = '',
172
        /*# bool */ $rawMode  = false
173
    ) {
174
        foreach ($cols as $fld => $val) {
175
            if (is_array($val)) {
176
                $opr = $val[0];
177
                $val = $val[1];
178
            } else {
179
                $opr = '=';
180
            }
181
            $this->realWhere($fld, $opr, $val, $logicAnd, $whereNot, $rawMode);
182
        }
183
    }
184
185
    /**
186
     * Build WHERE
187
     *
188
     * @param  string $clause 'where|having'
189
     * @return array
190
     * @access protected
191
     */
192
    protected function buildWhere(
193
        array $settings,
194
        /*# string */ $clause = 'WHERE'
195
    )/*# : string */ {
196
        $result = [];
197
        $wheres = &$this->getClause($clause);
198
        foreach ($wheres as $idx => $where) {
199
            $cls = [];
200
            // AND OR
201
            if ($idx) {
202
                $cls[] = $where[2];
203
            }
204
            // NOT
205
            if ($where[1]) {
206
                $cls[] = $where[1];
207
            }
208
            $result[] = $this->buildWhereClause($cls, $where, $settings);
209
        }
210
        return $this->joinClause($clause, '', $result, $settings);
211
    }
212
213
    /**
214
     * Build 'col = val' part
215
     *
216
     * @param  array $cls
217
     * @param  array $where
218
     * @param  array $settings
219
     * @return string
220
     * @access protected
221
     */
222
    protected function buildWhereClause(array $cls, array $where, array $settings)
223
    {
224
        // col
225
        if (!empty($where[3])) {
226
            $cls[] = $this->quoteItem(
227
                $where[3], $settings, $this->isRaw($where[3], $where[0])
228
            );
229
        }
230
231
        // operator
232
        if (WhereInterface::NO_OPERATOR !== $where[4]) {
233
            $cls[] = $where[4];
234
        }
235
236
        // value
237
        if (WhereInterface::NO_VALUE !== $where[5]) {
238
            $cls[] = $this->processValue($where[5], $settings);
239
        }
240
241
        return join(' ', $cls);
242
    }
243
244
    abstract protected function isRaw($str, /*# bool */ $rawMode)/*# : bool */;
245
    abstract protected function processValue($value, array $settings)/*# : string */;
246
    abstract protected function &getClause(/*# string */ $clauseName)/*# : array */;
247
    abstract protected function quoteItem($item, array $settings, /*# bool */ $rawMode = false)/*# : string */;
248
    abstract protected function joinClause(
249
        /*# : string */ $prefix,
250
        /*# : string */ $seperator,
251
        array $clause,
252
        array $settings
253
    )/*# : string */;
254
}
255