1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-04 12:17:35 +02:00

Add fluent setters for parameters in the slack handler

This commit is contained in:
Andrew Smith
2016-05-31 20:48:51 +10:00
parent e5900c3814
commit c9d15bb808
2 changed files with 210 additions and 17 deletions

View File

@@ -85,26 +85,31 @@ class SlackRecord
*/ */
private $normalizerFormatter; private $normalizerFormatter;
public function __construct($channel = null, $username = null, $useAttachment = true, $userIcon = null, $useShortAttachment = false, $includeContextAndExtra = false, array $excludeFields = array(), FormatterInterface $formatter = null) public function __construct(
{ $channel = null,
$this->channel = $channel; $username = null,
$this->username = $username; $useAttachment = true,
$this->userIcon = $userIcon !== null ? trim($userIcon, ':') : null; $userIcon = null,
$this->useAttachment = $useAttachment; $useShortAttachment = false,
$this->useShortAttachment = $useShortAttachment; $includeContextAndExtra = false,
$this->includeContextAndExtra = $includeContextAndExtra; array $excludeFields = array(),
$this->excludeFields = $excludeFields; FormatterInterface $formatter = null
$this->formatter = $formatter; ) {
$this
if ($this->includeContextAndExtra) { ->setChannel($channel)
$this->normalizerFormatter = new NormalizerFormatter(); ->setUsername($username)
} ->useAttachment($useAttachment)
->setUserIcon($userIcon)
->useShortAttachment($useShortAttachment)
->includeContextAndExtra($includeContextAndExtra)
->excludeFields($excludeFields)
->setFormatter($formatter);
} }
public function getSlackData(array $record) public function getSlackData(array $record)
{ {
$dataArray = array(); $dataArray = array();
$record = $this->excludeFields($record); $record = $this->removeExcludedFields($record);
if ($this->username) { if ($this->username) {
$dataArray['username'] = $this->username; $dataArray['username'] = $this->username;
@@ -215,14 +220,114 @@ class SlackRecord
: json_encode($normalized, JSON_UNESCAPED_UNICODE); : json_encode($normalized, JSON_UNESCAPED_UNICODE);
} }
/**
* Channel used by the bot when posting
*
* @param ?string $channel
*
* @return SlackHandler
*/
public function setChannel(?string $channel = null): self
{
$this->channel = $channel;
return $this;
}
/**
* Username used by the bot when posting
*
* @param ?string $username
*
* @return SlackHandler
*/
public function setUsername(?string $username = null): self
{
$this->username = $username;
return $this;
}
/**
* @param bool $useAttachment
*
* @return SlackHandler
*/
public function useAttachment(bool $useAttachment = true): self
{
$this->useAttachment = $useAttachment;
return $this;
}
/**
* @param string $userIcon
*
* @return SlackHandler
*/
public function setUserIcon(?string $userIcon = null): self
{
$this->userIcon = $userIcon;
if (\is_string($userIcon)) {
$this->userIcon = trim($userIcon, ':');
}
return $this;
}
/**
* @param bool $useShortAttachment
*
* @return SlackHandler
*/
public function useShortAttachment(bool $useShortAttachment = false): self
{
$this->useShortAttachment = $useShortAttachment;
return $this;
}
/**
* @param bool $includeContextAndExtra
*
* @return SlackHandler
*/
public function includeContextAndExtra(bool $includeContextAndExtra = false): self
{
$this->includeContextAndExtra = $includeContextAndExtra;
if ($this->includeContextAndExtra) {
$this->normalizerFormatter = new NormalizerFormatter();
}
return $this;
}
/**
* @param array $excludeFields
*
* @return SlackHandler
*/
public function excludeFields(array $excludeFields = []): self
{
$this->excludeFields = $excludeFields;
return $this;
}
/** /**
* Sets the formatter * Sets the formatter
* *
* @param FormatterInterface $formatter * @param FormatterInterface $formatter
*
* @return SlackHandler
*/ */
public function setFormatter(FormatterInterface $formatter) public function setFormatter(?FormatterInterface $formatter = null): self
{ {
$this->formatter = $formatter; $this->formatter = $formatter;
return $this;
} }
/** /**
@@ -270,7 +375,7 @@ class SlackRecord
* *
* @return array * @return array
*/ */
private function excludeFields(array $record) private function removeExcludedFields(array $record)
{ {
foreach ($this->excludeFields as $field) { foreach ($this->excludeFields as $field) {
$keys = explode('.', $field); $keys = explode('.', $field);

View File

@@ -216,4 +216,92 @@ class SlackHandler extends SocketHandler
return $formatter; return $formatter;
} }
/**
* Channel used by the bot when posting
*
* @param string $channel
*
* @return SlackHandler
*/
public function setChannel(string $channel): self
{
$this->slackRecord->setChannel($channel);
return $this;
}
/**
* Username used by the bot when posting
*
* @param string $username
*
* @return SlackHandler
*/
public function setUsername(string $username): self
{
$this->slackRecord->setUsername($username);
return $this;
}
/**
* @param bool $useAttachment
*
* @return SlackHandler
*/
public function useAttachment(bool $useAttachment): self
{
$this->slackRecord->useAttachment($useAttachment);
return $this;
}
/**
* @param string $iconEmoji
*
* @return SlackHandler
*/
public function setIconEmoji(string $iconEmoji): self
{
$this->slackRecord->setUserIcon($iconEmoji);
return $this;
}
/**
* @param bool $useShortAttachment
*
* @return SlackHandler
*/
public function useShortAttachment(bool $useShortAttachment): self
{
$this->slackRecord->useShortAttachment($useShortAttachment);
return $this;
}
/**
* @param bool $includeContextAndExtra
*
* @return SlackHandler
*/
public function includeContextAndExtra(bool $includeContextAndExtra): self
{
$this->slackRecord->includeContextAndExtra($includeContextAndExtra);
return $this;
}
/**
* @param array $excludeFields
*
* @return SlackHandler
*/
public function excludeFields(array $excludeFields): self
{
$this->slackRecord->excludeFields($excludeFields);
return $this;
}
} }