Skip to content

Commit 9ad0ea7

Browse files
committed
Improve internal codebase
1 parent b22377c commit 9ad0ea7

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

src/Item.php

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,27 @@ private function __construct(
2929
) {
3030
}
3131

32+
/**
33+
* Returns a new instance from an HTTP Header or Trailer value string
34+
* in compliance with RFC8941.
35+
*
36+
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-3.3
37+
*/
38+
public static function fromHttpValue(Stringable|string $httpValue): self
39+
{
40+
$itemString = trim((string) $httpValue, ' ');
41+
if ('' === $itemString || 1 === preg_match("/[\r\t\n]|[^\x20-\x7E]/", $itemString)) {
42+
throw new SyntaxError('The HTTP textual representation "'.$httpValue.'" for an item contains invalid characters.');
43+
}
44+
45+
[$value, $offset] = Parser::parseBareItem($itemString);
46+
if (!str_contains($itemString, ';') && $offset !== strlen($itemString)) {
47+
throw new SyntaxError('The HTTP textual representation "'.$httpValue.'" for an item contains invalid characters.');
48+
}
49+
50+
return new self(new Value($value), Parameters::fromHttpValue(substr($itemString, $offset)));
51+
}
52+
3253
/**
3354
* Returns a new instance from a value type and an iterable of key-value parameters.
3455
*
@@ -69,24 +90,11 @@ public static function fromPair(array $pair): self
6990
}
7091

7192
/**
72-
* Returns a new instance from an HTTP Header or Trailer value string
73-
* in compliance with RFC8941.
74-
*
75-
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-3.3
93+
* Returns a new bare instance from value.
7694
*/
77-
public static function fromHttpValue(Stringable|string $httpValue): self
95+
private static function fromValue(Value $value): self
7896
{
79-
$itemString = trim((string) $httpValue, ' ');
80-
if ('' === $itemString || 1 === preg_match("/[\r\t\n]|[^\x20-\x7E]/", $itemString)) {
81-
throw new SyntaxError('The HTTP textual representation "'.$httpValue.'" for an item contains invalid characters.');
82-
}
83-
84-
[$value, $offset] = Parser::parseBareItem($itemString);
85-
if (!str_contains($itemString, ';') && $offset !== strlen($itemString)) {
86-
throw new SyntaxError('The HTTP textual representation "'.$httpValue.'" for an item contains invalid characters.');
87-
}
88-
89-
return new self(new Value($value), Parameters::fromHttpValue(substr($itemString, $offset)));
97+
return new self($value, Parameters::create());
9098
}
9199

92100
/**
@@ -97,14 +105,6 @@ public static function fromEncodedByteSequence(Stringable|string $value): self
97105
return self::fromValue(Value::fromEncodedByteSequence($value));
98106
}
99107

100-
/**
101-
* Returns a new bare instance from value.
102-
*/
103-
private static function fromValue(Value $value): self
104-
{
105-
return new self($value, Parameters::create());
106-
}
107-
108108
/**
109109
* Returns a new instance from a decoded byte sequence and an iterable of key-value parameters.
110110
*/
@@ -219,11 +219,6 @@ public function parameter(string $key): mixed
219219
}
220220
}
221221

222-
public function __toString(): string
223-
{
224-
return $this->toHttpValue();
225-
}
226-
227222
/**
228223
* Serialize the Item value according to RFC8941.
229224
*
@@ -234,6 +229,11 @@ public function toHttpValue(): string
234229
return $this->value->serialize().$this->parameters->toHttpValue();
235230
}
236231

232+
public function __toString(): string
233+
{
234+
return $this->toHttpValue();
235+
}
236+
237237
/**
238238
* @return array{0:SfItemInput, 1:MemberOrderedMap<string, SfItem>}
239239
*/

0 commit comments

Comments
 (0)