-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Framework agnostic approach #38
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,21 +8,25 @@ | |
use Merkeleon\PhpCryptocurrencyAddressValidation\Contracts\Driver; | ||
use Merkeleon\PhpCryptocurrencyAddressValidation\Enums\CurrencyEnum; | ||
use Merkeleon\PhpCryptocurrencyAddressValidation\Exception\AddressValidationException; | ||
use function app; | ||
use function config; | ||
|
||
readonly class Validator implements Contracts\Validator | ||
class Validator implements Contracts\Validator | ||
{ | ||
private const CONFIG_PATH = __DIR__ . '/../config/address_validation.php'; | ||
|
||
/** | ||
* @param DriverConfig[] $options | ||
*/ | ||
public function __construct( | ||
private string $chain, | ||
private array $options, | ||
private bool $isMainnet = true | ||
private readonly string $chain, | ||
private readonly bool $isMainnet = true, | ||
private ?array $options = null, | ||
) { | ||
$this->options = $options ?? $this->resolveConfigForCurrency(CurrencyEnum::from($this->chain)); | ||
} | ||
|
||
public static function make(CurrencyEnum $currency): Validator | ||
public static function make(CurrencyEnum $currency, bool $isMainnet = true, ?array $config = null): self | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since this factory method copies current construct method I was thinking if it would make sense to remove it. But I left it here for backwards compatibility reasons. Maybe we can docblock it with @deprecated to encourage developers to use constructor instead? |
||
{ | ||
return new Validator($currency->value, config("address_validation.{$currency->value}"), app()->isProduction()); | ||
return new self($currency->value, $isMainnet, $config); | ||
} | ||
|
||
public function isValid(?string $address): bool | ||
|
@@ -32,6 +36,7 @@ public function isValid(?string $address): bool | |
} | ||
|
||
$drivers = $this->getDrivers(); | ||
|
||
// if there is no drivers we force address to be valid | ||
if (null === $drivers || !$drivers->valid()) { | ||
return true; | ||
|
@@ -47,6 +52,7 @@ public function validate(?string $address): void | |
} | ||
|
||
$drivers = $this->getDrivers(); | ||
|
||
// if there is no drivers we force address to be valid | ||
if (null === $drivers || !$drivers->valid()) { | ||
return; | ||
|
@@ -68,7 +74,6 @@ public function validate(?string $address): void | |
*/ | ||
protected function getDrivers(): ?Generator | ||
{ | ||
/** @var DriverConfig $driverConfig */ | ||
foreach ($this->options as $driverConfig) { | ||
if ($driver = $driverConfig->makeDriver($this->isMainnet)) { | ||
yield $driver; | ||
|
@@ -78,9 +83,11 @@ protected function getDrivers(): ?Generator | |
return null; | ||
} | ||
|
||
/** | ||
* @param Driver[] $drivers | ||
*/ | ||
protected function getDriver(iterable $drivers, string $address): ?Driver | ||
{ | ||
/** @var Driver $driver */ | ||
foreach ($drivers as $driver) { | ||
if ($driver->match($address)) { | ||
return $driver; | ||
|
@@ -89,4 +96,15 @@ protected function getDriver(iterable $drivers, string $address): ?Driver | |
|
||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* @return DriverConfig[] | ||
*/ | ||
protected function resolveConfigForCurrency(CurrencyEnum $currency): array | ||
{ | ||
/** @var array<string, DriverConfig[]> $config */ | ||
$config = require self::CONFIG_PATH; | ||
|
||
return $config[$currency->value]; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems like there is not usage for this magic method in a code anymore. feel free to correct me if I missed something