Skip to content

Commit

Permalink
refactor(options): Improve options handling in Traits
Browse files Browse the repository at this point in the history
  - Refactor the `HasOptions` trait to improve options handling
  - Update the `setOptions` method to iterate over the options and call `setOption` for each option
  - Update the `setOption` method to directly set the option value in the `$options` array
  - Update the `getOption` method to directly retrieve the option value from the `$options` array
  - Add a new method `resolveOptions` to configure and resolve the options using an `OptionsResolver`
  - Update the `configureAndResolveOptions` method to accept a callable `configurator` for configuring the `OptionsResolver`
  - Update the `configureOptionsResolver` and `preConfigureOptionsResolver` methods to allow custom configuration of the `OptionsResolver`
  - Remove unused magic methods from the trait
  • Loading branch information
guanguans committed Feb 6, 2024
1 parent 1d614f7 commit 38faad9
Show file tree
Hide file tree
Showing 14 changed files with 153 additions and 188 deletions.
6 changes: 6 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,12 @@
// ],
'phpdoc_to_comment' => false,
'phpdoc_param_order' => true,
'phpdoc_no_alias_tag' => [
'replacements' => [
'type' => 'var',
'link' => 'see',
],
],

// return_notation
'simplified_null_return' => true,
Expand Down
13 changes: 4 additions & 9 deletions src/Chanify/Messages/TextMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,11 @@ class TextMessage extends Message
// 'priority' => 10,
// ];

protected function configureOptionsResolver(OptionsResolver $optionsResolver): OptionsResolver
protected function configureOptionsResolver(OptionsResolver $optionsResolver): void
{
return tap(
parent::configureOptionsResolver($optionsResolver),
static function (OptionsResolver $optionsResolver): void {
$optionsResolver->setNormalizer(
'actions',
static fn (OptionsResolver $optionsResolver, $value): array => (array) $value
);
}
$optionsResolver->setNormalizer(
'actions',
static fn (OptionsResolver $optionsResolver, $value): array => (array) $value
);
}
}
3 changes: 2 additions & 1 deletion src/Foundation/Concerns/AsBody.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace Guanguans\Notify\Foundation\Concerns;

use GuzzleHttp\Psr7\Utils;
use GuzzleHttp\RequestOptions;

/**
Expand All @@ -22,7 +23,7 @@ trait AsBody
public function toHttpOptions(): array
{
return [
RequestOptions::BODY => $this->getOptions(),
RequestOptions::BODY => Utils::streamFor($this->resolveOptions()),
];
}
}
2 changes: 1 addition & 1 deletion src/Foundation/Concerns/AsFormParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ trait AsFormParams
public function toHttpOptions(): array
{
return [
RequestOptions::FORM_PARAMS => $this->getOptions(),
RequestOptions::FORM_PARAMS => $this->resolveOptions(),
];
}
}
2 changes: 1 addition & 1 deletion src/Foundation/Concerns/AsJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ trait AsJson
public function toHttpOptions(): array
{
return [
RequestOptions::JSON => $this->getOptions(),
RequestOptions::JSON => $this->resolveOptions(),
];
}
}
2 changes: 1 addition & 1 deletion src/Foundation/Concerns/AsMultipart.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ trait AsMultipart
public function toHttpOptions(): array
{
return [
RequestOptions::MULTIPART => to_multipart($this->getOptions()),
RequestOptions::MULTIPART => to_multipart($this->resolveOptions()),
];
}
}
2 changes: 1 addition & 1 deletion src/Foundation/Concerns/AsQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ trait AsQuery
public function toHttpOptions(): array
{
return [
RequestOptions::QUERY => $this->getOptions(),
RequestOptions::QUERY => $this->resolveOptions(),
];
}
}
21 changes: 0 additions & 21 deletions src/Foundation/Traits/ConfigureOptionsable.php

This file was deleted.

121 changes: 65 additions & 56 deletions src/Foundation/Traits/HasOptions.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

/** @noinspection MissingReturnTypeInspection */
/** @noinspection MissingParameterTypeDeclarationInspection */

declare(strict_types=1);

/**
Expand All @@ -16,40 +19,27 @@
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* @property array $defined;
* @property array $required;
* @property array $deprecated;
* @property array $defaults;
* @property bool $prototype;
* @property array $allowedValues;
* @property array $allowedTypes;
* @property array $normalizers;
* @property array $infos;
* @property-read array<string, mixed> $defaults
* @property-read array<string> $required
* @property-read array<string> $defined
* @property-read array<array-key, array|string> $deprecated
* @property-read array<string, \Closure> $normalizers
* @property-read array<string, mixed> $allowedValues
* @property-read array<string, array<string>|string> $allowedTypes;
* @property-read array<string, string> $infos
* @property-read bool $prototype
*/
trait HasOptions
{
protected array $options = [];

public function __get($option)
{
return $this->offsetGet($option);
}

public function __set($option, $value): void
{
$this->offsetSet($option, $value);
}

public function __isset($option)
{
return $this->offsetExists($option);
}

public function __unset($option): void
{
$this->offsetUnset($option);
}

/**
* @noinspection MissingParameterTypeDeclarationInspection
* @noinspection MissingReturnTypeInspection
*
* @param mixed $name
* @param mixed $arguments
*/
public function __call($name, $arguments)
{
$defined = $this->defined ?? [];
Expand All @@ -59,7 +49,11 @@ public function __call($name, $arguments)

if (\in_array($casedName, $defined, true)) {
if (empty($arguments)) {
throw new \InvalidArgumentException(sprintf('Method %s::%s requires an argument', static::class, $name));
throw new \InvalidArgumentException(sprintf(
'Method %s::%s requires an argument',
static::class,
$name
));
}

return $this->setOption($casedName, $arguments[0]);
Expand All @@ -71,25 +65,35 @@ public function __call($name, $arguments)

public function setOptions(array $options): self
{
$this->options = array_replace_recursive($this->options, $options);
foreach ($options as $option => $value) {
$this->setOption($option, $value);
}

return $this;
}

public function setOption(string $option, $value): self
{
return $this->setOptions([$option => $value]);
$this->options[$option] = $value;

return $this;
}

public function getOption(string $option, $default = null)
{
return $this->getOptions()[$option] ?? $default;
return $this->options[$option] ?? $default;
}

public function getOptions(): array
{
return $this->options = $this->configureAndResolveOptions($this->options, function (OptionsResolver $optionsResolver): void {
$this->configureOptionsResolver($this->preConfigureOptionsResolver($optionsResolver));
return $this->options;
}

public function resolveOptions(): array
{
return $this->configureAndResolveOptions($this->options, function (OptionsResolver $optionsResolver): void {
$this->preConfigureOptionsResolver($optionsResolver);
$this->configureOptionsResolver($optionsResolver);
});
}

Expand All @@ -113,13 +117,7 @@ public function offsetUnset($offset): void
unset($this->options[$offset]);
}

protected function configureOptionsResolver(OptionsResolver $optionsResolver): OptionsResolver
{
// configure options resolver...
return $optionsResolver;
}

private function configureAndResolveOptions(array $options, callable $configurator): array
protected function configureAndResolveOptions(array $options, callable $configurator): array
{
$optionsResolver = new OptionsResolver;

Expand All @@ -128,17 +126,34 @@ private function configureAndResolveOptions(array $options, callable $configurat
return $optionsResolver->resolve($options);
}

private function preConfigureOptionsResolver(OptionsResolver $optionsResolver): OptionsResolver
protected function configureOptionsResolver(OptionsResolver $optionsResolver): void
{
// configure options resolver...
}

private function preConfigureOptionsResolver(OptionsResolver $optionsResolver): void
{
property_exists($this, 'defined') and $optionsResolver->setDefined($this->defined);
property_exists($this, 'required') and $optionsResolver->setRequired($this->required);
property_exists($this, 'defaults') and $optionsResolver->setDefaults($this->defaults);
property_exists($this, 'prototype') and $optionsResolver->setPrototype($this->prototype);
property_exists($this, 'required') and $optionsResolver->setRequired($this->required);
property_exists($this, 'defined') and $optionsResolver->setDefined($this->defined);

if (property_exists($this, 'deprecated')) {
foreach ($this->deprecated as $option => $deprecated) {
array_unshift($deprecated, $option);
$optionsResolver->setDeprecated(...$deprecated);
foreach ($this->deprecated as $option => $arguments) {
if (\is_string($arguments)) {
$arguments = [$arguments];
}

if (\is_string($option)) {
array_unshift($arguments, $option);
}

$optionsResolver->setDeprecated(...$arguments);
}
}

if (property_exists($this, 'normalizers')) {
foreach ($this->normalizers as $option => $normalizer) {
$optionsResolver->setNormalizer($option, $normalizer);
}
}

Expand All @@ -154,18 +169,12 @@ private function preConfigureOptionsResolver(OptionsResolver $optionsResolver):
}
}

if (property_exists($this, 'normalizers')) {
foreach ($this->normalizers as $option => $normalizer) {
$optionsResolver->setNormalizer($option, $normalizer);
}
}

if (property_exists($this, 'infos')) {
foreach ($this->infos as $option => $info) {
$optionsResolver->setInfo($option, $info);
}
}

return $optionsResolver;
property_exists($this, 'prototype') and $optionsResolver->setPrototype($this->prototype);
}
}
25 changes: 10 additions & 15 deletions src/MicrosoftTeams/Messages/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,21 +143,16 @@ public function toHttpUri(): string
return '';
}

protected function configureOptionsResolver(OptionsResolver $optionsResolver): OptionsResolver
protected function configureOptionsResolver(OptionsResolver $optionsResolver): void
{
return tap(
parent::configureOptionsResolver($optionsResolver),
static function (OptionsResolver $resolver): void {
$resolver->setNormalizer('sections', static fn (
OptionsResolver $optionsResolver,
array $value
): array => isset($value[0]) ? $value : [$value]);

$resolver->setNormalizer('potentialAction', static fn (
OptionsResolver $optionsResolver,
array $value
): array => isset($value[0]) ? $value : [$value]);
}
);
$optionsResolver->setNormalizer('sections', static fn (
OptionsResolver $optionsResolver,
array $value
): array => isset($value[0]) ? $value : [$value]);

$optionsResolver->setNormalizer('potentialAction', static fn (
OptionsResolver $optionsResolver,
array $value
): array => isset($value[0]) ? $value : [$value]);
}
}
Loading

0 comments on commit 38faad9

Please sign in to comment.