Skip to content

Commit

Permalink
Add emailDomain helper
Browse files Browse the repository at this point in the history
  • Loading branch information
mpstenson committed Jul 11, 2024
1 parent fc051ed commit fabd4e4
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 35 deletions.
84 changes: 49 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ AdvStr::redactSsn('My social security number is 222-22-2222'); // My social secu
- [Usage](#usage)
- [Available Methods](#available-methods)
- [advPassword](#advpassword)
- [readTime](#readtime)
- [charWrap](#charwrap)
- [splitName](#splitname)
- [redactSsn](#redactssn)
- [emailDomain](#emaildomain)
- [readTime](#readtime)
- [redactCreditCard](#redactcreditcard)
- [redactSsn](#redactssn)
- [splitName](#splitname)
- [Testing](#testing)
- [Changelog](#changelog)
- [Contributing](#contributing)
Expand Down Expand Up @@ -104,58 +105,77 @@ public static function advPassword(
#### Returns:
- string: Generated password

### [readTime](#readtime)
### [charWrap](#charwrap)

Calculates the read time of a string.
Wraps a string at a given number of characters regardless of words.

```php
public static function readTime(
public static function charWrap(
$string,
$wpm = 200
$length = 80
)
```

#### Parameters:
- `$string` (string): The text to calculate read time for
- `$wpm` (int): Words per minute (default: 200)
- `$string` (string): The string to wrap
- `$length` (int): The number of characters to wrap at (default: 80)

#### Returns:
- int: Estimated read time in seconds
- string: The wrapped string

### [charWrap](#charwrap)
### [emailDomain](#emailDomain)

Wraps a string at a given number of characters regardless of words.

```php
public static function charWrap(
$string,
$length = 80
public static function EmailDomain(
$string
)
```

#### Parameters:
- `$string` (string): The string to wrap
- `$length` (int): The number of characters to wrap at (default: 80)
- `$string` (string): The string to extract the email domain from.

#### Returns:
- string: The wrapped string
- string: The email domain from the string

### [splitName](#splitname)
### [readTime](#readtime)

Splits a full name into first name, middle name (if present), and last name, removing any prefixes and suffixes. This method can handle both "Firstname Lastname" and "Lastname, Firstname" formats.
Calculates the read time of a string.

```php
public static function splitName(
$name
public static function readTime(
$string,
$wpm = 200
)
```

#### Parameters:
- `$name` (string): The full name to split
- `$string` (string): The text to calculate read time for
- `$wpm` (int): Words per minute (default: 200)

#### Returns:
- array: An associative array containing 'first', 'middle' (if present), and 'last' name
- int: Estimated read time in seconds

### [redactCreditCard](#redactcreditcard)
Note: This method is currently not implemented (TODO).
Redacts credit card numbers in a string.

```php
public static function redactCreditCard(
$string,
$redacted = '********',
$exclude = []
)
```

#### Parameters:
- `$string` (string): The string containing credit card numbers to redact
- `$redacted` (string): The string to replace credit card numbers with (default: '********')
- `$exclude` (array): An array of credit card types to exclude from redaction

#### Returns:
- string: The string with credit card numbers redacted
### [redactSsn](#redactssn)

Redacts Social Security Numbers (SSN) in a string.
Expand All @@ -178,27 +198,21 @@ public static function redactSsn(
#### Returns:
- string: The string with SSNs redacted

### [redactCreditCard](#redactcreditcard)
### [splitName](#splitname)

Redacts credit card numbers in a string.
Splits a full name into first name, middle name (if present), and last name, removing any prefixes and suffixes. This method can handle both "Firstname Lastname" and "Lastname, Firstname" formats.

```php
public static function redactCreditCard(
$string,
$redacted = '********',
$exclude = []
public static function splitName(
$name
)
```

#### Parameters:
- `$string` (string): The string containing credit card numbers to redact
- `$redacted` (string): The string to replace credit card numbers with (default: '********')
- `$exclude` (array): An array of credit card types to exclude from redaction
- `$name` (string): The full name to split

#### Returns:
- string: The string with credit card numbers redacted

Note: This method is currently not implemented (TODO).
- array: An associative array containing 'first', 'middle' (if present), and 'last' name


## Testing
Expand Down
16 changes: 16 additions & 0 deletions src/AdvStr.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,22 @@ public static function redactSsn($string, $redacted = '********', $dashes = true
return $string;
}

/**
* Extracts the domain from an email address.
*
* @param string $string The email address to extract the domain from.
* @return string The domain extracted from the email address.
*/
public static function emailDomain($string)
{
// Extract the domain using regex
if (preg_match('/@([a-zA-Z0-9.-]+)/', $string, $matches)) {
return $matches[1];
}

return '';
}

/**
* Redacts credit card numbers and replaces them with a given string
*
Expand Down
23 changes: 23 additions & 0 deletions tests/EmailDomainTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use mpstenson\AdvStr\Facades\AdvStr;

test('Can get email domain', function () {
$domain = AdvStr::emailDomain('[email protected]');
expect($domain)->toBe('example.com');
});

test('Can get email domain with subdomain', function () {
$domain = AdvStr::emailDomain('[email protected]');
expect($domain)->toBe('test.example.com');
});

test('Can get email domain with trailing spaces and text', function () {
$domain = AdvStr::emailDomain('[email protected] some more text');
expect($domain)->toBe('example.com');
});

test('Cat get email domain with not allowed characters afterwards', function () {
$domain = AdvStr::emailDomain('"example user" <[email protected]>');
expect($domain)->toBe('example.com');
});

0 comments on commit fabd4e4

Please sign in to comment.