Skip to content

Commit

Permalink
add readable string method to address
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikbosch committed Nov 23, 2017
1 parent 05dea08 commit 95aa894
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,23 @@ public function __toString(): string
return sprintf('%s <%s>', $encodedName, $this->address->getPunyCode());
}

/**
* @return string
*/
public function toReadableString(): string
{
if ($this->name === '') {
return (string)$this->address;
}

$encodedName = $this->name;
if ($encodedName !== $this->name || preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $this->name) === 1) {
$encodedName = sprintf('"%s"', $encodedName);
}

return sprintf('%s <%s>', $encodedName, $this->address->getAddress());
}

/**
* @param string $addressAsString
* @return Address
Expand Down
16 changes: 16 additions & 0 deletions test/Unit/AddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,22 @@ public function it_parses_address_strings(string $addressString, bool $construct
}
}

/**
* @test
* @dataProvider provideAddressStrings
*/
public function it_can_be_converted_to_readable_string()
{
$address = new Address(new EmailAddress('local-part@münchen.com'), 'sprode, henriëtte');
$this->assertEquals('"sprode, henriëtte" <local-part@münchen.com>', $address->toReadableString());

$address = new Address(new EmailAddress('local-part@münchen.com'), 'Frederik');
$this->assertEquals('Frederik <local-part@münchen.com>', $address->toReadableString());

$address = new Address(new EmailAddress('local-part@münchen.com'));
$this->assertEquals('local-part@münchen.com', $address->toReadableString());
}

/**
* @return array
*/
Expand Down

0 comments on commit 95aa894

Please sign in to comment.