Skip to content

Commit

Permalink
Improve Field DX
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Nov 9, 2024
1 parent 27c4fef commit ce4dbf8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 33 deletions.
35 changes: 7 additions & 28 deletions src/Forward.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<array{0:string, 1:SfType}>
*/
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<string, SfType>
*/
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());
}
}
10 changes: 5 additions & 5 deletions src/HandledRequestCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
);
}
Expand Down

0 comments on commit ce4dbf8

Please sign in to comment.