Skip to content

Commit ef72809

Browse files
committed
Adding ::equals method to ByteSequence and Token class
1 parent 066dc98 commit ef72809

File tree

6 files changed

+18
-10
lines changed

6 files changed

+18
-10
lines changed

.gitattributes

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
* text=auto
22

3-
/docs export-ignore
43
/.github export-ignore
4+
/docs export-ignore
55
/.editorconfig export-ignore
66
/.gitattributes export-ignore
77
/.gitignore export-ignore
88
/.php-cs-fixer.php export-ignore
9-
/phpstan.neon export-ignore
109
/CHANGELOG.md export-ignore
1110
/README.md export-ignore
11+
/phpstan.neon export-ignore
1212
/phpunit.xml export-ignore
13-
/Makefile export-ignore
1413
/**/*Test.php export-ignore
1514
/**/Test** export-ignore
16-
/rector.php export-ignore

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ All Notable changes to `bakame/http-strucured-fields` will be documented in this
1616
- `Type` Enum to list all possible Item Type supported.
1717
- `Value` Interface is introduced with `Item` being the only available implementation.
1818
- `MemberOrderedMap::add` and `MemberOrderedMap::remove` methods
19+
- `ByteSequence::equals` and `Token::equals` to easily compare type instances.
1920

2021
### Fixed
2122

src/ByteSequence.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,9 @@ public function encoded(): string
6060
{
6161
return base64_encode($this->value);
6262
}
63+
64+
public function equals(mixed $other): bool
65+
{
66+
return $other instanceof self && $other->value === $this->value;
67+
}
6368
}

src/Item.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,8 @@ public function value(): Token|ByteSequence|DateTimeImmutable|string|int|float|b
195195
public function withValue(mixed $value): static
196196
{
197197
return match (true) {
198-
$value instanceof ByteSequence && $this->value instanceof ByteSequence && $value->encoded() === $this->value->encoded(),
199-
$value instanceof Token && $this->value instanceof Token && $value->value === $this->value->value,
200-
$value instanceof DateTimeInterface && $this->value instanceof DateTimeInterface && $value == $this->value,
198+
($this->value instanceof ByteSequence || $this->value instanceof Token) && $this->value->equals($value),
199+
$this->value instanceof DateTimeInterface && $value instanceof DateTimeInterface && $value == $this->value,
201200
$value instanceof Stringable && $value->__toString() === $this->value,
202201
$value === $this->value => $this,
203202
default => self::from($value, $this->parameters),
@@ -249,7 +248,7 @@ public function toHttpValue(): string
249248
return match (true) {
250249
is_string($this->value) => '"'.preg_replace('/(["\\\])/', '\\\$1', $this->value).'"',
251250
is_int($this->value) => (string) $this->value,
252-
is_float($this->value) => $this->serializeDecimal($this->value),
251+
is_float($this->value) => self::serializeDecimal($this->value),
253252
is_bool($this->value) => '?'.($this->value ? '1' : '0'),
254253
$this->value instanceof Token => $this->value->value,
255254
$this->value instanceof DateTimeImmutable => '@'.$this->value->getTimestamp(),
@@ -262,7 +261,7 @@ public function toHttpValue(): string
262261
*
263262
* @see https://www.rfc-editor.org/rfc/rfc8941.html#section-4.1.5
264263
*/
265-
private function serializeDecimal(float $value): string
264+
private static function serializeDecimal(float $value): string
266265
{
267266
/** @var string $result */
268267
$result = json_encode(round($value, 3, PHP_ROUND_HALF_EVEN));

src/Token.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,9 @@ public static function fromString(Stringable|string $value): self
2323
{
2424
return new self((string) $value);
2525
}
26+
27+
public function equals(mixed $other): bool
28+
{
29+
return $other instanceof self && $other->value === $this->value;
30+
}
2631
}

src/Type.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static function fromValue(mixed $value): self
3030
is_bool($value) => self::Boolean,
3131
is_int($value) => self::Integer,
3232
is_float($value) => self::Decimal,
33-
default => throw new SyntaxError('Unknown or unsupported type.'),
33+
default => throw new SyntaxError('The type "'.(is_object($value) ? $value::class : gettype($value)).'" is not supported.'),
3434
};
3535
}
3636
}

0 commit comments

Comments
 (0)