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\Interfaces\RawInterface; |
18
|
|
|
use Phossa2\Query\Interfaces\ClauseInterface; |
19
|
|
|
use Phossa2\Query\Interfaces\StatementInterface; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* ClauseTrait |
24
|
|
|
* |
25
|
|
|
* @package Phossa2\Query |
26
|
|
|
* @author Hong Zhang <[email protected]> |
27
|
|
|
* @see ClauseInterface |
28
|
|
|
* @version 2.0.0 |
29
|
|
|
* @since 2.0.0 added |
30
|
|
|
*/ |
31
|
|
|
trait ClauseTrait |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* storage for clauses |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
* @access protected |
38
|
|
|
*/ |
39
|
|
|
protected $clause = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Is $str a raw string ? |
43
|
|
|
* |
44
|
|
|
* @param mixed $str |
45
|
|
|
* @param bool $rawMode |
46
|
|
|
* @access protected |
47
|
|
|
*/ |
48
|
|
|
protected function isRaw($str, /*# bool */ $rawMode)/*# : bool */ |
49
|
|
|
{ |
50
|
|
|
if ($rawMode) { |
51
|
|
|
return true; |
52
|
|
|
} elseif (is_string($str)) { |
53
|
|
|
return (bool) preg_match('/[^0-9a-zA-Z\$_.]/', $str); |
54
|
|
|
} elseif (is_object($str) && $str instanceof RawInterface) { |
55
|
|
|
return true; |
56
|
|
|
} |
57
|
|
|
return false; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Quote an alias if not an int |
62
|
|
|
* |
63
|
|
|
* @param int|string $alias |
64
|
|
|
* @return string |
65
|
|
|
* @access protected |
66
|
|
|
*/ |
67
|
|
|
protected function quoteAlias($alias)/*# : string */ |
68
|
|
|
{ |
69
|
|
|
return is_int($alias) ? '' : (' AS ' . $this->quoteSpace($alias)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Quote an item with spaces allowed in between |
74
|
|
|
* |
75
|
|
|
* @param string|StatementInterface $item |
76
|
|
|
* @param bool $rawMode |
77
|
|
|
* @access protected |
78
|
|
|
*/ |
79
|
|
|
protected function quoteItem( |
80
|
|
|
$item, /*# bool */ $rawMode = false |
81
|
|
|
)/*# : string */ { |
82
|
|
|
if (is_object($item) && $item instanceof StatementInterface) { |
83
|
|
|
// @TODO quoteItem check settings |
84
|
|
|
return '(' . $item->getStatement([], false) . ')'; |
|
|
|
|
85
|
|
|
} else { |
86
|
|
|
return $rawMode ? (string) $item : $this->quote($item); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Return specific clause part |
92
|
|
|
* |
93
|
|
|
* @param string $clauseName |
94
|
|
|
* @param array |
95
|
|
|
* @access protected |
96
|
|
|
*/ |
97
|
|
|
protected function &getClause(/*# string */ $clauseName)/*# : array */ |
98
|
|
|
{ |
99
|
|
|
if (empty($clauseName)) { |
100
|
|
|
return $this->clause; |
101
|
|
|
} else { |
102
|
|
|
if (!isset($this->clause[$clauseName])) { |
103
|
|
|
$this->clause[$clauseName] = []; |
104
|
|
|
} |
105
|
|
|
return $this->clause[$clauseName]; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Convert a template 'COUNT(%s)' to 'COUNT(`score`)' |
111
|
|
|
* |
112
|
|
|
* @param string $template |
113
|
|
|
* @param string|string[] $col column[s] |
114
|
|
|
* @return string |
115
|
|
|
* @access protected |
116
|
|
|
*/ |
117
|
|
|
protected function clauseTpl(/*# string */ $template, $col)/*# : string */ |
118
|
|
|
{ |
119
|
|
|
$quoted = []; |
120
|
|
|
foreach ((array) $col as $c) { |
121
|
|
|
$quoted[] = $this->quote($c); |
122
|
|
|
} |
123
|
|
|
return vsprintf($template, $quoted); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
protected function quote(/*# string */ $str)/*# : string */ |
127
|
|
|
{ |
128
|
|
|
// @TODO quote() |
|
|
|
|
129
|
|
|
return '"' . $str . '"'; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
protected function quoteSpace(/*# string */ $str)/*# : string */ |
133
|
|
|
{ |
134
|
|
|
// @TODO quoteSpace |
135
|
|
|
return $this->quote($str); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Process value |
140
|
|
|
* |
141
|
|
|
* @param mixed $value |
142
|
|
|
* @return string |
143
|
|
|
* @access protected |
144
|
|
|
*/ |
145
|
|
|
protected function processValue($value)/*# : string */ |
146
|
|
|
{ |
147
|
|
|
// @TODO processValue |
148
|
|
|
return (string) $value; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Join each clause |
153
|
|
|
* |
154
|
|
|
* @param string $prefix |
155
|
|
|
* @param string $seperator |
156
|
|
|
* @param array $clause |
157
|
|
|
* @param array $settings |
158
|
|
|
* @return string |
159
|
|
|
* @access protected |
160
|
|
|
*/ |
161
|
|
|
protected function joinClause( |
162
|
|
|
/*# : string */ $prefix, |
163
|
|
|
/*# : string */ $seperator, |
164
|
|
|
array $clause, |
165
|
|
|
array $settings |
166
|
|
|
)/*# : string */ { |
167
|
|
|
if (empty($clause)) { |
168
|
|
|
return ''; |
169
|
|
|
} else { |
170
|
|
|
$join = $settings['seperator'] . $settings['indent']; |
171
|
|
|
$pref = empty($prefix) ? '' : ($prefix . $join); |
172
|
|
|
return $settings['seperator'] . $pref . join($seperator . $join, $clause); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.