Skip to content

Commit

Permalink
Major Bug Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
afiqiqmal committed Nov 13, 2020
1 parent 8f4947b commit 08d2d27
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 18 deletions.
15 changes: 15 additions & 0 deletions src/Helper/ArrayHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php


namespace Afiqiqmal\HuaweiPush\Helper;


class ArrayHelper
{
static function filter($array)
{
return array_filter($array, function ($var) {
return ($var !== NULL && $var !== FALSE && $var !== '');
});
}
}
7 changes: 4 additions & 3 deletions src/Structure/Android/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Afiqiqmal\HuaweiPush\Structure\Android;


use Afiqiqmal\HuaweiPush\Helper\ArrayHelper;
use Afiqiqmal\HuaweiPush\Structure\Validation\Extras;

class Config implements Extras
Expand Down Expand Up @@ -176,16 +177,16 @@ public function setNotification(Notification $notification)

public function toArray()
{
return [
return ArrayHelper::filter([
'collapse_key' => $this->collapse_state,
'urgency' => self::$urgencyName[$this->urgency] ?? null,
'category' => self::$categoryName[$this->category] ?? null,
'ttl' => "{$this->timeToLive}s",
'bi_tag' => $this->tags,
'fast_app_target' => $this->staging ? 1 : 2,
'data' => $this->data,
'data' => $this->data ? json_encode($this->data) : null,
'notification' => $this->notification ? $this->notification->toArray() : null,
];
]);
}

public function validate()
Expand Down
5 changes: 3 additions & 2 deletions src/Structure/Android/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Afiqiqmal\HuaweiPush\Structure\Android;


use Afiqiqmal\HuaweiPush\Helper\ArrayHelper;
use Afiqiqmal\HuaweiPush\Structure\Common\Badge;
use Afiqiqmal\HuaweiPush\Structure\Common\Button;
use Afiqiqmal\HuaweiPush\Structure\Common\ClickAction;
Expand Down Expand Up @@ -689,7 +690,7 @@ public function setClickAction(ClickAction $click_action)

public function toArray()
{
return [
return ArrayHelper::filter([
'title' => $this->title,
'body' => $this->body,
'icon' => $this->icon,
Expand Down Expand Up @@ -727,7 +728,7 @@ public function toArray()
'buttons' => collect($this->buttons)->map(function ($item) {
return $item->toArray();
})->toArray()
];
]);
}

public function validate()
Expand Down
9 changes: 5 additions & 4 deletions src/Structure/Common/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Afiqiqmal\HuaweiPush\Structure\Common;


use Afiqiqmal\HuaweiPush\Helper\ArrayHelper;
use Afiqiqmal\HuaweiPush\Structure\Validation\Extras;

class Button implements Extras
Expand All @@ -25,7 +26,7 @@ class Button implements Extras
*
* @var integer
*/
private $action_type;
private $action_type = 0;

/**
* Method of opening a custom app page. The options are as follows:
Expand Down Expand Up @@ -120,13 +121,13 @@ public function toArray()
{
$this->validate();

return [
return ArrayHelper::filter([
'name' => $this->name,
'action_type' => $this->action_type,
'intent_type' => $this->intent_type,
'intent' => $this->intent,
'data' => $this->data,
];
'data' => $this->data ? json_encode($this->data) : null,
]);
}

public function validate()
Expand Down
5 changes: 3 additions & 2 deletions src/Structure/Common/LightSetting.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Afiqiqmal\HuaweiPush\Structure\Common;


use Afiqiqmal\HuaweiPush\Helper\ArrayHelper;
use Afiqiqmal\HuaweiPush\Structure\Validation\Extras;

class LightSetting implements Extras
Expand Down Expand Up @@ -77,11 +78,11 @@ public function toArray()
{
$this->validate();

return [
return ArrayHelper::filter([
'color' => $this->color,
'light_on_duration' => $this->light_on_duration,
'light_off_duration' => $this->light_off_duration
];
]);
}

public function validate()
Expand Down
8 changes: 5 additions & 3 deletions src/Structure/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
namespace Afiqiqmal\HuaweiPush\Structure;


use Afiqiqmal\HuaweiPush\Helper\ArrayHelper;

class Message
{
/**
Expand Down Expand Up @@ -130,13 +132,13 @@ public function setCondition(string $condition)

public function toArray()
{
return [
'data' => $this->data ?? null,
return ArrayHelper::filter([
'data' => $this->data ? json_encode($this->data) : null,
'notification' => $this->notification ? $this->notification->toArray() : null,
'android' => $this->android ? $this->android->toArray() : null,
'token' => $this->targetDeviceIds,
'condition' => $this->condition,
'topic' => $this->topic,
];
]);
}
}
6 changes: 4 additions & 2 deletions src/Structure/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
namespace Afiqiqmal\HuaweiPush\Structure;


use Afiqiqmal\HuaweiPush\Helper\ArrayHelper;

class Notification
{
/**
Expand Down Expand Up @@ -74,10 +76,10 @@ public function setImage(string $image)

public function toArray()
{
return [
return ArrayHelper::filter([
'title' => $this->title,
'body' => $this->body,
'image' => $this->image,
];
]);
}
}
6 changes: 4 additions & 2 deletions src/Structure/NotificationPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
namespace Afiqiqmal\HuaweiPush\Structure;


use Afiqiqmal\HuaweiPush\Helper\ArrayHelper;

class NotificationPayload
{
/**
Expand Down Expand Up @@ -46,9 +48,9 @@ public function setMessage(Message $message): NotificationPayload

public function toArray()
{
return [
return ArrayHelper::filter([
'validate_only' => $this->validate_only,
'message' => $this->message->toArray() ?? null
];
]);
}
}

0 comments on commit 08d2d27

Please sign in to comment.