Skip to content

Commit c912c1d

Browse files
committed
Improve Type class codebase
1 parent e37dab0 commit c912c1d

File tree

3 files changed

+28
-30
lines changed

3 files changed

+28
-30
lines changed

src/ByteSequence.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Bakame\Http\StructuredFields;
66

77
use Stringable;
8-
98
use Throwable;
109

1110
use function base64_decode;
@@ -25,25 +24,25 @@ private function __construct(
2524
/**
2625
* Returns a new instance from a Base64 encoded string.
2726
*/
28-
public static function fromEncoded(Stringable|string $encodedValue): self
27+
public static function fromEncoded(Stringable|string $encoded): self
2928
{
30-
$encodedValue = (string) $encodedValue;
31-
if (1 !== preg_match('/^[a-z\d+\/=]*$/i', $encodedValue)) {
32-
throw new SyntaxError('Invalid character in byte sequence.');
29+
$encoded = (string) $encoded;
30+
if (1 !== preg_match('/^[a-z\d+\/=]*$/i', $encoded)) {
31+
throw new SyntaxError('The byte sequence '.$encoded.' contains invalid characters.');
3332
}
3433

35-
$decoded = base64_decode($encodedValue, true);
34+
$decoded = base64_decode($encoded, true);
3635
if (false === $decoded) {
37-
throw new SyntaxError('Unable to base64 decode the byte sequence.');
36+
throw new SyntaxError('Unable to base64 decode the byte sequence '.$encoded);
3837
}
3938

4039
return new self($decoded);
4140
}
4241

43-
public static function tryFromEncoded(Stringable|string $encodedValue): ?self
42+
public static function tryFromEncoded(Stringable|string $encoded): ?self
4443
{
4544
try {
46-
return self::fromEncoded($encodedValue);
45+
return self::fromEncoded($encoded);
4746
} catch (Throwable) {
4847
return null;
4948
}
@@ -52,9 +51,9 @@ public static function tryFromEncoded(Stringable|string $encodedValue): ?self
5251
/**
5352
* Returns a new instance from a raw decoded string.
5453
*/
55-
public static function fromDecoded(Stringable|string $value): self
54+
public static function fromDecoded(Stringable|string $decoded): self
5655
{
57-
return new self((string) $value);
56+
return new self((string) $decoded);
5857
}
5958

6059
/**

src/DisplayString.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ private function __construct(
2222
) {
2323
}
2424

25-
public static function tryFromEncoded(Stringable|string $encodedValue): ?self
25+
public static function tryFromEncoded(Stringable|string $encoded): ?self
2626
{
2727
try {
28-
return self::fromEncoded($encodedValue);
28+
return self::fromEncoded($encoded);
2929
} catch (Throwable) {
3030
return null;
3131
}
@@ -34,41 +34,41 @@ public static function tryFromEncoded(Stringable|string $encodedValue): ?self
3434
/**
3535
* Returns a new instance from a Base64 encoded string.
3636
*/
37-
public static function fromEncoded(Stringable|string $encodedValue): self
37+
public static function fromEncoded(Stringable|string $encoded): self
3838
{
39-
$value = (string) $encodedValue;
39+
$encoded = (string) $encoded;
4040

41-
if (1 === preg_match('/[^\x20-\x7E]/i', $value)) {
42-
throw new SyntaxError('The string contains invalid characters.'.$value);
41+
if (1 === preg_match('/[^\x20-\x7E]/i', $encoded)) {
42+
throw new SyntaxError('The display string '.$encoded.' contains invalid characters.');
4343
}
4444

45-
if (!str_contains($value, '%')) {
46-
return new self($value);
45+
if (!str_contains($encoded, '%')) {
46+
return new self($encoded);
4747
}
4848

49-
if (1 === preg_match('/%(?![0-9a-f]{2})/', $value)) {
50-
throw new SyntaxError('The string '.$value.' contains invalid utf-8 encoded sequence.');
49+
if (1 === preg_match('/%(?![0-9a-f]{2})/', $encoded)) {
50+
throw new SyntaxError('The display string '.$encoded.' contains invalid utf-8 encoded sequence.');
5151
}
5252

53-
$value = (string) preg_replace_callback(
53+
$decoded = (string) preg_replace_callback(
5454
',%[a-f0-9]{2},',
5555
fn (array $matches): string => rawurldecode($matches[0]),
56-
$value
56+
$encoded
5757
);
5858

59-
if (1 !== preg_match('//u', $value)) {
60-
throw new SyntaxError('The string contains invalid characters.'.$value);
59+
if (1 !== preg_match('//u', $decoded)) {
60+
throw new SyntaxError('The display string '.$encoded.' contains invalid characters.');
6161
}
6262

63-
return new self($value);
63+
return new self($decoded);
6464
}
6565

6666
/**
6767
* Returns a new instance from a raw decoded string.
6868
*/
69-
public static function fromDecoded(Stringable|string $value): self
69+
public static function fromDecoded(Stringable|string $decoded): self
7070
{
71-
return new self((string) $value);
71+
return new self((string) $decoded);
7272
}
7373

7474
/**

src/Token.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Bakame\Http\StructuredFields;
66

77
use Stringable;
8-
98
use Throwable;
109

1110
use function preg_match;
@@ -18,7 +17,7 @@ final class Token
1817
private function __construct(private readonly string $value)
1918
{
2019
if (1 !== preg_match("/^([a-z*][a-z\d:\/!#\$%&'*+\-.^_`|~]*)$/i", $this->value)) {
21-
throw new SyntaxError('Invalid characters in token.');
20+
throw new SyntaxError('The token '.$this->value.' contains invalid characters.');
2221
}
2322
}
2423

0 commit comments

Comments
 (0)