/** * Builds the body of API call * * @param array $record * @return string */ private function buildContent($record) { $dataArray = array('token' => $this->token, 'channel' => $this->channel, 'username' => $this->username, 'text' => '', 'attachments' => array()); if ($this->useAttachment) { $attachment = array('fallback' => $record['message'], 'color' => $this->getAttachmentColor($record['level'])); if ($this->useShortAttachment) { $attachment['fields'] = array(array('title' => $record['level_name'], 'value' => $record['message'], 'short' => false)); } else { $attachment['fields'] = array(array('title' => 'Message', 'value' => $record['message'], 'short' => false), array('title' => 'Level', 'value' => $record['level_name'], 'short' => true)); } if ($this->includeExtra) { $extra = ''; foreach ($record['extra'] as $var => $val) { $extra .= $var . ': ' . $this->lineFormatter->stringify($val) . " | "; } $extra = rtrim($extra, " |"); $attachment['fields'][] = array('title' => "Extra", 'value' => $extra, 'short' => false); } $dataArray['attachments'] = json_encode(array($attachment)); } else { $dataArray['text'] = $record['message']; } if ($this->iconEmoji) { $dataArray['icon_emoji'] = ":{$this->iconEmoji}:"; } return http_build_query($dataArray); }
/** * Stringifies an array of key/value pairs to be used in attachment fields * * @param array $fields * @access protected * @return string */ protected function stringify($fields) { $string = ''; foreach ($fields as $var => $val) { $string .= $var . ': ' . $this->lineFormatter->stringify($val) . " | "; } $string = rtrim($string, " |"); return $string; }
/** * @inheritdoc */ public function stringify($value) { if (is_array($value)) { $result = []; foreach ($value as $k => $v) { $result[] = "{$k}={$v}"; } return '[' . implode(', ', $result) . ']'; } return parent::stringify($value); }
/** * Generates attachment field * * @param string $title * @param string|array $value * @param bool $short * @return array */ private function generateAttachmentField($title, $value, $short) { return array('title' => $title, 'value' => is_array($value) ? $this->lineFormatter->stringify($value) : $value, 'short' => $short); }