From 80676591a5cc34afdda2df8da4d08bb39baee290 Mon Sep 17 00:00:00 2001 From: Krzysztof Grabania Date: Wed, 10 Jul 2024 13:49:04 +0200 Subject: [PATCH] fix: add notification object serialization --- phpstan-baseline.neon | 40 ++++++++++++++++++++++++++++++++++ src/Core/Notification.php | 46 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 85 insertions(+), 1 deletion(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 303f0179..96d2a0c7 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -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\\, 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\\\\) 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 diff --git a/src/Core/Notification.php b/src/Core/Notification.php index e3dab9bf..ce15bdb5 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 array + */ + public function __serialize(): array + { + /** @var array */ + return $this->to('array'); + } + + /** + * Unserializes Notification instance. + * + * @param array $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']), + array_values($data['carriers']) + ) + ); + } }