Skip to content

Commit

Permalink
Merge pull request #131 from KoIIIeY/patch-1
Browse files Browse the repository at this point in the history
Update Payload.php. Add ability to set array and json string as Alert
  • Loading branch information
edamov authored Mar 22, 2021
2 parents aada72b + cdeeb24 commit 743397b
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions src/Payload.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public static function create(): Payload
/**
* Get Alert.
*
* @return Alert|null
* @return Alert|string|array|null
*/
public function getAlert()
{
Expand All @@ -145,13 +145,13 @@ public function getAlert()
/**
* Set Alert.
*
* @param Alert|string $alert
* @param Alert|string $alert|array $alert
*
* @return Payload
*/
public function setAlert($alert): Payload
{
if ($alert instanceof Alert || is_string($alert)) {
if ($alert instanceof Alert || is_string($alert) || is_array($alert)) {
$this->alert = $alert;
}

Expand Down Expand Up @@ -349,6 +349,22 @@ public function setCustomValue(string $key, $value): Payload

return $this;
}

/**
* Merges custom value for Payload.
*
* @param string $key
* @param mixed $value
*
* @return Payload
* @throws InvalidPayloadException
*/
public function addCustomValue(string $key, $value): Payload
{
$this->customValues = array_merge_recursive($this->customValues ? $this->customValues : [], [$key => $value]);

return $this;
}

/**
* Get custom value.
Expand Down Expand Up @@ -412,7 +428,16 @@ public function jsonSerialize()
{
$payload = self::getDefaultPayloadStructure();

if ($this->alert instanceof Alert || is_string($this->alert)) {
if ($this->alert instanceof Alert) {
$payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_ALERT_KEY} = $this->alert;
} elseif(is_string($this->alert)){
$json = json_decode($this->alert, true);
if($json){
$payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_ALERT_KEY} = $json;
} else {
$payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_ALERT_KEY} = $this->alert;
}
} elseif (is_array($this->alert)){
$payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_ALERT_KEY} = $this->alert;
}

Expand Down

0 comments on commit 743397b

Please sign in to comment.