Skip to content
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

Cleanup code regarding php8 #94

Merged
merged 1 commit into from
Dec 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
},
"require" : {
"php" : "^8",
"ext-ctype": "*",
"symfony/options-resolver": "^5.4|^6"
},
"suggest": {
Expand Down
10 changes: 2 additions & 8 deletions src/CountryInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,9 @@
*/
final class CountryInfo
{
/**
* @var string
*/
private $countryCode;
private string $countryCode;

/**
* @var Registry
*/
private $swiftRegistry;
private Registry $swiftRegistry;

public function __construct(string $countryCode, Registry $swiftRegistry = null)
{
Expand Down
5 changes: 1 addition & 4 deletions src/Exception/InvalidChecksumException.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
*/
class InvalidChecksumException extends \RuntimeException
{
/**
* @var string
*/
protected $validChecksum;
protected string $validChecksum;

public function __construct(string $iban, string $validChecksum)
{
Expand Down
33 changes: 14 additions & 19 deletions src/Iban.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ final class Iban
private const CHECKSUM_LENGTH = 2;
private const BBAN_OFFSET = 4;

/**
* @var string
*/
private $iban;
private string $iban;

public function __construct(string $iban)
private Registry $swiftRegistry;

public function __construct(string $iban, Registry $swiftRegistry = null)
{
$this->iban = $iban;

$this->swiftRegistry = $swiftRegistry ?? new Registry();
}

public function __toString(): string
Expand All @@ -59,16 +60,12 @@ public function getNormalizedIban(): string

public function format(string $type = self::FORMAT_PRINT): string
{
switch ($type) {
case self::FORMAT_ELECTRONIC:
return $this->getNormalizedIban();
case self::FORMAT_PRINT:
return wordwrap($this->getNormalizedIban(), 4, ' ', true);
case self::FORMAT_ANONYMIZED:
return str_pad(substr($this->getNormalizedIban(), -4), strlen($this->getNormalizedIban()), 'X', STR_PAD_LEFT);
default:
return $this->iban;
}
return match ($type) {
self::FORMAT_ELECTRONIC => $this->getNormalizedIban(),
self::FORMAT_PRINT => wordwrap($this->getNormalizedIban(), 4, ' ', true),
self::FORMAT_ANONYMIZED => str_pad(substr($this->getNormalizedIban(), -4), strlen($this->getNormalizedIban()), 'X', STR_PAD_LEFT),
default => $this->iban,
};
}

public function countryCode(): string
Expand All @@ -88,12 +85,10 @@ public function bban(): string

public function bbanBankIdentifier(): string
{
$registry = new Registry();

return substr(
$this->bban(),
$registry->getBbanBankIdentifierStartPos($this->countryCode()),
$registry->getBbanBankIdentifierEndPos($this->countryCode())
$this->swiftRegistry->getBbanBankIdentifierStartPos($this->countryCode()),
$this->swiftRegistry->getBbanBankIdentifierEndPos($this->countryCode())
);
}
}
5 changes: 1 addition & 4 deletions src/Swift/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
*/
final class Registry
{
/**
* @var array
*/
private $registry;
private array $registry;

public function __construct(RegistryLoaderInterface $registryLoader = null)
{
Expand Down
35 changes: 11 additions & 24 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,64 +25,51 @@
*/
final class Validator
{
/**
* @var Registry
*/
private $swiftRegistry;
private array $options;

/**
* @var array
*/
private $options = [];
private Registry $swiftRegistry;

/**
* @var array
*/
private $violations = [];
private array $violations;

public function __construct(array $options = [], Registry $swiftRegistry = null)
{
$this->swiftRegistry = $swiftRegistry ?? new Registry();

$resolver = new OptionsResolver();
$this->configureOptions($resolver);
$this->options = $resolver->resolve($options);

$this->swiftRegistry = $swiftRegistry ?? new Registry();
$this->violations = [];
}

/**
* @param string|Iban $iban
*/
public function validate($iban): bool
public function validate(string|Iban $iban): bool
{
if (!$iban instanceof Iban) {
$iban = new Iban($iban);
}

$this->violations = [];

try {
$this->validateCountryCode($iban);
} catch (UnsupportedCountryCodeException $exception) {
} catch (UnsupportedCountryCodeException) {
$this->violations[] = $this->options['violation.unsupported_country'];

return false; // return here because with an unsupported country code all other checks make no sense at all
}

try {
$this->validateLength($iban);
} catch (InvalidLengthException $exception) {
} catch (InvalidLengthException) {
$this->violations[] = $this->options['violation.invalid_length'];
}

try {
$this->validateFormat($iban);
} catch (InvalidFormatException $exception) {
} catch (InvalidFormatException) {
$this->violations[] = $this->options['violation.invalid_format'];
}

try {
$this->validateChecksum($iban);
} catch (InvalidChecksumException $exception) {
} catch (InvalidChecksumException) {
$this->violations[] = $this->options['violation.invalid_checksum'];
}

Expand Down