Skip to content

Commit

Permalink
Remove StructuredField interface
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Nov 19, 2024
1 parent 329a28d commit 8452f71
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 85 deletions.
10 changes: 5 additions & 5 deletions src/Dictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* @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
19 changes: 8 additions & 11 deletions src/InnerList.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* @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
2 changes: 1 addition & 1 deletion src/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* @phpstan-import-type SfItemPair from StructuredFieldProvider
* @phpstan-import-type SfTypeInput from StructuredFieldProvider
*/
final class Item implements StructuredField
final class Item
{
use ParameterAccess;

Expand Down
18 changes: 9 additions & 9 deletions src/OuterList.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* @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
12 changes: 6 additions & 6 deletions src/ParameterAccess.php
Original file line number Diff line number Diff line change
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
14 changes: 7 additions & 7 deletions src/Parameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* @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
43 changes: 0 additions & 43 deletions src/StructuredField.php

This file was deleted.

1 change: 1 addition & 0 deletions src/StructuredFieldProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use DateTimeInterface;

/**
* @phpstan-type StructuredField OuterList|Dictionary|InnerList|Parameters|Item
* @phpstan-type SfType ByteSequence|Token|DisplayString|DateTimeImmutable|string|int|float|bool
* @phpstan-type SfTypeInput SfType|DateTimeInterface
* @phpstan-type SfItemInput Item|SfTypeInput|StructuredFieldProvider|StructuredField
Expand Down
4 changes: 2 additions & 2 deletions tests/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private function __construct(
public readonly array $canonical,
public readonly bool $mustFail,
public readonly bool $canFail,
public readonly ?StructuredField $expected
public readonly OuterList|Dictionary|InnerList|Item|Parameters|null $expected
) {
}

Expand All @@ -53,7 +53,7 @@ public static function fromDecoded(array $data): self
);
}

private static function parseExpected(string $dataTypeValue, array $expected): ?StructuredField
private static function parseExpected(string $dataTypeValue, array $expected): OuterList|Dictionary|InnerList|Item|Parameters|null
{
return match (DataType::tryFrom($dataTypeValue)) {
DataType::Dictionary => self::parseDictionary($expected),
Expand Down

0 comments on commit 8452f71

Please sign in to comment.