diff options
author | Côme Chilliet <come.chilliet@nextcloud.com> | 2024-05-30 14:32:26 +0200 |
---|---|---|
committer | Côme Chilliet <91878298+come-nc@users.noreply.github.com> | 2024-06-11 14:10:29 +0200 |
commit | 734aad8934cdbe072c7c880482e5caf88b61400b (patch) | |
tree | 74eeccb411e9903945e0706fc52f258100dc4db6 /lib/public | |
parent | c3d4d2aad1539002e9ce31ad35e6df4801d93197 (diff) | |
download | nextcloud-server-734aad8934cdbe072c7c880482e5caf88b61400b.tar.gz nextcloud-server-734aad8934cdbe072c7c880482e5caf88b61400b.zip |
feat: Make node events serializable
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
Diffstat (limited to 'lib/public')
-rw-r--r-- | lib/public/EventDispatcher/JsonSerializer.php | 42 | ||||
-rw-r--r-- | lib/public/Files/Events/Node/AbstractNodeEvent.php | 13 | ||||
-rw-r--r-- | lib/public/Files/Events/Node/AbstractNodesEvent.php | 12 |
3 files changed, 66 insertions, 1 deletions
diff --git a/lib/public/EventDispatcher/JsonSerializer.php b/lib/public/EventDispatcher/JsonSerializer.php new file mode 100644 index 00000000000..1eb75ed7527 --- /dev/null +++ b/lib/public/EventDispatcher/JsonSerializer.php @@ -0,0 +1,42 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace OCP\EventDispatcher; + +use OCP\Files\FileInfo; +use OCP\IUser; + +/** + * Contains helper static methods to serialize OCP classes into arrays to pass json_encode. + * Useful for events implementing \JsonSerializable. + * + * @since 30.0.0 + */ + +final class JsonSerializer { + /** + * @since 30.0.0 + */ + public static function serializeFileInfo(FileInfo $node): array { + return [ + 'id' => $node->getId(), + 'path' => $node->getPath(), + ]; + } + + /** + * @since 30.0.0 + */ + public static function serializeUser(IUser $user): array { + return [ + 'uid' => $user->getUID(), + 'displayName' => $user->getDisplayName(), + ]; + } +} diff --git a/lib/public/Files/Events/Node/AbstractNodeEvent.php b/lib/public/Files/Events/Node/AbstractNodeEvent.php index 1b290578ab9..cc27bdd8cfd 100644 --- a/lib/public/Files/Events/Node/AbstractNodeEvent.php +++ b/lib/public/Files/Events/Node/AbstractNodeEvent.php @@ -9,12 +9,13 @@ declare(strict_types=1); namespace OCP\Files\Events\Node; use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\JsonSerializer; use OCP\Files\Node; /** * @since 20.0.0 */ -abstract class AbstractNodeEvent extends Event { +abstract class AbstractNodeEvent extends Event implements \JsonSerializable { /** * @since 20.0.0 */ @@ -29,4 +30,14 @@ abstract class AbstractNodeEvent extends Event { public function getNode(): Node { return $this->node; } + + /** + * @since 30.0.0 + */ + public function jsonSerialize(): array { + return [ + 'class' => static::class, + 'node' => JsonSerializer::serializeFileInfo($this->node), + ]; + } } diff --git a/lib/public/Files/Events/Node/AbstractNodesEvent.php b/lib/public/Files/Events/Node/AbstractNodesEvent.php index a5b058f18f5..b5fd1b75898 100644 --- a/lib/public/Files/Events/Node/AbstractNodesEvent.php +++ b/lib/public/Files/Events/Node/AbstractNodesEvent.php @@ -9,6 +9,7 @@ declare(strict_types=1); namespace OCP\Files\Events\Node; use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\JsonSerializer; use OCP\Files\Node; /** @@ -37,4 +38,15 @@ abstract class AbstractNodesEvent extends Event { public function getTarget(): Node { return $this->target; } + + /** + * @since 30.0.0 + */ + public function jsonSerialize(): array { + return [ + 'class' => static::class, + 'source' => JsonSerializer::serializeFileInfo($this->source), + 'target' => JsonSerializer::serializeFileInfo($this->target), + ]; + } } |