Skip to content

Commit

Permalink
Adding consistent named constructor and toRfc* method
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Oct 19, 2024
1 parent c2fdb5f commit 88b91b4
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 15 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ parameters:
ignoreErrors:
- message: '#it_fails_to_create_an_item_from_an_array_of_pairs\(\)#'
path: tests/ItemTest.php
- message: '#Method Bakame\\Http\\StructuredFields\\DataType::(serialize|build|create)\(\) has parameter \$data with no value type specified in iterable type iterable.#'
- message: '#Method Bakame\\Http\\StructuredFields\\DataType::(serialize|build|create|toRfc9651|toRfc8941)\(\) has parameter \$data with no value type specified in iterable type iterable.#'
path: src/DataType.php
excludePaths:
- tests/Record.php
Expand Down
40 changes: 36 additions & 4 deletions src/DataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,39 @@ enum DataType: string
/**
* @throws StructuredFieldError
*/
public function parse(Stringable|string $httpValue, Ietf $rfc = Ietf::Rfc9651): StructuredField
public function fromRfc9651(Stringable|string $httpValue): StructuredField
{
return $this->parse($httpValue, Ietf::Rfc9651);
}

/**
* @throws StructuredFieldError
*/
public function fromRfc8941(Stringable|string $httpValue): StructuredField
{
return $this->parse($httpValue, Ietf::Rfc8941);
}

/**
* @throws StructuredFieldError
*/
public function toRfc9651(iterable $data): string
{
return $this->serialize($data, Ietf::Rfc9651);
}

/**
* @throws StructuredFieldError
*/
public function toRfc8941(iterable $data): string
{
return $this->serialize($data, Ietf::Rfc8941);
}

/**
* @throws StructuredFieldError
*/
public function parse(Stringable|string $httpValue, ?Ietf $rfc = null): StructuredField
{
$parser = new Parser($rfc);

Expand All @@ -33,15 +65,15 @@ public function parse(Stringable|string $httpValue, Ietf $rfc = Ietf::Rfc9651):
/**
* @throws StructuredFieldError
*/
public function serialize(iterable $data, Ietf $rfc = Ietf::Rfc9651): string
public function serialize(iterable $data, ?Ietf $rfc = null): string
{
return $this->create($data)->toHttpValue($rfc);
}

/**
* @throws StructuredFieldError
*/
public function create(iterable $data, Ietf $rfc = Ietf::Rfc9651): StructuredField
public function create(iterable $data): StructuredField
{
return match ($this) {
self::List => OuterList::fromPairs($data),
Expand All @@ -60,7 +92,7 @@ public function create(iterable $data, Ietf $rfc = Ietf::Rfc9651): StructuredFie
*
* @see DataType::serialize()
*/
public function build(iterable $data, Ietf $rfc = Ietf::Rfc9651): string
public function build(iterable $data, ?Ietf $rfc = null): string
{
return $this->serialize($data, $rfc);
}
Expand Down
23 changes: 22 additions & 1 deletion src/Dictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,19 @@ public static function fromHttpValue(Stringable|string $httpValue, DictionaryPar
return new self(array_map($converter, $parser->parseDictionary($httpValue)));
}

public function toHttpValue(Ietf $rfc = Ietf::Rfc9651): string
public static function fromRfc9651(Stringable|string $httpValue): self
{
return self::fromHttpValue($httpValue, new Parser(Ietf::Rfc9651));
}

public static function fromRfc8941(Stringable|string $httpValue): self
{
return self::fromHttpValue($httpValue, new Parser(Ietf::Rfc8941));
}

public function toHttpValue(?Ietf $rfc = null): string
{
$rfc ??= Ietf::Rfc9651;
$members = [];
foreach ($this->members as $key => $member) {
$members[] = match (true) {
Expand All @@ -160,6 +171,16 @@ public function toHttpValue(Ietf $rfc = Ietf::Rfc9651): string
return implode(', ', $members);
}

public function toRfc9651(): string
{
return $this->toHttpValue(Ietf::Rfc9651);
}

public function toRfc8941(): string
{
return $this->toHttpValue(Ietf::Rfc8941);
}

public function __toString(): string
{
return $this->toHttpValue();
Expand Down
2 changes: 1 addition & 1 deletion src/Ietf.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function supports(Type $type): bool
};
}

public function published(): string
public function publishedAt(): string
{
return match ($this) {
self::Rfc9651 => '2024-09',
Expand Down
24 changes: 23 additions & 1 deletion src/InnerList.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,33 @@ public static function new(
return new self($members, Parameters::new());
}

public function toHttpValue(Ietf $rfc = Ietf::Rfc9651): string
public static function fromRfc9651(Stringable|string $httpValue): self
{
return self::fromHttpValue($httpValue, new Parser(Ietf::Rfc9651));
}

public static function fromRfc8941(Stringable|string $httpValue): self
{
return self::fromHttpValue($httpValue, new Parser(Ietf::Rfc8941));
}

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);
}

public function toRfc9651(): string
{
return $this->toHttpValue(Ietf::Rfc9651);
}

public function toRfc8941(): string
{
return $this->toHttpValue(Ietf::Rfc8941);
}

public function __toString(): string
{
return $this->toHttpValue();
Expand Down
25 changes: 24 additions & 1 deletion src/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ private function __construct(
) {
}

public static function fromRfc9651(Stringable|string $httpValue): self
{
return self::fromHttpValue($httpValue, new Parser(Ietf::Rfc9651));
}

public static function fromRfc8941(Stringable|string $httpValue): self
{
return self::fromHttpValue($httpValue, new Parser(Ietf::Rfc8941));
}

/**
* Returns a new instance from an HTTP Header or Trailer value string
* in compliance with RFC8941.
Expand Down Expand Up @@ -278,11 +288,24 @@ public function parameterByIndex(int $index): array
*
* @see https://www.rfc-editor.org/rfc/rfc9651.html#section-4.1
*/
public function toHttpValue(Ietf $rfc = Ietf::Rfc9651): string
public function toHttpValue(?Ietf $rfc = null): string
{
$rfc ??= Ietf::Rfc9651;


return $this->value->serialize($rfc).$this->parameters->toHttpValue($rfc);
}

public function toRfc9651(): string
{
return $this->toHttpValue(Ietf::Rfc9651);
}

public function toRfc8941(): string
{
return $this->toHttpValue(Ietf::Rfc8941);
}

public function __toString(): string
{
return $this->toHttpValue();
Expand Down
24 changes: 23 additions & 1 deletion src/OuterList.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,33 @@ public static function new(iterable|StructuredField|Token|ByteSequence|DisplaySt
return new self(...$members);
}

public function toHttpValue(Ietf $rfc = Ietf::Rfc9651): string
public static function fromRfc9651(Stringable|string $httpValue): self
{
return self::fromHttpValue($httpValue, new Parser(Ietf::Rfc9651));
}

public static function fromRfc8941(Stringable|string $httpValue): self
{
return self::fromHttpValue($httpValue, new Parser(Ietf::Rfc8941));
}

public function toHttpValue(?Ietf $rfc = null): string
{
$rfc ??= Ietf::Rfc9651;

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

public function toRfc9651(): string
{
return $this->toHttpValue(Ietf::Rfc9651);
}

public function toRfc8941(): string
{
return $this->toHttpValue(Ietf::Rfc8941);
}

public function __toString(): string
{
return $this->toHttpValue();
Expand Down
23 changes: 22 additions & 1 deletion src/Parameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,19 @@ public static function fromHttpValue(Stringable|string $httpValue, ParametersPar
return new self($parser->parseParameters($httpValue));
}

public function toHttpValue(Ietf $rfc = Ietf::Rfc9651): string
public static function fromRfc9651(Stringable|string $httpValue): self
{
return self::fromHttpValue($httpValue, new Parser(Ietf::Rfc9651));
}

public static function fromRfc8941(Stringable|string $httpValue): self
{
return self::fromHttpValue($httpValue, new Parser(Ietf::Rfc8941));
}

public function toHttpValue(?Ietf $rfc = null): string
{
$rfc ??= Ietf::Rfc9651;
$formatter = static fn (ValueAccess $member, string $offset): string => match (true) {
true === $member->value() => ';'.$offset,
default => ';'.$offset.'='.$member->toHttpValue($rfc),
Expand All @@ -121,6 +132,16 @@ public function toHttpValue(Ietf $rfc = Ietf::Rfc9651): string
return implode('', array_map($formatter, $this->members, array_keys($this->members)));
}

public function toRfc9651(): string
{
return $this->toHttpValue(Ietf::Rfc9651);
}

public function toRfc8941(): string
{
return $this->toHttpValue(Ietf::Rfc8941);
}

public function __toString(): string
{
return $this->toHttpValue();
Expand Down
2 changes: 1 addition & 1 deletion src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ final class Parser implements DictionaryParser, InnerListParser, ItemParser, Lis

private Ietf $rfc;

public static function new(Ietf $rfc = Ietf::Rfc9651): self
public static function new(?Ietf $rfc = null): self
{
return new self($rfc);
}
Expand Down
2 changes: 1 addition & 1 deletion src/StructuredField.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ interface StructuredField extends Stringable
/**
* Returns the serialize-representation of the Structured Field as a textual HTTP field value.
*/
public function toHttpValue(Ietf $rfc = Ietf::Rfc9651): string;
public function toHttpValue(?Ietf $rfc = null): string;
}
4 changes: 2 additions & 2 deletions tests/DataTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function it_will_fail_to_generate_rfc8941_structured_field_text_represena
{
$this->expectExceptionObject(new SyntaxError('The date type is not serializable by RFC8941'));

DataType::Dictionary->serialize([['a', false], ['b', Item::fromDateString('+30 minutes')]], Ietf::Rfc8941);
DataType::Dictionary->toRfc8941([['a', false], ['b', Item::fromDateString('+30 minutes')]]);
}

#[Test]
Expand All @@ -108,6 +108,6 @@ public function it_will_fail_to_parse_rfc9651_structured_field_text_represenatio

$string = DataType::Dictionary->serialize([['a', false], ['b', Item::fromDateString('+30 minutes')]]);

DataType::Dictionary->parse($string, Ietf::Rfc8941);
DataType::Dictionary->fromRfc8941($string);
}
}

0 comments on commit 88b91b4

Please sign in to comment.