-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathUtilities.php
More file actions
146 lines (131 loc) · 3.54 KB
/
Copy pathUtilities.php
File metadata and controls
146 lines (131 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
namespace Envms\FluentPDO;
/**
* Class Utilities
*/
class Utilities
{
/**
* Convert "camelCaseWord" to "CAMEL CASE WORD"
*
* @param string $string
*
* @return string
*/
public static function toUpperWords($string)
{
$regex = new Regex();
return trim(strtoupper($regex->camelCaseSpaced($string)));
}
/**
* @param string $query
*
* @return string
*/
public static function formatQuery($query)
{
$regex = new Regex();
$query = $regex->splitClauses($query);
$query = $regex->splitSubClauses($query);
$query = $regex->removeLineEndWhitespace($query);
return $query;
}
/**
* Converts columns from strings to types according to PDOStatement::columnMeta()
*
* @param \PDOStatement $statement
* @param array|\Traversable $rows - provided by PDOStatement::fetch with PDO::FETCH_ASSOC
*
* @return array|\Traversable
*/
public static function stringToNumeric(\PDOStatement $statement, $rows)
{
$columnCount = $statement->columnCount();
for ($i = 0; $i < $columnCount && ($columnMeta = $statement->getColumnMeta($i)) !== false; $i++)
{
$type = $columnMeta['native_type'];
switch ($type) {
case 'DECIMAL':
case 'DOUBLE':
case 'FLOAT':
case 'INT24':
case 'LONG':
case 'LONGLONG':
case 'NEWDECIMAL':
case 'SHORT':
case 'TINY':
if (isset($rows[$columnMeta['name']])) {
$rows[$columnMeta['name']] = $rows[$columnMeta['name']] + 0;
} else {
if (is_array($rows) || $rows instanceof \Traversable) {
foreach ($rows as &$row) {
if (isset($row[$columnMeta['name']])) {
$row[$columnMeta['name']] = $row[$columnMeta['name']] + 0;
}
}
unset($row);
}
}
break;
default:
// return as string
break;
}
}
return $rows;
}
/**
* @param $value
*
* @return bool
*/
public static function convertSqlWriteValues($value)
{
if (is_array($value)) {
foreach ($value as $k => $v) {
$value[$k] = self::convertValue($v);
}
} else {
$value = self::convertValue($value);
}
return $value;
}
/**
* @param $value
*
* @return int|string
*/
public static function convertValue($value)
{
switch (gettype($value)) {
case 'boolean':
$conversion = ($value) ? 1 : 0;
break;
default:
$conversion = $value;
break;
}
return $conversion;
}
/**
* @param $subject
*
* @return bool
*/
public static function isCountable($subject)
{
return (is_array($subject) || ($subject instanceof \Countable));
}
/**
* @param $value
*
* @return Literal|mixed
*/
public static function nullToLiteral($value)
{
if ($value === null) {
return new Literal('NULL');
}
return $value;
}
}