PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 1.10.2
Fluent Support – Helpdesk & Customer Support Ticket System v1.10.2
2.4.0 2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 All 68 releases
fluent-support / vendor / wpfluent / framework / src / WPFluent / Database / BaseGrammar.php

BaseGrammar.php in Fluent Support – Helpdesk & Customer Support Ticket System 1.10.2, at vendor/wpfluent/framework/src/WPFluent/Database/BaseGrammar.php

239 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentSupport\Framework\Database;
4
5 use FluentSupport\Framework\Support\Helper;
6 use FluentSupport\Framework\Support\MacroableTrait;
7 use FluentSupport\Framework\Database\Query\Expression;
8
9 abstract class BaseGrammar
10 {
11 use MacroableTrait;
12
13 /**
14 * The grammar table prefix.
15 *
16 * @var string
17 */
18 protected $tablePrefix = '';
19
20 /**
21 * Wrap an array of values.
22 *
23 * @param array $values
24 * @return array
25 */
26 public function wrapArray(array $values)
27 {
28 return array_map([$this, 'wrap'], $values);
29 }
30
31 /**
32 * Wrap a table in keyword identifiers.
33 *
34 * @param \FluentSupport\Framework\Database\Query\Expression|string $table
35 * @return string
36 */
37 public function wrapTable($table)
38 {
39 if (! $this->isExpression($table)) {
40 return $this->wrap(
41 $this->getTableNameWithPrefix($table), true
42 );
43 }
44
45 return $this->getValue($table);
46 }
47
48 /**
49 * Get the grammar's full table name.
50 * Handles multisite table names
51 *
52 * @param string $table
53 * @return string $tableName
54 */
55 protected function getTableNameWithPrefix($table)
56 {
57 global $wpdb;
58
59 return isset($wpdb->{$table}) ? $wpdb->{$table} : $this->tablePrefix.$table;
60 }
61
62 /**
63 * Wrap a value in keyword identifiers.
64 *
65 * @param \FluentSupport\Framework\Database\Query\Expression|string $value
66 * @param bool $prefixAlias
67 * @return string
68 */
69 public function wrap($value, $prefixAlias = false)
70 {
71 if ($this->isExpression($value)) {
72 return $this->getValue($value);
73 }
74
75 // If the value being wrapped has a column alias we will need to separate out
76 // the pieces so we can wrap each of the segments of the expression on its
77 // own, and then join these both back together using the "as" connector.
78 if (stripos($value, ' as ') !== false) {
79 return $this->wrapAliasedValue($value, $prefixAlias);
80 }
81
82 return $this->wrapSegments(explode('.', $value));
83 }
84
85 /**
86 * Wrap a value that has an alias.
87 *
88 * @param string $value
89 * @param bool $prefixAlias
90 * @return string
91 */
92 protected function wrapAliasedValue($value, $prefixAlias = false)
93 {
94 $segments = preg_split('/\s+as\s+/i', $value);
95
96 // If we are wrapping a table we need to prefix the alias with the table prefix
97 // as well in order to generate proper syntax. If this is a column of course
98 // no prefix is necessary. The condition will be true when from wrapTable.
99 if ($prefixAlias) {
100 $segments[1] = $this->tablePrefix.$segments[1];
101 }
102
103 return $this->wrap($segments[0]).' as '.$this->wrapValue($segments[1]);
104 }
105
106 /**
107 * Wrap the given value segments.
108 *
109 * @param array $segments
110 * @return string
111 */
112 protected function wrapSegments($segments)
113 {
114 return Helper::collect($segments)->map(function ($segment, $key) use ($segments) {
115 return $key == 0 && count($segments) > 1
116 ? $this->wrapTable($segment)
117 : $this->wrapValue($segment);
118 })->implode('.');
119 }
120
121 /**
122 * Wrap a single string in keyword identifiers.
123 *
124 * @param string $value
125 * @return string
126 */
127 protected function wrapValue($value)
128 {
129 if ($value !== '*') {
130 return '"'.str_replace('"', '""', $value).'"';
131 }
132
133 return $value;
134 }
135
136 /**
137 * Convert an array of column names into a delimited string.
138 *
139 * @param array $columns
140 * @return string
141 */
142 public function columnize(array $columns)
143 {
144 return implode(', ', array_map([$this, 'wrap'], $columns));
145 }
146
147 /**
148 * Create query parameter place-holders for an array.
149 *
150 * @param array $values
151 * @return string
152 */
153 public function parameterize(array $values)
154 {
155 return implode(', ', array_map([$this, 'parameter'], $values));
156 }
157
158 /**
159 * Get the appropriate query parameter place-holder for a value.
160 *
161 * @param mixed $value
162 * @return string
163 */
164 public function parameter($value)
165 {
166 return $this->isExpression($value) ? $this->getValue($value) : '?';
167 }
168
169 /**
170 * Quote the given string literal.
171 *
172 * @param string|array $value
173 * @return string
174 */
175 public function quoteString($value)
176 {
177 if (is_array($value)) {
178 return implode(', ', array_map([$this, __FUNCTION__], $value));
179 }
180
181 return "'$value'";
182 }
183
184 /**
185 * Determine if the given value is a raw expression.
186 *
187 * @param mixed $value
188 * @return bool
189 */
190 public function isExpression($value)
191 {
192 return $value instanceof Expression;
193 }
194
195 /**
196 * Get the value of a raw expression.
197 *
198 * @param \FluentSupport\Framework\Database\Query\Expression $expression
199 * @return mixed
200 */
201 public function getValue($expression)
202 {
203 return $expression->getValue();
204 }
205
206 /**
207 * Get the format for database stored dates.
208 *
209 * @return string
210 */
211 public function getDateFormat()
212 {
213 return 'Y-m-d H:i:s';
214 }
215
216 /**
217 * Get the grammar's table prefix.
218 *
219 * @return string
220 */
221 public function getTablePrefix()
222 {
223 return $this->tablePrefix;
224 }
225
226 /**
227 * Set the grammar's table prefix.
228 *
229 * @param string $prefix
230 * @return $this
231 */
232 public function setTablePrefix($prefix)
233 {
234 $this->tablePrefix = $prefix;
235
236 return $this;
237 }
238 }
239