Skip to content

Commit

Permalink
Merge branch 'master' into 9.x
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitry-ivanov committed Mar 10, 2022
2 parents f6530d3 + ec906bf commit ea35c47
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 18 deletions.
30 changes: 26 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Laravel-specific and pure PHP Helper Functions.
- [is_email](#is_email)
- [to_rfc2822_email](#to_rfc2822_email)
- [to_swiftmailer_emails](#to_swiftmailer_emails)
- [to_symfony_emails](#to_symfony_emails)
- [Filesystem](#filesystem)
- [relative_path](#relative_path)
Expand Down Expand Up @@ -295,10 +296,10 @@ Convert addresses data to [RFC 2822](http://faqs.org/rfcs/rfc2822.html) string,
```php
to_rfc2822_email([
['address' => '[email protected]', 'name' => 'John Doe'],
['address' => 'mary[email protected]'],
['address' => 'jane[email protected]'],
]);
// "John Doe <[email protected]>, mary[email protected]"
// "John Doe <[email protected]>, jane[email protected]"
```
Also, it supports simplified syntax for a single address:
Expand All @@ -316,10 +317,10 @@ Convert addresses data to [SwiftMailer-suitable format](https://swiftmailer.org/
```php
to_swiftmailer_emails([
['address' => '[email protected]', 'name' => 'John Doe'],
['address' => 'mary[email protected]'],
['address' => 'jane[email protected]'],
]);
// ["[email protected]" => "John Doe", "mary[email protected]"]
// ["[email protected]" => "John Doe", "jane[email protected]"]
```
Also, it supports simplified syntax for a single address:
Expand All @@ -330,6 +331,27 @@ to_swiftmailer_emails(['address' => '[email protected]', 'name' => 'John Doe'
// ["[email protected]" => "John Doe"]
```
#### `to_symfony_emails()`
Convert addresses data to [Symfony-suitable format](https://symfony.com/doc/current/mailer.html#email-addresses):
```php
to_symfony_emails([
['address' => '[email protected]', 'name' => 'John Doe'],
['address' => '[email protected]'],
]);
// ["John Doe <[email protected]>", "[email protected]"]
```
Also, it supports simplified syntax for a single address:
```php
to_symfony_emails(['address' => '[email protected]', 'name' => 'John Doe']);
// ["John Doe <[email protected]>"]
```
## Filesystem
#### `relative_path()`
Expand Down
44 changes: 30 additions & 14 deletions src/email.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
if (!function_exists('is_email')) {
/**
* Check whether the given string is an email address or not.
*
* @param mixed $string
* @return bool
*/
function is_email($string)
function is_email(mixed $string): bool
{
return (bool) filter_var($string, FILTER_VALIDATE_EMAIL);
}
Expand All @@ -21,13 +18,9 @@ function is_email($string)
*
* @see http://faqs.org/rfcs/rfc2822.html
* @see https://php.net/manual/en/function.mail.php
*
* @param array $addresses
* @return string
*/
function to_rfc2822_email(array $addresses)
function to_rfc2822_email(array $addresses): string
{
// Check if we're dealing with one address, without multiarray
$addresses = !empty($addresses['address']) ? [$addresses] : $addresses;

return collect($addresses)
Expand All @@ -51,13 +44,9 @@ function to_rfc2822_email(array $addresses)
* Convert addresses data to SwiftMailer-suitable format.
*
* @see https://swiftmailer.org/docs/messages.html
*
* @param array $addresses
* @return array
*/
function to_swiftmailer_emails(array $addresses)
function to_swiftmailer_emails(array $addresses): array
{
// Check if we're dealing with one address, without multiarray
$addresses = !empty($addresses['address']) ? [$addresses] : $addresses;

return collect($addresses)
Expand All @@ -75,3 +64,30 @@ function to_swiftmailer_emails(array $addresses)
->toArray();
}
}

if (!function_exists('to_symfony_emails')) {
/**
* Convert addresses data to Symfony-suitable format.
*
* @see https://symfony.com/doc/current/mailer.html#email-addresses
*/
function to_symfony_emails(array $addresses): array
{
$addresses = !empty($addresses['address']) ? [$addresses] : $addresses;

return collect($addresses)
->map(function (array $item) {
$name = Arr::get($item, 'name');
$address = Arr::get($item, 'address');

if (!is_email($address)) {
return false;
}

return $name ? "{$name} <{$address}>" : $address;
})
->filter()
->values()
->toArray();
}
}
86 changes: 86 additions & 0 deletions tests/email/ToSymfonyEmailsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace Illuminated\Helpers\Tests\Email;

use Illuminated\Helpers\Tests\TestCase;

class ToSymfonyEmailsTest extends TestCase
{
/** @test */
public function it_returns_an_empty_array_for_an_empty_array()
{
$this->assertEquals([], to_symfony_emails([]));
}

/** @test */
public function it_supports_simplified_syntax_for_one_email()
{
$this->assertEquals(
['John Doe <[email protected]>'],
to_symfony_emails(['address' => '[email protected]', 'name' => 'John Doe']),
);
}

/** @test */
public function it_supports_multiple_emails()
{
$this->assertEquals(
['John Doe <[email protected]>', 'Jane Doe <[email protected]>', 'Mary Doe <[email protected]>'],
to_symfony_emails([
['address' => '[email protected]', 'name' => 'John Doe'],
['address' => '[email protected]', 'name' => 'Jane Doe'],
['address' => '[email protected]', 'name' => 'Mary Doe'],
]),
);
}

/** @test */
public function it_skips_items_with_empty_addresses()
{
$this->assertEquals(
['John Doe <[email protected]>', 'Mary Doe <[email protected]>'],
to_symfony_emails([
['address' => '[email protected]', 'name' => 'John Doe'],
['address' => null, 'name' => 'Jane Doe'],
['address' => false, 'name' => 'Jane Doe'],
['address' => '', 'name' => 'Jane Doe'],
['name' => 'Fake Doe'],
['address' => '[email protected]', 'name' => 'Mary Doe'],
]),
);
}

/** @test */
public function it_skips_items_with_invalid_addresses()
{
$this->assertEquals(
['John Doe <[email protected]>', 'Vicky Doe <[email protected]>'],
to_symfony_emails([
['address' => '[email protected]', 'name' => 'John Doe'],
['address' => 'jane@', 'name' => 'Jane Doe'],
['address' => 'jane@example', 'name' => 'Jane Doe'],
['address' => '[email protected]', 'name' => 'Vicky Doe'],
['address' => null, 'name' => 'Jane Doe'],
]),
);
}

/** @test */
public function name_is_optional_for_one_email()
{
$this->assertEquals(['[email protected]'], to_symfony_emails(['address' => '[email protected]']));
}

/** @test */
public function name_is_optional_for_multiple_emails()
{
$this->assertEquals(
['John Doe <[email protected]>', '[email protected]', '[email protected]'],
to_symfony_emails([
['address' => '[email protected]', 'name' => 'John Doe'],
['address' => '[email protected]'],
['address' => '[email protected]'],
]),
);
}
}

0 comments on commit ea35c47

Please sign in to comment.