basis-company / nats.php

nats jetstream client for php
116 stars 29 forks source link

class Payload cannot handle mixed types in publish despite publish function accepting mixed payload #81

Open IvanWillsBI opened 2 months ago

IvanWillsBI commented 2 months ago

public function publish(string $name, mixed $payload, ?string $replyTo = null): self
{
        return $this->send(new Publish([
            'payload' => Payload::parse($payload),
            'replyTo' => $replyTo,
            'subject' => $name,
        ]));
}

public static function parse(mixed $data): self
{
        if ($data instanceof self) {
            return $data;
        }
        if (is_string($data)) {
            return new self($data);
        }
        if (is_array($data)) {
            return new self(json_encode($data));
        }

        return new self("");
}```

Causes nil bodies to be published to NATS if data type is not string or array
IvanWillsBI commented 2 months ago
public static function parse(mixed $data): self
    {
        if ($data instanceof self) {
            return $data;
        }
        if (is_string($data)) {
            return new self($data);
        }
        if (is_array($data)) {
            return new self(json_encode($data));
        }
        if (is_resource($data)) {
            return new self('');
        }

        return new self(json_encode($data));
    }
IvanWillsBI commented 2 months ago
public static function parse(mixed $data): self
    {
        if ($data instanceof self) {
            return $data;
        }
        if (is_string($data)) {
            return new self($data);
        }
        if (is_array($data)) {
            return new self(json_encode($data));
        }
        if (is_resource($data)) {
            return new self('');
        }

        return new self(json_encode($data));
    }

Is this a viable fix, I would raise a PR but encountering a permissions issue