Skip to content

Commit

Permalink
feat(Message): Add new methods to handle form parameters, query strin…
Browse files Browse the repository at this point in the history
…gs, and multipart data

- Added `toFormParams` method to handle form parameters
- Added `toQuery` method to handle query strings
- Added `toMultipart` method to handle multipart data
- Updated `toJson` method to use GuzzleHttp\Utils::jsonEncode
  • Loading branch information
guanguans committed Jun 14, 2024
1 parent 48c4510 commit e7c2a7c
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/Foundation/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use Guanguans\Notify\Foundation\Concerns\Dumpable;
use Guanguans\Notify\Foundation\Concerns\HasOptions;
use Guanguans\Notify\Foundation\Support\Arr;
use Guanguans\Notify\Foundation\Support\Utils;
use GuzzleHttp\Psr7\MultipartStream;

/**
* @template-implements \ArrayAccess<string, mixed>
Expand Down Expand Up @@ -72,9 +74,30 @@ final public static function make($options = []): self
return new static($options);
}

protected function toJson(int $options = \JSON_THROW_ON_ERROR | \JSON_UNESCAPED_UNICODE): string
protected function toFormParams(
string $numericPrefix = '',
?string $argSeparator = '&',
int $encodingType = \PHP_QUERY_RFC1738
): string {
return $this->toQuery($numericPrefix, $argSeparator, $encodingType);
}

protected function toQuery(
string $numericPrefix = '',
?string $argSeparator = '&',
int $encodingType = \PHP_QUERY_RFC3986
): string {
return http_build_query($this->toPayload(), $numericPrefix, $argSeparator, $encodingType);
}

protected function toMultipart(int $options = MULTIPART_TRY_OPEN_FILE): MultipartStream
{
return new MultipartStream(Utils::multipartFor($this->toPayload(), $options));
}

protected function toJson(int $options = \JSON_THROW_ON_ERROR | \JSON_UNESCAPED_UNICODE, int $depth = 512): string
{
return json_encode($this->toPayload(), $options);
return \GuzzleHttp\Utils::jsonEncode($this->toPayload(), $options, $depth);
}

protected function toPayload(): array
Expand Down

0 comments on commit e7c2a7c

Please sign in to comment.