Skip to content

Commit ce80d08

Browse files
committed
StructuredField extends Stringable interface
1 parent ee5d712 commit ce80d08

15 files changed

+64
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ All Notable changes to `bakame/http-strucured-fields` will be documented in this
1717
- `Value` Interface is introduced with `Item` being the only available implementation.
1818
- `MemberOrderedMap::add` and `MemberOrderedMap::remove` methods
1919
- `ByteSequence::equals` and `Token::equals` to easily compare type instances.
20+
- `StructuredField` extends the `Stringable` interface
2021

2122
### Fixed
2223

docs/basic-usage.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ use Bakame\Http\StructuredFields\OrderedList;
2020

2121
$dictionary = Dictionary::fromHttpValue("a=?0, b, c=?1; foo=bar");
2222
echo $dictionary->toHttpValue(); // 'a=?0, b, c;foo=bar'
23+
echo $dictionary; // 'a=?0, b, c;foo=bar'
2324

2425
$list = OrderedList::fromHttpValue('("foo"; a=1;b=2);lvl=5, ("bar" "baz");lvl=1');
2526
echo $list->toHttpValue(); // '("foo";a=1;b=2);lvl=5, ("bar" "baz");lvl=1'
27+
echo $list; // '("foo";a=1;b=2);lvl=5, ("bar" "baz");lvl=1'
2628

2729
$item = Item::fromHttpValue('"foo";a=1;b=2');
2830
echo $item->toHttpValue(); // "foo";a=1;b=2
31+
echo $item; // "foo";a=1;b=2
2932
```
3033

src/ByteSequenceTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,17 @@ public function it_can_encode_raw_field(): void
5050
self::assertSame('pretend this is binary content.', $item->decoded());
5151
self::assertSame($source, $item->encoded());
5252
}
53+
54+
/** @test */
55+
public function it_can_compare_instances(): void
56+
{
57+
$decoded = 'pretend this is binary content.';
58+
$source = 'cHJldGVuZCB0aGlzIGlzIGJpbmFyeSBjb250ZW50Lg==';
59+
$value1 = ByteSequence::fromDecoded($decoded);
60+
$value2 = ByteSequence::fromEncoded($source);
61+
$value3 = ByteSequence::fromDecoded($source);
62+
63+
self::assertTrue($value1->equals($value2));
64+
self::assertFalse($value1->equals($value3));
65+
}
5366
}

src/Dictionary.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ public function toHttpValue(): string
117117
return implode(', ', array_map($formatter, $this->members, array_keys($this->members)));
118118
}
119119

120+
public function __toString(): string
121+
{
122+
return $this->toHttpValue();
123+
}
124+
120125
public function count(): int
121126
{
122127
return count($this->members);

src/DictionaryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public function it_can_prepend_a_new_member(): void
122122
->append('a', Item::from(false))
123123
->prepend('b', Item::from(true));
124124

125-
self::assertSame('b, a=?0', $instance->toHttpValue());
125+
self::assertSame('b, a=?0', (string) $instance);
126126
}
127127

128128
/** @test */

src/InnerList.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ public function toHttpValue(): string
100100
return '('.implode(' ', array_map(fn (Value $value): string => $value->toHttpValue(), $this->members)).')'.$this->parameters->toHttpValue();
101101
}
102102

103+
public function __toString(): string
104+
{
105+
return $this->toHttpValue();
106+
}
107+
103108
public function count(): int
104109
{
105110
return count($this->members);

src/InnerListTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public function it_can_unshift_insert_and_replace(): void
7272
self::assertCount(3, $container);
7373
self::assertTrue($container->hasMembers());
7474
self::assertSame('(:SGVsbG8gV29ybGQ=: 42.0 42)', $container->toHttpValue());
75+
self::assertSame('(:SGVsbG8gV29ybGQ=: 42.0 42)', (string) $container);
7576
}
7677

7778
/** @test */

src/Item.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,11 @@ public function toHttpValue(): string
256256
}.$this->parameters->toHttpValue();
257257
}
258258

259+
public function __toString(): string
260+
{
261+
return $this->toHttpValue();
262+
}
263+
259264
/**
260265
* Serialize the Item decimal value according to RFC8941.
261266
*

src/ItemTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public function it_fails_to_instantiate_a_decimal_too_small(): void
4646
public function it_instantiate_a_decimal(): void
4747
{
4848
self::assertSame('42.0', Item::from(42.0)->toHttpValue());
49+
self::assertSame('42.0', (string) Item::from(42.0));
4950
}
5051

5152
/** @test */

src/OrderedList.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ public function toHttpValue(): string
7878
return implode(', ', array_map(fn (StructuredField $member): string => $member->toHttpValue(), $this->members));
7979
}
8080

81+
public function __toString(): string
82+
{
83+
return $this->toHttpValue();
84+
}
85+
8186
public function count(): int
8287
{
8388
return count($this->members);

0 commit comments

Comments
 (0)