1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-10-20 08:06:19 +02:00

Merge remote-tracking branch 'EspadaV8/add-fluent-setters-for-slack-handler'

This commit is contained in:
Jordi Boggiano
2019-07-06 13:46:59 +02:00
2 changed files with 146 additions and 19 deletions

View File

@@ -86,23 +86,24 @@ class SlackRecord
private $normalizerFormatter; private $normalizerFormatter;
public function __construct( public function __construct(
?string $channel = null, ?string $channel = null,
?string $username = null, ?string $username = null,
bool $useAttachment = true, bool $useAttachment = true,
?string $userIcon = null, ?string $userIcon = null,
bool $useShortAttachment = false, bool $useShortAttachment = false,
bool $includeContextAndExtra = false, bool $includeContextAndExtra = false,
array $excludeFields = array(), array $excludeFields = array(),
FormatterInterface $formatter = null FormatterInterface $formatter = null
) { ) {
$this->channel = $channel; $this
$this->username = $username; ->setChannel($channel)
$this->userIcon = $userIcon !== null ? trim($userIcon, ':') : null; ->setUsername($username)
$this->useAttachment = $useAttachment; ->useAttachment($useAttachment)
$this->useShortAttachment = $useShortAttachment; ->setUserIcon($userIcon)
$this->includeContextAndExtra = $includeContextAndExtra; ->useShortAttachment($useShortAttachment)
$this->excludeFields = $excludeFields; ->includeContextAndExtra($includeContextAndExtra)
$this->formatter = $formatter; ->excludeFields($excludeFields)
->setFormatter($formatter);
if ($this->includeContextAndExtra) { if ($this->includeContextAndExtra) {
$this->normalizerFormatter = new NormalizerFormatter(); $this->normalizerFormatter = new NormalizerFormatter();
@@ -111,12 +112,12 @@ class SlackRecord
/** /**
* Returns required data in format that Slack * Returns required data in format that Slack
* is expecting. * is expecting.
*/ */
public function getSlackData(array $record): array public function getSlackData(array $record): array
{ {
$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;
@@ -220,7 +221,78 @@ class SlackRecord
: json_encode($normalized, JSON_UNESCAPED_UNICODE); : json_encode($normalized, JSON_UNESCAPED_UNICODE);
} }
public function setFormatter(FormatterInterface $formatter): self /**
* 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;
}
public function useAttachment(bool $useAttachment = true): self
{
$this->useAttachment = $useAttachment;
return $this;
}
public function setUserIcon(?string $userIcon = null): self
{
$this->userIcon = $userIcon;
if (\is_string($userIcon)) {
$this->userIcon = trim($userIcon, ':');
}
return $this;
}
public function useShortAttachment(bool $useShortAttachment = false): self
{
$this->useShortAttachment = $useShortAttachment;
return $this;
}
public function includeContextAndExtra(bool $includeContextAndExtra = false): self
{
$this->includeContextAndExtra = $includeContextAndExtra;
if ($this->includeContextAndExtra) {
$this->normalizerFormatter = new NormalizerFormatter();
}
return $this;
}
public function excludeFields(array $excludeFields = []): self
{
$this->excludeFields = $excludeFields;
return $this;
}
public function setFormatter(?FormatterInterface $formatter = null): self
{ {
$this->formatter = $formatter; $this->formatter = $formatter;
@@ -261,7 +333,7 @@ class SlackRecord
/** /**
* Get a copy of record with fields excluded according to $this->excludeFields * Get a copy of record with fields excluded according to $this->excludeFields
*/ */
private function excludeFields(array $record): array private function removeExcludedFields(array $record): array
{ {
foreach ($this->excludeFields as $field) { foreach ($this->excludeFields as $field) {
$keys = explode('.', $field); $keys = explode('.', $field);

View File

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