Skip to content

Commit

Permalink
Change APS Type to Object
Browse files Browse the repository at this point in the history
Fix Reviews
  • Loading branch information
Hugo Nicolas committed Oct 31, 2019
1 parent 3f0d9d3 commit 1f97f0e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
18 changes: 9 additions & 9 deletions src/Payload.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public function getCustomValue($key)
*/
public function toJson(): string
{
return json_encode($this, JSON_UNESCAPED_UNICODE | JSON_FORCE_OBJECT);
return json_encode($this, JSON_UNESCAPED_UNICODE);
}

/**
Expand All @@ -323,31 +323,31 @@ public function jsonSerialize()
$payload = self::getDefaultPayloadStructure();

if ($this->alert instanceof Alert || is_string($this->alert)) {
$payload[self::PAYLOAD_ROOT_KEY][self::PAYLOAD_ALERT_KEY] = $this->alert;
$payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_ALERT_KEY} = $this->alert;
}

if (is_int($this->badge)) {
$payload[self::PAYLOAD_ROOT_KEY][self::PAYLOAD_BADGE_KEY] = $this->badge;
$payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_BADGE_KEY} = $this->badge;
}

if (is_string($this->sound)) {
$payload[self::PAYLOAD_ROOT_KEY][self::PAYLOAD_SOUND_KEY] = $this->sound;
$payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_SOUND_KEY} = $this->sound;
}

if (is_bool($this->contentAvailable)) {
$payload[self::PAYLOAD_ROOT_KEY][self::PAYLOAD_CONTENT_AVAILABLE_KEY] = (int)$this->contentAvailable;
$payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_CONTENT_AVAILABLE_KEY} = (int)$this->contentAvailable;
}

if (is_bool($this->mutableContent)) {
$payload[self::PAYLOAD_ROOT_KEY][self::PAYLOAD_MUTABLE_CONTENT_KEY] = (int)$this->mutableContent;
$payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_MUTABLE_CONTENT_KEY} = (int)$this->mutableContent;
}

if (is_string($this->category)) {
$payload[self::PAYLOAD_ROOT_KEY][self::PAYLOAD_CATEGORY_KEY] = $this->category;
$payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_CATEGORY_KEY} = $this->category;
}

if (is_string($this->threadId)) {
$payload[self::PAYLOAD_ROOT_KEY][self::PAYLOAD_THREAD_ID_KEY] = $this->threadId;
$payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_THREAD_ID_KEY} = $this->threadId;
}

if ((is_array($this->customValues) || $this->customValues instanceof Countable) && count($this->customValues)) {
Expand All @@ -364,6 +364,6 @@ public function jsonSerialize()
*/
private static function getDefaultPayloadStructure()
{
return [self::PAYLOAD_ROOT_KEY => []];
return [self::PAYLOAD_ROOT_KEY => new \stdClass];
}
}
12 changes: 11 additions & 1 deletion tests/PayloadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public function testSetCustomValue()

public function testConvertToJSon()
{
$alert = Alert::create()->setTitle('title');

$alert = Alert::create()->setTitle('title');
$payload = Payload::create()
->setAlert($alert)
->setBadge(1)
Expand All @@ -93,5 +93,15 @@ public function testConvertToJSon()
' "thread-id": "tread-id", "mutable-content": 1, "content-available": 1}, "key": "value"}',
$payload->toJson()
);

}

public function testSetCustomArrayType() {
$alert = Alert::create()->setTitle('title');
$payload = Payload::create()
->setAlert($alert)
->setCustomValue('array', array(1,2,3));

$this->assertEquals(gettype(json_decode($payload->toJson())->array), 'array');
}
}

0 comments on commit 1f97f0e

Please sign in to comment.