Skip to content

Commit

Permalink
New: Add Carbon.String.isValidEmail helper
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnitto committed Sep 26, 2023
1 parent 4817dc3 commit 6b5607f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
20 changes: 18 additions & 2 deletions Classes/EelHelper/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
use Neos\Eel\EvaluationException;
use Neos\Eel\ProtectedContextAwareInterface;
use Neos\Flow\Annotations as Flow;
use function array_unique;
use function func_get_args;
use Neos\Flow\Validation\Validator\EmailAddressValidator;
use function implode;
use function is_array;
use function lcfirst;
Expand Down Expand Up @@ -51,6 +50,23 @@ public function urlize(string $string): string
return Transliterator::urlize($string);
}

/**
* Checks if the string is a valid email address
*
* @param string|null $email
* @return boolean
*/
public function isValidEmail(?string $email = null)
{
if (!is_string($email)) {
return false;
}

$validator = new EmailAddressValidator();
$result = $validator->validate($email);
return $validator->validate($email)->hasErrors() === false;
}

/**
* Helper to convert strings to PascalCase
*
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,20 @@ Examples:

**Return** The phone number, optimized for links

### `Carbon.String.isValidEmail(emailAddress)`

Checks if the string is a valid email address

Examples:

| Expression | Result |
| ------------------------------------------ | ------- |
| `Carbon.String.isValidEmail('')` | `false` |
| `Carbon.String.phone('Some text')` | `false` |
| `Carbon.String.phone('[email protected]')` | `true` |

- `emailAddress` (string) The string to check

## Number Helper

### `Carbon.Number.format(number, decimals, dec_point, thousands_sep)`
Expand Down

0 comments on commit 6b5607f

Please sign in to comment.