From b7723789aaf62944fcb399cd70ebc1e45932cae8 Mon Sep 17 00:00:00 2001 From: yaozm Date: Fri, 2 Feb 2024 16:07:27 +0800 Subject: [PATCH] wip Pushover --- src/Foundation/Concerns/AsMultipart.php | 2 +- src/Foundation/Support/helpers.php | 39 +++++++++ src/Pushover/Credential.php | 21 ++--- src/Pushover/Messages/Message.php | 102 ++++++++++++------------ 4 files changed, 97 insertions(+), 67 deletions(-) diff --git a/src/Foundation/Concerns/AsMultipart.php b/src/Foundation/Concerns/AsMultipart.php index e33c5eb3..651739fb 100644 --- a/src/Foundation/Concerns/AsMultipart.php +++ b/src/Foundation/Concerns/AsMultipart.php @@ -23,7 +23,7 @@ trait AsMultipart public function toHttpOptions(): array { return [ - RequestOptions::MULTIPART => $this->getOptions(), + RequestOptions::MULTIPART => to_multipart($this->getOptions()), ]; } } diff --git a/src/Foundation/Support/helpers.php b/src/Foundation/Support/helpers.php index d4e90554..d3e529f0 100644 --- a/src/Foundation/Support/helpers.php +++ b/src/Foundation/Support/helpers.php @@ -10,8 +10,47 @@ * This source file is subject to the MIT license that is bundled. */ +use GuzzleHttp\Psr7\Utils; use Symfony\Component\OptionsResolver\OptionsResolver; +if (! function_exists('to_multipart')) { + /** + * @noinspection SlowArrayOperationsInLoopInspection + */ + function to_multipart(array $form): array + { + $normalizeMultipartField = function (string $name, $contents) use (&$normalizeMultipartField): array { + $field = []; + if (! is_array($contents)) { + if (is_string($contents) && is_file($contents)) { + $contents = Utils::tryFopen($contents, 'r'); + } + + return [compact('name', 'contents')]; + } + + foreach ($contents as $key => $value) { + $key = "{$name}[$key]"; + $field = array_merge( + $field, + is_array($value) + ? $normalizeMultipartField($key, $value) + : [['name' => $key, 'contents' => $value]] + ); + } + + return $field; + }; + + $multipart = []; + foreach ($form as $name => $contents) { + $multipart = array_merge($multipart, $normalizeMultipartField($name, $contents)); + } + + return $multipart; + } +} + if (! function_exists('configure_options')) { /** * Configuration options. diff --git a/src/Pushover/Credential.php b/src/Pushover/Credential.php index ae5473b3..3552caee 100644 --- a/src/Pushover/Credential.php +++ b/src/Pushover/Credential.php @@ -12,24 +12,13 @@ namespace Guanguans\Notify\Pushover; -use Guanguans\Notify\Foundation\Credentials\NullCredential; +use Guanguans\Notify\Foundation\Credentials\AggregateCredential; +use Guanguans\Notify\Foundation\Credentials\QueryCredential; -class Credential extends NullCredential +class Credential extends AggregateCredential { - private ?string $token; - private ?string $user; - - public function __construct(?string $token = null, ?string $user = null) + public function __construct(string $user, string $token) { - $this->token = $token; - $this->user = $user; - } - - public function applyToOptions(array $options): array - { - $options['multipart']['token'] = $this->token; - $options['multipart']['user'] = $this->user; - - return $options; + parent::__construct(new QueryCredential('user', $user), new QueryCredential('token', $token)); } } diff --git a/src/Pushover/Messages/Message.php b/src/Pushover/Messages/Message.php index be036a86..6cef416a 100644 --- a/src/Pushover/Messages/Message.php +++ b/src/Pushover/Messages/Message.php @@ -14,6 +14,7 @@ use Guanguans\Notify\Foundation\Concerns\AsMultipart; use Guanguans\Notify\Foundation\Concerns\AsPost; +use GuzzleHttp\Psr7\Utils; use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; use Symfony\Component\OptionsResolver\Exception\MissingOptionsException; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -39,9 +40,6 @@ class Message extends \Guanguans\Notify\Foundation\Message use AsMultipart; use AsPost; - /** - * @var array - */ protected array $defined = [ // 'token', // 'user', @@ -59,11 +57,11 @@ class Message extends \Guanguans\Notify\Foundation\Message 'callback', 'device', 'attachment', + + // 'attachment_base64', + // 'attachment_type', ]; - /** - * @var array - */ protected array $required = [ // 'token', // 'user', @@ -89,58 +87,62 @@ class Message extends \Guanguans\Notify\Foundation\Message 'monospace' => [0, 1], ]; - public function toHttpUri() + public function toHttpUri(): string { return 'https://api.pushover.net/1/messages.json'; } protected function configureOptionsResolver(OptionsResolver $optionsResolver): OptionsResolver { - return tap(parent::configureOptionsResolver($optionsResolver), static function (OptionsResolver $optionsResolver): void { - $optionsResolver->setNormalizer('priority', static function (OptionsResolver $optionsResolver, $value): int { - if (2 !== $value) { - return $value; - } - - if (isset($optionsResolver['retry'], $optionsResolver['expire'])) { - return $value; - } - - throw new MissingOptionsException('The required option "retry" or "expire" is missing.'); - }); - - $optionsResolver->setNormalizer('html', static function (OptionsResolver $optionsResolver, $value): int { - if (1 !== $value) { - return $value; - } - - if (! isset($optionsResolver['monospace'])) { - return $value; - } - - if (1 !== $optionsResolver['monospace']) { - return $value; - } - - throw new InvalidOptionsException('Html cannot be set with monospace, Monospace cannot be set with html, Html and monospace are mutually.'); - }); - - $optionsResolver->setNormalizer('attachment', static function (OptionsResolver $optionsResolver, $value): array { - if (\is_string($value)) { - if ('' === $value) { - throw new InvalidOptionsException('The attachment cannot be empty.'); + return tap( + parent::configureOptionsResolver($optionsResolver), + static function (OptionsResolver $optionsResolver): void { + $optionsResolver->setNormalizer( + 'priority', + static function (OptionsResolver $optionsResolver, $value): int { + if (2 !== $value) { + return $value; + } + + if (isset($optionsResolver['retry'], $optionsResolver['expire'])) { + return $value; + } + + throw new MissingOptionsException('The required option "retry" or "expire" is missing.'); } + ); - $value = fopen($value, 'r'); - if (false === $value) { - throw new InvalidOptionsException("The attachment resource file does not exist: {$value}."); - } - } + $optionsResolver->setNormalizer( + 'html', + static function (OptionsResolver $optionsResolver, $value): int { + if (1 !== $value) { + return $value; + } + + if (! isset($optionsResolver['monospace'])) { + return $value; + } - return [ - 'attachment' => $value, - ]; - }); - }); + if (1 !== $optionsResolver['monospace']) { + return $value; + } + + throw new InvalidOptionsException('Html cannot be set with monospace, Monospace cannot be set with html, Html and monospace are mutually.'); + } + ); + + // $optionsResolver->setNormalizer( + // 'attachment', + // static function (OptionsResolver $optionsResolver, $value) { + // if (\is_string($value)) { + // // $value = fopen($value, 'r'); + // $value = Utils::tryFopen($value, 'r'); + // } + // + // return $value; + // } + // ); + } + ); } }