diff --git a/README.md b/README.md index 349905e..6508dcb 100644 --- a/README.md +++ b/README.md @@ -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) @@ -295,10 +296,10 @@ Convert addresses data to [RFC 2822](http://faqs.org/rfcs/rfc2822.html) string, ```php to_rfc2822_email([ ['address' => 'john.doe@example.com', 'name' => 'John Doe'], - ['address' => 'mary.smith@example.com'], + ['address' => 'jane.smith@example.com'], ]); -// "John Doe , mary.smith@example.com" +// "John Doe , jane.smith@example.com" ``` Also, it supports simplified syntax for a single address: @@ -316,10 +317,10 @@ Convert addresses data to [SwiftMailer-suitable format](https://swiftmailer.org/ ```php to_swiftmailer_emails([ ['address' => 'john.doe@example.com', 'name' => 'John Doe'], - ['address' => 'mary.smith@example.com'], + ['address' => 'jane.smith@example.com'], ]); -// ["john.doe@example.com" => "John Doe", "mary.smith@example.com"] +// ["john.doe@example.com" => "John Doe", "jane.smith@example.com"] ``` Also, it supports simplified syntax for a single address: @@ -330,6 +331,27 @@ to_swiftmailer_emails(['address' => 'john.doe@example.com', 'name' => 'John Doe' // ["john.doe@example.com" => "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' => 'john.doe@example.com', 'name' => 'John Doe'], + ['address' => 'jane.smith@example.com'], +]); + +// ["John Doe ", "jane.smith@example.com"] +``` + +Also, it supports simplified syntax for a single address: + +```php +to_symfony_emails(['address' => 'john.doe@example.com', 'name' => 'John Doe']); + +// ["John Doe "] +``` + ## Filesystem #### `relative_path()` diff --git a/src/email.php b/src/email.php index 3c11d9c..6bb5a1f 100644 --- a/src/email.php +++ b/src/email.php @@ -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); } @@ -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) @@ -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) @@ -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(); + } +} diff --git a/tests/email/ToSymfonyEmailsTest.php b/tests/email/ToSymfonyEmailsTest.php new file mode 100644 index 0000000..f32989e --- /dev/null +++ b/tests/email/ToSymfonyEmailsTest.php @@ -0,0 +1,86 @@ +assertEquals([], to_symfony_emails([])); + } + + /** @test */ + public function it_supports_simplified_syntax_for_one_email() + { + $this->assertEquals( + ['John Doe '], + to_symfony_emails(['address' => 'john.doe@example.com', 'name' => 'John Doe']), + ); + } + + /** @test */ + public function it_supports_multiple_emails() + { + $this->assertEquals( + ['John Doe ', 'Jane Doe ', 'Mary Doe '], + to_symfony_emails([ + ['address' => 'john.doe@example.com', 'name' => 'John Doe'], + ['address' => 'jane.doe@example.com', 'name' => 'Jane Doe'], + ['address' => 'mary.doe@example.com', 'name' => 'Mary Doe'], + ]), + ); + } + + /** @test */ + public function it_skips_items_with_empty_addresses() + { + $this->assertEquals( + ['John Doe ', 'Mary Doe '], + to_symfony_emails([ + ['address' => 'john.doe@example.com', 'name' => 'John Doe'], + ['address' => null, 'name' => 'Jane Doe'], + ['address' => false, 'name' => 'Jane Doe'], + ['address' => '', 'name' => 'Jane Doe'], + ['name' => 'Fake Doe'], + ['address' => 'mary.doe@example.com', 'name' => 'Mary Doe'], + ]), + ); + } + + /** @test */ + public function it_skips_items_with_invalid_addresses() + { + $this->assertEquals( + ['John Doe ', 'Vicky Doe '], + to_symfony_emails([ + ['address' => 'john.doe@example.com', 'name' => 'John Doe'], + ['address' => 'jane@', 'name' => 'Jane Doe'], + ['address' => 'jane@example', 'name' => 'Jane Doe'], + ['address' => 'vicky.doe@example.com', 'name' => 'Vicky Doe'], + ['address' => null, 'name' => 'Jane Doe'], + ]), + ); + } + + /** @test */ + public function name_is_optional_for_one_email() + { + $this->assertEquals(['john.doe@example.com'], to_symfony_emails(['address' => 'john.doe@example.com'])); + } + + /** @test */ + public function name_is_optional_for_multiple_emails() + { + $this->assertEquals( + ['John Doe ', 'jane.doe@example.com', 'mary.doe@example.com'], + to_symfony_emails([ + ['address' => 'john.doe@example.com', 'name' => 'John Doe'], + ['address' => 'jane.doe@example.com'], + ['address' => 'mary.doe@example.com'], + ]), + ); + } +}