diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:44:51 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:44:51 +0000 |
commit | a1ec78bf0dc93d0e05e5f066f1949dc3baecea06 (patch) | |
tree | ee596ce1bc9840661386f96f9b8d1f919a106317 /vendor/gipfl/influxdb/src/Escape.php | |
parent | Initial commit. (diff) | |
download | icingaweb2-module-incubator-a1ec78bf0dc93d0e05e5f066f1949dc3baecea06.tar.xz icingaweb2-module-incubator-a1ec78bf0dc93d0e05e5f066f1949dc3baecea06.zip |
Adding upstream version 0.20.0.upstream/0.20.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/gipfl/influxdb/src/Escape.php')
-rw-r--r-- | vendor/gipfl/influxdb/src/Escape.php | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/vendor/gipfl/influxdb/src/Escape.php b/vendor/gipfl/influxdb/src/Escape.php new file mode 100644 index 0000000..e6cb555 --- /dev/null +++ b/vendor/gipfl/influxdb/src/Escape.php @@ -0,0 +1,67 @@ +<?php + +namespace gipfl\InfluxDb; + +use InvalidArgumentException; +use function addcslashes; +use function ctype_digit; +use function is_bool; +use function is_int; +use function is_null; +use function preg_match; +use function strpos; + +abstract class Escape +{ + const ESCAPE_COMMA_SPACE = ' ,\\'; + + const ESCAPE_COMMA_EQUAL_SPACE = ' =,\\'; + + const ESCAPE_DOUBLE_QUOTES = '"\\'; + + const NULL = 'null'; + + const TRUE = 'true'; + + const FALSE = 'false'; + + public static function measurement($value) + { + static::assertNoNewline($value); + return addcslashes($value, self::ESCAPE_COMMA_SPACE); + } + + public static function key($value) + { + static::assertNoNewline($value); + return addcslashes($value, self::ESCAPE_COMMA_EQUAL_SPACE); + } + + public static function tagValue($value) + { + static::assertNoNewline($value); + return addcslashes($value, self::ESCAPE_COMMA_EQUAL_SPACE); + } + + public static function fieldValue($value) + { + // Faster checks first + if (is_int($value) || ctype_digit($value) || preg_match('/^-\d+$/', $value)) { + return "{$value}i"; + } elseif (is_bool($value)) { + return $value ? self::TRUE : self::FALSE; + } elseif (is_null($value)) { + return self::NULL; + } else { + static::assertNoNewline($value); + return '"' . addcslashes($value, self::ESCAPE_DOUBLE_QUOTES) . '"'; + } + } + + protected static function assertNoNewline($value) + { + if (strpos($value, "\n") !== false) { + throw new InvalidArgumentException('Newlines are forbidden in InfluxDB line protocol'); + } + } +} |