@@ -82,10 +82,10 @@ public function __toString()
82
82
$ string = $ this ->measurement ;
83
83
84
84
if (count ($ this ->tags ) > 0 ) {
85
- $ string .= ', ' . $ this ->arrayToString ($ this ->escapeCharacters ($ this ->tags ));
85
+ $ string .= ', ' . $ this ->arrayToString ($ this ->escapeTags ($ this ->tags ));
86
86
}
87
87
88
- $ string .= ' ' . $ this ->arrayToString ($ this ->escapeCharacters ($ this ->fields ));
88
+ $ string .= ' ' . $ this ->arrayToString ($ this ->escapeFields ($ this ->fields ));
89
89
90
90
if ($ this ->timestamp ) {
91
91
$ string .= ' ' .$ this ->timestamp ;
@@ -143,7 +143,7 @@ public function setFields($fields)
143
143
if (is_integer ($ field )) {
144
144
$ field = sprintf ('%di ' , $ field );
145
145
} elseif (is_string ($ field )) {
146
- $ field = sprintf ( "\" %s \"" , $ field );
146
+ $ field = $ this -> escapeFieldValue ( $ field );
147
147
} elseif (is_bool ($ field )) {
148
148
$ field = ($ field ? "true " : "false " );
149
149
}
@@ -174,7 +174,7 @@ public function setTimestamp($timestamp)
174
174
* @param array $arr
175
175
* @return array
176
176
*/
177
- private function escapeCharacters (array $ arr )
177
+ private function escapeTags (array $ arr )
178
178
{
179
179
$ returnArr = [];
180
180
@@ -185,6 +185,35 @@ private function escapeCharacters(array $arr)
185
185
return $ returnArr ;
186
186
}
187
187
188
+ /**
189
+ * Escapes invalid characters in field keys and values
190
+ *
191
+ * @param array $arr
192
+ * @return array
193
+ */
194
+ private function escapeFields (array $ arr )
195
+ {
196
+ $ returnArr = [];
197
+
198
+ foreach ($ arr as $ key => $ value ) {
199
+ $ returnArr [$ this ->addSlashes ($ key )] = $ value ;
200
+ }
201
+
202
+ return $ returnArr ;
203
+ }
204
+
205
+ /*
206
+ * Returns string double-quoted and double-quotes escaped per Influx write protocol syntax
207
+ *
208
+ * @param string $value
209
+ * @return string
210
+ */
211
+ private function escapeFieldValue ($ value )
212
+ {
213
+ $ escapedValue = str_replace ('" ' , '\" ' , $ value );
214
+ return sprintf ('"%s" ' , $ escapedValue );
215
+ }
216
+
188
217
/**
189
218
* Returns strings with space, comma, or equals sign characters backslashed per Influx write protocol syntax
190
219
*
@@ -244,7 +273,5 @@ private function isValidTimeStamp($timestamp)
244
273
}
245
274
246
275
return true ;
247
-
248
-
249
276
}
250
277
}
0 commit comments