Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d0d188f

Browse files
committed
feature #30484 [Mime] added Headers::toArray() (fabpot)
This PR was merged into the 4.3-dev branch. Discussion ---------- [Mime] added Headers::toArray() | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | n/a | License | MIT | Doc PR | n/a That helps when using the Mime component to create a form submission HTTP body (where you need to merge the headers of the MIME body with the HTTP headers): ```php $body->getPreparedHeaders()->toArray(); vs foreach ($body->getPreparedHeaders()->getAll() as $header) { $headers[] = $header->toString(); } ``` Commits ------- 788bb81 [Mime] added Headers::toArray()
2 parents ed4a74a + 788bb81 commit d0d188f

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/Symfony/Component/Mime/Header/Headers.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,23 @@ public static function isUniqueHeader(string $name): bool
210210
public function toString(): string
211211
{
212212
$string = '';
213+
foreach ($this->toArray() as $str) {
214+
$string .= $str."\r\n";
215+
}
216+
217+
return $string;
218+
}
219+
220+
public function toArray(): array
221+
{
222+
$arr = [];
213223
foreach ($this->getAll() as $header) {
214224
if ('' !== $header->getBodyAsString()) {
215-
$string .= $header->toString()."\r\n";
225+
$arr[] = $header->toString();
216226
}
217227
}
218228

219-
return $string;
229+
return $arr;
220230
}
221231

222232
/**

src/Symfony/Component/Mime/Tests/Header/HeadersTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,4 +231,15 @@ public function testHeadersWithoutBodiesAreNotDisplayed()
231231
$headers->addTextHeader('Zip', '');
232232
$this->assertEquals("Foo: bar\r\n", $headers->toString());
233233
}
234+
235+
public function testToArray()
236+
{
237+
$headers = new Headers();
238+
$headers->addIdHeader('Message-ID', 'some@id');
239+
$headers->addTextHeader('Foo', str_repeat('a', 60).pack('C', 0x8F));
240+
$this->assertEquals([
241+
'Message-ID: <some@id>',
242+
"Foo: =?utf-8?Q?aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa?=\r\n =?utf-8?Q?aaaa?=",
243+
], $headers->toArray());
244+
}
234245
}

0 commit comments

Comments
 (0)