Skip to content

Commit aae32eb

Browse files
committed
Simplify Item::toHttpValue implementation
1 parent d6b2fa4 commit aae32eb

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

src/Item.php

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public static function fromHttpValue(string $httpValue): self
103103
{
104104
$httpValue = trim($httpValue, ' ');
105105
[$value, $parameters] = match (true) {
106-
$httpValue === '',
106+
'' === $httpValue,
107107
1 === preg_match("/[\r\t\n]/", $httpValue),
108108
1 === preg_match("/[^\x20-\x7E]/", $httpValue) => throw new SyntaxError("The HTTP textual representation `$httpValue` for an item contains invalid characters."),
109109
1 === preg_match('/^(-?[0-9])/', $httpValue) => self::parseNumber($httpValue),
@@ -225,26 +225,21 @@ private static function parseString(string $string): array
225225
throw new SyntaxError("The HTTP textual representation `$originalString` for a string contains an invalid end string.");
226226
}
227227

228-
public function toHttpValue(): string
229-
{
230-
return $this->serializeValue($this->value).$this->parameters->toHttpValue();
231-
}
232-
233228
/**
234229
* Serialize the Item value according to RFC8941.
235230
*
236231
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.1
237232
*/
238-
private function serializeValue(Token|ByteSequence|int|float|string|bool $value): string
233+
public function toHttpValue(): string
239234
{
240235
return match (true) {
241-
$value instanceof Token => $value->toHttpValue(),
242-
$value instanceof ByteSequence => $value->toHttpValue(),
243-
is_string($value) => '"'.preg_replace('/(["\\\])/', '\\\$1', $value).'"',
244-
is_int($value) => (string) $value,
245-
is_float($value) => $this->serializeDecimal($value),
246-
default => '?'.($value ? '1' : '0'),
247-
};
236+
is_string($this->value) => '"'.preg_replace('/(["\\\])/', '\\\$1', $this->value).'"',
237+
is_int($this->value) => (string) $this->value,
238+
is_float($this->value) => $this->serializeDecimal($this->value),
239+
is_bool($this->value) => '?'.($this->value ? '1' : '0'),
240+
default => $this->value->toHttpValue(),
241+
}
242+
.$this->parameters->toHttpValue();
248243
}
249244

250245
/**

0 commit comments

Comments
 (0)