measurement = (string) $measurement; $this->tags = $tags; $this->fields = $additionalFields; if ($value) { $this->fields['value'] = $value; } if ($timestamp && !$this->isValidTimeStamp($timestamp)) { throw new DatabaseException(sprintf('%s is not a valid timestamp', $timestamp)); } $this->timestamp = $timestamp; } /** * @see: https://influxdb.com/docs/v0.9/concepts/reading_and_writing_data.html * * Should return this format * 'cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000' */ public function __toString() { $string = $this->measurement; if (count($this->tags) > 0) { $string .= ',' . $this->arrayToString($this->tags); } $string .= ' ' . $this->arrayToString($this->fields); if ($this->timestamp) { $string .= ' '.$this->timestamp; } return $string; } /** * @param array $arr * @return string */ private function arrayToString(array $arr) { $strParts = []; foreach ($arr as $key => $value) { $strParts[] = sprintf('%s=%s', $key, $value); } return implode(',', $strParts); } /** * @param int $timestamp * @return bool */ private function isValidTimeStamp($timestamp) { if ((int) $timestamp === $timestamp) { return true; } if ($timestamp <= PHP_INT_MAX && $timestamp >= ~PHP_INT_MAX) { return true; } return false; } }