From ce4dbf8a6c90619da4c70a7e511dcbaab6869efb Mon Sep 17 00:00:00 2001 From: Ignace Nyamagana Butera Date: Sat, 9 Nov 2024 12:20:20 +0100 Subject: [PATCH] Improve Field DX --- src/Forward.php | 35 +++++++---------------------------- src/HandledRequestCache.php | 10 +++++----- 2 files changed, 12 insertions(+), 33 deletions(-) diff --git a/src/Forward.php b/src/Forward.php index 08fb771..cab28eb 100644 --- a/src/Forward.php +++ b/src/Forward.php @@ -99,34 +99,13 @@ public function __toString(): string return $this->toStructuredField()->toHttpValue(); } - public function toStructuredField(): StructuredField + public function toStructuredField(): Parameters { - return Parameters::fromPairs($this->toPairs()); - } - - /** - * @return array - */ - public function toPairs(): array - { - return array_filter([ - [Properties::Forward->value, $this->reason->toToken()], - [Properties::ForwardStatusCode->value, $this->statusCode], - [Properties::Stored->value, $this->stored], - [Properties::Collapsed->value, $this->collapsed], - ], fn (array $pair): bool => !in_array($pair[1], [null, false], true)); - } - - /** - * @return array - */ - public function toAssociative(): array - { - return array_filter([ - Properties::Forward->value => $this->reason->toToken(), - Properties::ForwardStatusCode->value => $this->statusCode, - Properties::Stored->value => $this->stored, - Properties::Collapsed->value => $this->collapsed, - ], fn (mixed $value): bool => !in_array($value, [null, false], true)); + return Parameters::new() + ->append(Properties::Forward->value, $this->reason->toToken()) + ->append(Properties::ForwardStatusCode->value, $this->statusCode) + ->append(Properties::Stored->value, $this->stored) + ->append(Properties::Collapsed->value, $this->collapsed) + ->filter(fn (array $pair): bool => false !== $pair[1]->value()); } } diff --git a/src/HandledRequestCache.php b/src/HandledRequestCache.php index 9227678..4297887 100644 --- a/src/HandledRequestCache.php +++ b/src/HandledRequestCache.php @@ -155,11 +155,11 @@ public function toStructuredField(): StructuredField return Item::new($this->servedBy) ->withParameters( Parameters::new() - ->add(Properties::Hit->value, false === $this->hit ? null : $this->hit) - ->mergePairs($this->forward?->toPairs() ?? []) - ->add(Properties::TimeToLive->value, $this->ttl) - ->add(Properties::Key->value, $this->key) - ->add(Properties::Detail->value, $this->detail) + ->append(Properties::Hit->value, false === $this->hit ? null : $this->hit) + ->mergePairs($this->forward?->toStructuredField() ?? []) + ->append(Properties::TimeToLive->value, $this->ttl) + ->append(Properties::Key->value, $this->key) + ->append(Properties::Detail->value, $this->detail) ->filter(fn (array $pair): bool => false !== $pair[1]->value()), ); }