diff --git a/phpstan.neon b/phpstan.neon index 36493271..01c1bbe0 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -25,6 +25,17 @@ parameters: version?: int, } ''' + NotificationAsArray: ''' + array{ + hash: string, + title: string, + trigger: string, + carriers: array>, + enabled: bool, + extras: array|bool|float|int|string>, + version: int, + } + ''' NotificationUnconvertedData: ''' array{ hash?: string, diff --git a/readme.txt b/readme.txt index dc9f9490..8f376792 100644 --- a/readme.txt +++ b/readme.txt @@ -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. diff --git a/src/Core/Notification.php b/src/Core/Notification.php index e3dab9bf..a3a5dca9 100644 --- a/src/Core/Notification.php +++ b/src/Core/Notification.php @@ -36,7 +36,7 @@ class Notification * * @var Interfaces\Triggerable|null */ - private $trigger; + private $trigger = null; /** * Carriers @@ -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']) + ) + ); + } } diff --git a/src/Repository/Converter/ArrayConverter.php b/src/Repository/Converter/ArrayConverter.php index bde7e6f6..04a12c73 100644 --- a/src/Repository/Converter/ArrayConverter.php +++ b/src/Repository/Converter/ArrayConverter.php @@ -71,7 +71,7 @@ public function from($data): Notification * @since [Next] * @param Notification $notification Notification instance * @param array $config The additional configuration of the converter - * @return mixed + * @return NotificationAsArray */ public function to(Notification $notification, array $config = []) {