diff --git a/src/lib/PubSub/CloudEvent.php b/src/lib/PubSub/CloudEvent.php index 60b6533..d02da71 100644 --- a/src/lib/PubSub/CloudEvent.php +++ b/src/lib/PubSub/CloudEvent.php @@ -8,12 +8,15 @@ use Exception; use InvalidArgumentException; use JetBrains\PhpStorm\ArrayShape; +use JetBrains\PhpStorm\Deprecated; use LogicException; /** * Class CloudEvent * @package Dapr\PubSub + * @deprecated 1.3.0 Replaced by cloudevents/sdk-php */ +#[Deprecated(since: '1.3.0', replacement: \CloudEvents\V1\CloudEvent::class)] class CloudEvent implements IDeserialize { /** diff --git a/src/lib/PubSub/Topic.php b/src/lib/PubSub/Topic.php index eb325b3..84f7bf6 100644 --- a/src/lib/PubSub/Topic.php +++ b/src/lib/PubSub/Topic.php @@ -7,6 +7,8 @@ use Dapr\exceptions\DaprException; use JetBrains\PhpStorm\Deprecated; use Psr\Log\LoggerInterface; +use CloudEvents\V1\CloudEventInterface; +use CloudEvents\Serializers\JsonSerializer; /** * Class Topic @@ -25,7 +27,7 @@ public function __construct( /** * Publish an event to the topic * - * @param CloudEvent|mixed $event The event to publish + * @param CloudEventInterface|CloudEvent|mixed $event The event to publish * @param array|null $metadata Additional metadata to pass to the component * @param string $content_type The header to include in the publish request. Ignored when $event is a CloudEvent * @@ -38,13 +40,17 @@ public function publish(mixed $event, ?array $metadata = null, string $content_t } elseif ($this->client instanceof NewClient) { $this->client->logger->debug('Sending {event} to {topic}', ['event' => $event, 'topic' => $this->topic]); } - if ($event instanceof CloudEvent) { + if ($event instanceof CloudEvent || $event instanceof CloudEventInterface) { $content_type = 'application/cloudevents+json'; $this->client->extra_headers = [ 'Content-Type: application/cloudevents+json', ]; - $event = $event->to_array(); + if ($event instanceof CloudEvent) { + $event = $event->to_array(); + } elseif ($event instanceof CloudEventInterface) { + $event = json_decode(JsonSerializer::create()->serializeStructured($event), true); + } } if ($this->client instanceof DaprClient) {