diff --git a/src/Forward.php b/src/Forward.php index bff2161..ec44b4a 100644 --- a/src/Forward.php +++ b/src/Forward.php @@ -84,10 +84,9 @@ public function toStructuredField(): Parameters { 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()); + ->when(null !== $this->statusCode, fn (Parameters $parameters) => $parameters->append(Properties::ForwardStatusCode->value, $this->statusCode)) /* @phpstan-ignore-line */ + ->when(true === $this->stored, fn (Parameters $parameters) => $parameters->append(Properties::Stored->value, $this->stored)) + ->when(true === $this->collapsed, fn (Parameters $parameters) => $parameters->append(Properties::Collapsed->value, $this->collapsed)); } public function equals(mixed $other): bool diff --git a/src/HandledRequestCache.php b/src/HandledRequestCache.php index 0e527c9..096ea35 100644 --- a/src/HandledRequestCache.php +++ b/src/HandledRequestCache.php @@ -136,15 +136,11 @@ private static function validator(): ItemValidator public function toStructuredField(): Item { return Item::new($this->servedBy) - ->withParameters( - Parameters::new() - ->append(Properties::Hit->value, $this->hit) - ->mergePairs($this->forward ?? []) - ->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()), - ); + ->when(true === $this->hit, fn (Item $item) => $item->appendParameter(Properties::Hit->value, $this->hit)) + ->when(null !== $this->forward, fn (Item $item) => $item->mergeParametersByPairs($this->forward)) /* @phpstan-ignore-line */ + ->when(null !== $this->ttl, fn (Item $item) => $item->appendParameter(Properties::TimeToLive->value, $this->ttl)) /* @phpstan-ignore-line */ + ->when(null !== $this->key, fn (Item $item) => $item->appendParameter(Properties::Key->value, $this->key)) /* @phpstan-ignore-line */ + ->when(null !== $this->detail, fn (Item $item) => $item->appendParameter(Properties::Detail->value, $this->detail)); /* @phpstan-ignore-line */ } /**