Skip to content

Commit

Permalink
improve trashmail facade
Browse files Browse the repository at this point in the history
  • Loading branch information
Gummibeer committed Feb 18, 2020
1 parent c61dd53 commit 3485b27
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

All notable changes to this package will be documented in this file.

## v0.2.0

* switch to manager driver pattern
* `\Elbgoods\TrashmailRule\TrashmailManager`
* `\Elbgoods\TrashmailRule\Providers`
* add service class `\Elbgoods\TrashmailRule\Trashmail`
* add facade `\Elbgoods\TrashmailRule\Facades\Trashmail`
* try-catch all errors in providers and skip this provider

## v0.1.0

* initial release
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ If you want to pass some domains always you can add them to the whitelist. These

## Usage

## Validation Rule

This package provides a basic `TrashmailRule` which you can use. All more specific rules only extend this rule with a predefined `format`.

```php
Expand All @@ -60,6 +62,47 @@ $rule = new TrashmailRule(false);
$rule->nullable();
```

## Facade

You can also use the facade if you want to check any email address outside validation.
This will run the same logic as the validation rule and runs all providers set in the config.

```php
use Elbgoods\TrashmailRule\Facades\Trashmail;

Trashmail::isDisposable('[email protected]');
```

## single Provider

You can also check using a single provider only.
Keep in mind that all providers only accept the domain to check and not a full email address.
The facade provides a method that returns the domain used in an email address.

```php
use Elbgoods\TrashmailRule\Facades\Trashmail;

Trashmail::provider('config')->isDisposable(
Trashmail::getDomain('[email protected]')
);
```

## custom Provider

If you want to add your own provider you can do so.

```php
use Elbgoods\TrashmailRule\Facades\Trashmail;
use Illuminate\Contracts\Container\Container;
use Elbgoods\TrashmailRule\Contracts\ProviderContract;

Trashmail::extend('custom_provider', static function (Container $app): ProviderContract {
return new CustomProvider();
});
```



## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
Expand Down
6 changes: 6 additions & 0 deletions src/Facades/Trashmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

namespace Elbgoods\TrashmailRule\Facades;

use Closure;
use Elbgoods\TrashmailRule\Contracts\ProviderContract;
use Elbgoods\TrashmailRule\Trashmail as TrashmailService;
use Elbgoods\TrashmailRule\TrashmailManager;
use Illuminate\Support\Facades\Facade;

/**
* @method static bool isDisposable(string $email)
* @method static ProviderContract provider(string $provider)
* @method static string getDomain(string $email)
* @method static TrashmailManager extend(string $provider, Closure $creator)
*/
class Trashmail extends Facade
{
Expand Down
14 changes: 13 additions & 1 deletion src/Trashmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Elbgoods\TrashmailRule;

use Closure;
use Elbgoods\TrashmailRule\Contracts\ProviderContract;
use Exception;
use Illuminate\Contracts\Config\Repository as ConfigRepository;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -48,7 +50,17 @@ public function isDisposable(string $email): bool
return false;
}

protected function getDomain(string $email): string
public function provider(string $provider): ProviderContract
{
return $this->manager->driver($provider);
}

public function extend(string $provider, Closure $creator): TrashmailManager
{
return $this->manager->extend($provider, $creator);
}

public function getDomain(string $email): string
{
return trim(mb_strtolower(Str::after($email, '@')));
}
Expand Down

0 comments on commit 3485b27

Please sign in to comment.