diff --git a/CHANGELOG.md b/CHANGELOG.md index a76fa1a..b00f3e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ All Notable changes to `bakame/http-strucured-fields` will be documented in this file. -## [Next] - TBD +## [1.1.0] - 2023-05-07 ### Added @@ -332,7 +332,8 @@ All Notable changes to `bakame/http-strucured-fields` will be documented in this **Initial release!** -[Next]: https://github.com/bakame-php/http-structured-fields/compare/1.0.1...master +[Next]: https://github.com/bakame-php/http-structured-fields/compare/1.1.0...master +[1.1.0]: https://github.com/bakame-php/http-structured-fields/compare/1.0.1...1.1.0 [1.0.1]: https://github.com/bakame-php/http-structured-fields/compare/1.0.0...1.0.1 [1.0.0]: https://github.com/bakame-php/http-structured-fields/compare/0.8.0...1.0.0 [0.8.0]: https://github.com/bakame-php/http-structured-fields/compare/0.7.0...0.8.0 diff --git a/README.md b/README.md index 1e9ec16..c4b92f8 100644 --- a/README.md +++ b/README.md @@ -13,10 +13,7 @@ build and update HTTP Structured Fields in PHP according to the [RFC8941](https: Once installed you will be able to do the following: ```php -use Bakame\Http\StructuredFields\InnerList; -use Bakame\Http\StructuredFields\Item; -use Bakame\Http\StructuredFields\OuterList; -use Bakame\Http\StructuredFields\Token; +use Bakame\Http\StructuredFields\{InnerList, Item, OuterList, Token}; //1 - parsing an Accept Header $headerValue = 'text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8'; diff --git a/src/InnerList.php b/src/InnerList.php index 7601126..a9bf7a2 100644 --- a/src/InnerList.php +++ b/src/InnerList.php @@ -37,7 +37,7 @@ final class InnerList implements MemberList, ParameterAccess */ private function __construct(iterable $members, private readonly Parameters $parameters) { - $this->members = array_map(self::filterMember(...), array_values([...$members])); + $this->members = array_map($this->filterMember(...), array_values([...$members])); } /** @@ -45,7 +45,7 @@ private function __construct(iterable $members, private readonly Parameters $par * * @return SfItem */ - private static function filterMember(mixed $member): object + private function filterMember(mixed $member): object { return match (true) { $member instanceof ValueAccess && $member instanceof ParameterAccess => $member, @@ -285,7 +285,7 @@ public function remove(string|int ...$keys): static return match (true) { [] === $indices => $this, - $max === count($indices) => self::new(), + count($indices) === $max => self::new(), default => new self(array_filter( $this->members, fn (int $key): bool => !in_array($key, $indices, true), diff --git a/src/OuterList.php b/src/OuterList.php index 6a23c3d..2cb640a 100644 --- a/src/OuterList.php +++ b/src/OuterList.php @@ -37,7 +37,7 @@ final class OuterList implements MemberList */ private function __construct(iterable|StructuredField|Token|ByteSequence|DateTimeInterface|string|int|float|bool ...$members) { - $this->members = array_map(self::filterMember(...), array_values([...$members])); + $this->members = array_map($this->filterMember(...), array_values([...$members])); } /** @@ -45,7 +45,7 @@ private function __construct(iterable|StructuredField|Token|ByteSequence|DateTim * * @return SfMember */ - private static function filterMember(mixed $member): object + private function filterMember(mixed $member): object { return match (true) { $member instanceof ParameterAccess && ($member instanceof MemberList || $member instanceof ValueAccess) => $member, diff --git a/src/Parser.php b/src/Parser.php index 20d859e..5c9eeab 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -46,14 +46,14 @@ final class Parser implements DictionaryParser, InnerListParser, ItemParser, Lis public function parseValue(Stringable|string $httpValue): ByteSequence|Token|DateTimeImmutable|string|int|float|bool { - $valueString = trim((string) $httpValue, ' '); - if ('' === $valueString || 1 === preg_match(self::REGEXP_INVALID_CHARACTERS, $valueString)) { - throw new SyntaxError('The HTTP textual representation "'.$httpValue.'" for an item value contains invalid characters.'); + $remainder = trim((string) $httpValue, ' '); + if ('' === $remainder || 1 === preg_match(self::REGEXP_INVALID_CHARACTERS, $remainder)) { + throw new SyntaxError("The HTTP textual representation \"$httpValue\" for an item value contains invalid characters."); } - [$value, $offset] = self::extractValue($valueString); - if ('' !== substr($valueString, $offset)) { - throw new SyntaxError('The HTTP textual representation "'.$httpValue.'" for an item value contains invalid characters.'); + [$value, $offset] = self::extractValue($remainder); + if ('' !== substr($remainder, $offset)) { + throw new SyntaxError("The HTTP textual representation \"$httpValue\" for an item value contains invalid characters."); } return $value; @@ -64,15 +64,15 @@ public function parseValue(Stringable|string $httpValue): ByteSequence|Token|Dat */ public function parseItem(Stringable|string $httpValue): array { - $itemString = trim((string) $httpValue, ' '); - if ('' === $itemString || 1 === preg_match(self::REGEXP_INVALID_CHARACTERS, $itemString)) { - throw new SyntaxError('The HTTP textual representation "'.$httpValue.'" for an item contains invalid characters.'); + $remainder = trim((string) $httpValue, ' '); + if ('' === $remainder || 1 === preg_match(self::REGEXP_INVALID_CHARACTERS, $remainder)) { + throw new SyntaxError("The HTTP textual representation \"$httpValue\" for an item contains invalid characters."); } - [$value, $offset] = self::extractValue($itemString); - $remainder = substr($itemString, $offset); + [$value, $offset] = self::extractValue($remainder); + $remainder = substr($remainder, $offset); if ('' !== $remainder && !str_contains($remainder, ';')) { - throw new SyntaxError('The HTTP textual representation "'.$httpValue.'" for an item contains invalid characters.'); + throw new SyntaxError("The HTTP textual representation \"$httpValue\" for an item contains invalid characters."); } return [$value, $this->parseParameters($remainder)]; @@ -89,10 +89,10 @@ public function parseItem(Stringable|string $httpValue): array */ public function parseParameters(Stringable|string $httpValue): array { - $parameterString = trim((string) $httpValue); - [$parameters, $offset] = self::extractParametersValues($parameterString); - if (strlen($parameterString) !== $offset) { - throw new SyntaxError('The HTTP textual representation "'.$httpValue.'" for Parameters contains invalid characters.'); + $remainder = trim((string) $httpValue); + [$parameters, $offset] = self::extractParametersValues($remainder); + if (strlen($remainder) !== $offset) { + throw new SyntaxError("The HTTP textual representation \"$httpValue\" for Parameters contains invalid characters."); } return $parameters; @@ -170,23 +170,23 @@ public function parseInnerList(Stringable|string $httpValue): array * * @see https://tools.ietf.org/html/rfc7230#section-3.2.3 */ - private static function removeCommaSeparatedWhiteSpaces(string $httpValue, int $offset): string + private static function removeCommaSeparatedWhiteSpaces(string $remainder, int $offset): string { - $httpValue = self::removeOptionalWhiteSpaces(substr($httpValue, $offset)); - if ('' === $httpValue) { - return $httpValue; + $remainder = self::removeOptionalWhiteSpaces(substr($remainder, $offset)); + if ('' === $remainder) { + return ''; } - if (1 !== preg_match(self::REGEXP_VALID_SPACE, $httpValue, $found)) { + if (1 !== preg_match(self::REGEXP_VALID_SPACE, $remainder, $found)) { throw new SyntaxError('The HTTP textual representation is missing an excepted comma.'); } - $httpValue = substr($httpValue, strlen($found['space'])); - if ('' === $httpValue) { + $remainder = substr($remainder, strlen($found['space'])); + if ('' === $remainder) { throw new SyntaxError('The HTTP textual representation has an unexpected end of line.'); } - return $httpValue; + return $remainder; } /** @@ -242,11 +242,11 @@ private static function extractInnerList(string $httpValue): array [$list[], $remainder] = self::extractItem($remainder); if ('' !== $remainder && !in_array($remainder[0], [' ', ')'], true)) { - throw new SyntaxError("The HTTP textual representation \"$remainder\" for a inner list is using invalid characters."); + throw new SyntaxError("The HTTP textual representation \"$httpValue\" for a inner list is using invalid characters."); } } - throw new SyntaxError("The HTTP textual representation \"$remainder\" for a inner list has an unexpected end of line."); + throw new SyntaxError("The HTTP textual representation \"$httpValue\" for a inner list has an unexpected end of line."); } /** @@ -372,12 +372,11 @@ private static function extractDate(string $httpValue): array private static function extractString(string $httpValue): array { $offset = 1; - $originalHttpValue = $httpValue; - $httpValue = substr($httpValue, $offset); + $remainder = substr($httpValue, $offset); $output = ''; - while ('' !== $httpValue) { - $char = $httpValue[0]; + while ('' !== $remainder) { + $char = $remainder[0]; $offset += 1; if ('"' === $char) { @@ -385,28 +384,28 @@ private static function extractString(string $httpValue): array } if (1 === preg_match(self::REGEXP_INVALID_CHARACTERS, $char)) { - throw new SyntaxError("The HTTP textual representation \"$originalHttpValue\" for a String contains an invalid end string."); + throw new SyntaxError("The HTTP textual representation \"$httpValue\" for a String contains an invalid end string."); } - $httpValue = substr($httpValue, 1); + $remainder = substr($remainder, 1); if ('\\' !== $char) { $output .= $char; continue; } - $char = $httpValue[0] ?? ''; + $char = $remainder[0] ?? ''; $offset += 1; - $httpValue = substr($httpValue, 1); + $remainder = substr($remainder, 1); if (!in_array($char, ['"', '\\'], true)) { - throw new SyntaxError("The HTTP textual representation \"$originalHttpValue\" for a String contains an invalid end string."); + throw new SyntaxError("The HTTP textual representation \"$httpValue\" for a String contains an invalid end string."); } $output .= $char; } - throw new SyntaxError("The HTTP textual representation \"$originalHttpValue\" for a String contains an invalid end string."); + throw new SyntaxError("The HTTP textual representation \"$httpValue\" for a String contains an invalid end string."); } /**