Skip to content
Permalink
Browse files

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()
  • Loading branch information...
fabpot committed Mar 8, 2019
2 parents ed4a74a + 788bb81 commit d0d188f0b91ab241ca8b28e06e64b766301bed94
@@ -210,13 +210,23 @@ public static function isUniqueHeader(string $name): bool
public function toString(): string
{
$string = '';
foreach ($this->toArray() as $str) {
$string .= $str."\r\n";
}
return $string;
}
public function toArray(): array
{
$arr = [];
foreach ($this->getAll() as $header) {
if ('' !== $header->getBodyAsString()) {
$string .= $header->toString()."\r\n";
$arr[] = $header->toString();
}
}
return $string;
return $arr;
}
/**
@@ -231,4 +231,15 @@ public function testHeadersWithoutBodiesAreNotDisplayed()
$headers->addTextHeader('Zip', '');
$this->assertEquals("Foo: bar\r\n", $headers->toString());
}
public function testToArray()
{
$headers = new Headers();
$headers->addIdHeader('Message-ID', 'some@id');
$headers->addTextHeader('Foo', str_repeat('a', 60).pack('C', 0x8F));
$this->assertEquals([
'Message-ID: <some@id>',
"Foo: =?utf-8?Q?aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa?=\r\n =?utf-8?Q?aaaa?=",
], $headers->toArray());
}
}

0 comments on commit d0d188f

Please sign in to comment.
You can’t perform that action at this time.