Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add notification object serialization #474

Merged
merged 7 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ parameters:
version?: int,
}
'''
NotificationAsArray: '''
array{
hash: string,
title: string,
trigger: string,
carriers: array<string,array<string,mixed>>,
enabled: bool,
extras: array<string, array<mixed>|bool|float|int|string>,
version: int,
}
'''
NotificationUnconvertedData: '''
array{
hash?: string,
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ Removed deprecated hooks:
* [Fixed] Stripping shortcodes in carrier fields.
* [Fixed] Email carrier header "From" prioritized over header in settings.
* [Fixed] User password reset link requires encoded username.
* [Fixed] Notification class serialization.
* [Removed] DOING_NOTIFICATION_SAVE constant.
* [Removed] NotificationQueries class in favor of NotificationDatabaseService.
* [Removed] Webook and Webhook JSON Carriers.
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 NotificationAsArray
*/
public function __serialize(): array
{
/** @var NotificationAsArray */
return $this->to('array');
}

/**
* Unserializes Notification instance.
*
* @param NotificationAsArray $data Notification data as array.
* @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']),
array_values($data['carriers'])
)
);
}
}
2 changes: 1 addition & 1 deletion src/Repository/Converter/ArrayConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function from($data): Notification
* @since [Next]
* @param Notification $notification Notification instance
* @param array<string|int,mixed> $config The additional configuration of the converter
* @return mixed
* @return NotificationAsArray
*/
public function to(Notification $notification, array $config = [])
{
Expand Down
Loading