Skip to content

Commit

Permalink
Remove structured field interface (#19)
Browse files Browse the repository at this point in the history
Remove StructuredField interface
  • Loading branch information
nyamsprod authored Nov 19, 2024
1 parent 9a6f75d commit 56da217
Show file tree
Hide file tree
Showing 16 changed files with 82 additions and 112 deletions.
12 changes: 6 additions & 6 deletions src/Dictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
/**
* @see https://www.rfc-editor.org/rfc/rfc9651.html#section-3.2
*
* @phpstan-import-type SfMemberInput from StructuredField
* @phpstan-import-type SfMemberInput from StructuredFieldProvider
*
* @implements ArrayAccess<string, InnerList|Item>
* @implements IteratorAggregate<int, array{0:string, 1:InnerList|Item}>
*/
final class Dictionary implements ArrayAccess, Countable, IteratorAggregate, StructuredField
final class Dictionary implements ArrayAccess, Countable, IteratorAggregate
{
/** @var array<string, InnerList|Item> */
private readonly array $members;
Expand Down Expand Up @@ -60,7 +60,7 @@ private static function filterMember(mixed $member): InnerList|Item

return match (true) {
$member instanceof InnerList || $member instanceof Item => $member,
$member instanceof StructuredField => throw new InvalidArgument('An instance of "'.$member::class.'" can not be a member of "'.self::class.'".'),
$member instanceof OuterList || $member instanceof Dictionary || $member instanceof Parameters => throw new InvalidArgument('An instance of "'.$member::class.'" can not be a member of "'.self::class.'".'),
is_iterable($member) => InnerList::new(...$member),
default => Item::new($member),
};
Expand Down Expand Up @@ -489,7 +489,7 @@ public function last(): array
*/
public function add(
string $name,
iterable|StructuredFieldProvider|StructuredField|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool|null $member
iterable|StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool|null $member
): self {
if (null === $member) {
return $this;
Expand Down Expand Up @@ -579,7 +579,7 @@ public function removeByNames(string ...$names): self
*/
public function append(
string $name,
iterable|StructuredFieldProvider|StructuredField|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool|null $member
iterable|StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool|null $member
): self {
if (null === $member) {
return $this;
Expand All @@ -602,7 +602,7 @@ public function append(
*/
public function prepend(
string $name,
iterable|StructuredFieldProvider|StructuredField|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool|null $member
iterable|StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool|null $member
): self {
if (null === $member) {
return $this;
Expand Down
7 changes: 6 additions & 1 deletion src/Ietf.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ public function supports(mixed $value): bool
$value = $value->toStructuredField();
}

if ($value instanceof StructuredField) {
if ($value instanceof OuterList ||
$value instanceof InnerList ||
$value instanceof Dictionary ||
$value instanceof Parameters ||
$value instanceof Item
) {
try {
$value->toHttpValue($this);

Expand Down
27 changes: 12 additions & 15 deletions src/InnerList.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@
/**
* @see https://www.rfc-editor.org/rfc/rfc9651.html#section-3.1.1
*
* @phpstan-import-type SfType from StructuredField
* @phpstan-import-type SfItemInput from StructuredField
* @phpstan-import-type SfInnerListPair from StructuredField
* @phpstan-import-type SfParameterInput from StructuredField
* @phpstan-import-type SfType from StructuredFieldProvider
* @phpstan-import-type SfItemInput from StructuredFieldProvider
* @phpstan-import-type SfInnerListPair from StructuredFieldProvider
* @phpstan-import-type SfParameterInput from StructuredFieldProvider
*
* @implements ArrayAccess<int, Item>
* @implements IteratorAggregate<int, Item>
*/
final class InnerList implements ArrayAccess, Countable, IteratorAggregate, StructuredField
final class InnerList implements ArrayAccess, Countable, IteratorAggregate
{
use ParameterAccess;

Expand Down Expand Up @@ -113,7 +113,7 @@ public static function fromPair(array $pair): self
* Returns a new instance.
*/
public static function new(
StructuredFieldProvider|StructuredField|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool ...$members
StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool ...$members
): self {
return new self($members, Parameters::new());
}
Expand All @@ -132,7 +132,7 @@ public function toHttpValue(?Ietf $rfc = null): string
{
$rfc ??= Ietf::Rfc9651;

return '('.implode(' ', array_map(fn (StructuredField $value): string => $value->toHttpValue($rfc), $this->members)).')'.$this->parameters->toHttpValue($rfc);
return '('.implode(' ', array_map(fn (Item $value): string => $value->toHttpValue($rfc), $this->members)).')'.$this->parameters->toHttpValue($rfc);
}

public function toRfc9651(): string
Expand Down Expand Up @@ -244,10 +244,7 @@ public function first(): ?Item
return $this->members[0] ?? null;
}

/**
* @return ?Item
*/
public function last(): ?StructuredField
public function last(): ?Item
{
return $this->members[$this->filterIndex(-1)] ?? null;
}
Expand All @@ -261,7 +258,7 @@ public function withParameters(Parameters $parameters): static
* Inserts members at the beginning of the list.
*/
public function unshift(
StructuredFieldProvider|StructuredField|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool ...$members
StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool ...$members
): self {
$membersToAdd = array_reduce(
$members,
Expand All @@ -285,7 +282,7 @@ function (array $carry, $member) {
* Inserts members at the end of the list.
*/
public function push(
StructuredFieldProvider|StructuredField|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool ...$members
StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool ...$members
): self {
$membersToAdd = array_reduce(
$members,
Expand All @@ -312,7 +309,7 @@ function (array $carry, $member) {
*/
public function insert(
int $index,
StructuredFieldProvider|StructuredField|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool ...$members
StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool ...$members
): self {
$offset = $this->filterIndex($index) ?? throw InvalidOffset::dueToIndexNotFound($index);

Expand All @@ -330,7 +327,7 @@ public function insert(

public function replace(
int $index,
StructuredFieldProvider|StructuredField|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool $member
StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool $member
): self {
$offset = $this->filterIndex($index) ?? throw InvalidOffset::dueToIndexNotFound($index);
$member = self::filterMember($member);
Expand Down
10 changes: 5 additions & 5 deletions src/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
/**
* @see https://www.rfc-editor.org/rfc/rfc9651.html#section-3.3
*
* @phpstan-import-type SfType from StructuredField
* @phpstan-import-type SfItemInput from StructuredField
* @phpstan-import-type SfItemPair from StructuredField
* @phpstan-import-type SfTypeInput from StructuredField
* @phpstan-import-type SfType from StructuredFieldProvider
* @phpstan-import-type SfItemInput from StructuredFieldProvider
* @phpstan-import-type SfItemPair from StructuredFieldProvider
* @phpstan-import-type SfTypeInput from StructuredFieldProvider
*/
final class Item implements StructuredField
final class Item
{
use ParameterAccess;

Expand Down
24 changes: 12 additions & 12 deletions src/OuterList.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
/**
* @see https://www.rfc-editor.org/rfc/rfc9651.html#name-lists
*
* @phpstan-import-type SfMemberInput from StructuredField
* @phpstan-import-type SfInnerListPair from StructuredField
* @phpstan-import-type SfItemPair from StructuredField
* @phpstan-import-type SfMemberInput from StructuredFieldProvider
* @phpstan-import-type SfInnerListPair from StructuredFieldProvider
* @phpstan-import-type SfItemPair from StructuredFieldProvider
*
* @implements ArrayAccess<int, InnerList|Item>
* @implements IteratorAggregate<int, InnerList|Item>
*/
final class OuterList implements ArrayAccess, Countable, IteratorAggregate, StructuredField
final class OuterList implements ArrayAccess, Countable, IteratorAggregate
{
/** @var list<InnerList|Item> */
private readonly array $members;
Expand All @@ -45,7 +45,7 @@ final class OuterList implements ArrayAccess, Countable, IteratorAggregate, Stru
* @param InnerList|Item|SfMemberInput ...$members
*/
private function __construct(
iterable|StructuredFieldProvider|StructuredField|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool ...$members
iterable|StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool ...$members
) {
$this->members = array_map($this->filterMember(...), array_values([...$members]));
}
Expand All @@ -61,7 +61,7 @@ private function filterMember(mixed $member): InnerList|Item

return match (true) {
$member instanceof InnerList || $member instanceof Item => $member,
$member instanceof StructuredField => throw new InvalidArgument('An instance of "'.$member::class.'" can not be a member of "'.self::class.'".'),
$member instanceof OuterList || $member instanceof Parameters || $member instanceof Dictionary => throw new InvalidArgument('An instance of "'.$member::class.'" can not be a member of "'.self::class.'".'),
is_iterable($member) => InnerList::new(...$member),
default => Item::new($member),
};
Expand Down Expand Up @@ -142,7 +142,7 @@ public static function fromPairs(StructuredFieldProvider|iterable $pairs): self
/**
* @param InnerList|Item|SfMemberInput ...$members
*/
public static function new(iterable|StructuredFieldProvider|StructuredField|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool ...$members): self
public static function new(iterable|StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool ...$members): self
{
return new self(...$members);
}
Expand All @@ -161,7 +161,7 @@ public function toHttpValue(?Ietf $rfc = null): string
{
$rfc ??= Ietf::Rfc9651;

return implode(', ', array_map(fn (StructuredField $member): string => $member->toHttpValue($rfc), $this->members));
return implode(', ', array_map(fn (Item|InnerList $member): string => $member->toHttpValue($rfc), $this->members));
}

public function toRfc9651(): string
Expand Down Expand Up @@ -276,7 +276,7 @@ public function last(): InnerList|Item|null
* @param StructuredFieldProvider|InnerList|Item|SfMemberInput ...$members
*/
public function unshift(
StructuredFieldProvider|StructuredField|iterable|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool ...$members
StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|iterable|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool ...$members
): self {
$membersToAdd = array_reduce(
$members,
Expand All @@ -302,7 +302,7 @@ function (array $carry, $member) {
* @param InnerList|Item|SfMemberInput ...$members
*/
public function push(
iterable|StructuredFieldProvider|StructuredField|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool ...$members
iterable|StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool ...$members
): self {
$membersToAdd = array_reduce(
$members,
Expand Down Expand Up @@ -331,7 +331,7 @@ function (array $carry, $member) {
*/
public function insert(
int $index,
iterable|StructuredFieldProvider|StructuredField|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool ...$members
iterable|StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool ...$members
): self {
$offset = $this->filterIndex($index) ?? throw InvalidOffset::dueToIndexNotFound($index);

Expand All @@ -352,7 +352,7 @@ public function insert(
*/
public function replace(
int $index,
iterable|StructuredFieldProvider|StructuredField|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool $member
iterable|StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool $member
): self {
$offset = $this->filterIndex($index) ?? throw InvalidOffset::dueToIndexNotFound($index);
$member = self::filterMember($member);
Expand Down
16 changes: 8 additions & 8 deletions src/ParameterAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
* Common manipulation methods used when interacting with an object
* with a Parameters instance attached to it.
*
* @phpstan-import-type SfType from StructuredField
* @phpstan-import-type SfItemInput from StructuredField
* @phpstan-import-type SfType from StructuredFieldProvider
* @phpstan-import-type SfItemInput from StructuredFieldProvider
*/
trait ParameterAccess
{
Expand Down Expand Up @@ -76,13 +76,13 @@ abstract public function withParameters(Parameters $parameters): static;
* This method MUST retain the state of the current instance, and return
* an instance that contains the specified parameter change.
*
* @param StructuredFieldProvider|StructuredField|SfType|null $member
* @param StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|SfType|null $member
*
* @throws SyntaxError If the string key is not a valid
*/
public function addParameter(
string $name,
StructuredFieldProvider|StructuredField|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool|null $member
StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool|null $member
): static {
return $this->withParameters($this->parameters()->add($name, $member));
}
Expand All @@ -93,13 +93,13 @@ public function addParameter(
* This method MUST retain the state of the current instance, and return
* an instance that contains the specified parameter change.
*
* @param StructuredFieldProvider|StructuredField|SfType|null $member
* @param StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|SfType|null $member
*
* @throws SyntaxError If the string key is not a valid
*/
public function prependParameter(
string $name,
StructuredFieldProvider|StructuredField|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool|null $member
StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool|null $member
): static {
return $this->withParameters($this->parameters()->prepend($name, $member));
}
Expand All @@ -110,13 +110,13 @@ public function prependParameter(
* This method MUST retain the state of the current instance, and return
* an instance that contains the specified parameter change.
*
* @param StructuredFieldProvider|StructuredField|SfType|null $member
* @param StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|SfType|null $member
*
* @throws SyntaxError If the string key is not a valid
*/
public function appendParameter(
string $name,
StructuredFieldProvider|StructuredField|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool|null $member
StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool|null $member
): static {
return $this->withParameters($this->parameters()->append($name, $member));
}
Expand Down
18 changes: 9 additions & 9 deletions src/Parameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
/**
* @see https://www.rfc-editor.org/rfc/rfc9651.html#section-3.1.2
*
* @phpstan-import-type SfItemInput from StructuredField
* @phpstan-import-type SfType from StructuredField
* @phpstan-import-type SfItemInput from StructuredFieldProvider
* @phpstan-import-type SfType from StructuredFieldProvider
*
* @implements ArrayAccess<string, InnerList|Item>
* @implements IteratorAggregate<int, array{0:string, 1:Item}>
*/
final class Parameters implements ArrayAccess, Countable, IteratorAggregate, StructuredField
final class Parameters implements ArrayAccess, Countable, IteratorAggregate
{
/** @var array<string, Item> */
private readonly array $members;
Expand Down Expand Up @@ -471,11 +471,11 @@ public function valueByIndex(int $index, ?callable $validate = null, bool|string
}

/**
* @param StructuredFieldProvider|StructuredField|SfType|null $member
* @param StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|SfType|null $member
*/
public function add(
string $name,
StructuredFieldProvider|StructuredField|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool|null $member
StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool|null $member
): self {
if (null === $member) {
return $this;
Expand Down Expand Up @@ -537,11 +537,11 @@ public function removeByKeys(string ...$names): self
}

/**
* @param StructuredFieldProvider|StructuredField|SfType|null $member
* @param StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|SfType|null $member
*/
public function append(
string $name,
StructuredFieldProvider|StructuredField|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool|null $member
StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool|null $member
): self {
if (null === $member) {
return $this;
Expand All @@ -554,11 +554,11 @@ public function append(
}

/**
* @param StructuredFieldProvider|StructuredField|SfType|null $member
* @param StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|SfType|null $member
*/
public function prepend(
string $name,
StructuredFieldProvider|StructuredField|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool|null $member
StructuredFieldProvider|OuterList|Dictionary|InnerList|Parameters|Item|Token|ByteSequence|DisplayString|DateTimeInterface|string|int|float|bool|null $member
): self {
if (null === $member) {
return $this;
Expand Down
2 changes: 1 addition & 1 deletion src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* @internal Do not use directly this class as it's behaviour and return type
* MAY change significantly even during a major release cycle.
*
* @phpstan-import-type SfType from StructuredField
* @phpstan-import-type SfType from StructuredFieldProvider
*/
final class Parser
{
Expand Down
Loading

0 comments on commit 56da217

Please sign in to comment.