Skip to content

Commit

Permalink
fix: add notification object serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Dartui committed Jul 10, 2024
1 parent d782e30 commit 8067659
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
40 changes: 40 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1080,11 +1080,51 @@ parameters:
count: 3
path: src/Core/License.php

-
message: "#^Parameter \\#1 \\$array of function array_keys expects array, mixed given\\.$#"
count: 1
path: src/Core/Notification.php

-
message: "#^Parameter \\#1 \\$array of function array_values expects array, mixed given\\.$#"
count: 1
path: src/Core/Notification.php

-
message: "#^Parameter \\#1 \\$data of method BracketSpace\\\\Notification\\\\Interfaces\\\\Sendable\\:\\:setData\\(\\) expects array\\<string, mixed\\>, mixed given\\.$#"
count: 1
path: src/Core/Notification.php

-
message: "#^Property BracketSpace\\\\Notification\\\\Core\\\\Notification\\:\\:\\$enabled \\(bool\\) does not accept mixed\\.$#"
count: 1
path: src/Core/Notification.php

-
message: "#^Property BracketSpace\\\\Notification\\\\Core\\\\Notification\\:\\:\\$extras \\(array\\<string, array\\|bool\\|float\\|int\\|string\\>\\) does not accept mixed\\.$#"
count: 1
path: src/Core/Notification.php

-
message: "#^Property BracketSpace\\\\Notification\\\\Core\\\\Notification\\:\\:\\$hash \\(string\\) does not accept mixed\\.$#"
count: 1
path: src/Core/Notification.php

-
message: "#^Property BracketSpace\\\\Notification\\\\Core\\\\Notification\\:\\:\\$sourcePostId is never read, only written\\.$#"
count: 1
path: src/Core/Notification.php

-
message: "#^Property BracketSpace\\\\Notification\\\\Core\\\\Notification\\:\\:\\$title \\(string\\) does not accept mixed\\.$#"
count: 1
path: src/Core/Notification.php

-
message: "#^Property BracketSpace\\\\Notification\\\\Core\\\\Notification\\:\\:\\$version \\(int\\) does not accept mixed\\.$#"
count: 1
path: src/Core/Notification.php

-
message: "#^Method BracketSpace\\\\Notification\\\\Core\\\\Resolver\\:\\:clear\\(\\) should return string but returns string\\|null\\.$#"
count: 1
Expand Down
46 changes: 45 additions & 1 deletion src/Core/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Notification
*
* @var Interfaces\Triggerable|null
*/
private $trigger;
private $trigger = null;

/**
* Carriers
Expand Down Expand Up @@ -639,4 +639,48 @@ public function to(string $type, array $config = [])
{
return apply_filters(sprintf('notification/to/%s', $type), $this, $config);
}

/**
* Serialized Notification instance.
*
* @return array<string, mixed>
*/
public function __serialize(): array
{
/** @var array<string, mixed> */
return $this->to('array');
}

/**
* Unserializes Notification instance.
*
* @param array<string, mixed> $data
* @return void
*/
public function __unserialize(array $data): void
{
$this->hash = $data['hash'];
$this->title = $data['title'];
$this->enabled = $data['enabled'];
$this->extras = $data['extras'];
$this->version = $data['version'];
$this->trigger = is_string($data['trigger']) ? Store\Trigger::get($data['trigger']) : null;
$this->carriers = array_filter(
array_map(
function($carrierSlug, $carrierData) {
$carrier = Store\Carrier::get($carrierSlug);

if (!$carrier instanceof Interfaces\Sendable) {
return null;
}

$carrier->setData($carrierData);

return $carrier;
},
array_keys($data['carriers']),

Check failure on line 681 in src/Core/Notification.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4)

Parameter #1 $input of function array_keys expects array, mixed given.
array_values($data['carriers'])

Check failure on line 682 in src/Core/Notification.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4)

Parameter #1 $input of function array_values expects array, mixed given.
)
);
}
}

0 comments on commit 8067659

Please sign in to comment.