public static function createFromBankAccount(BankAccountInterface $bankAccount, \Exception $e = null) { if ($bankAccount instanceof BankAccountNormalized) { $bankAccount = $bankAccount->getOriginalBankAccount(); } $details = sprintf('%s %s', Util::maskSortCode($bankAccount->getSortCode()), Util::maskAccountNumber($bankAccount->getAccountNumber())); return new self(sprintf('Unable to determine if the bank details `%s` are valid or invalid', $details), 0, $e); }
/** * Fetch the result of the modulus check on the account. * * @param bool $assume Result to use if the account could not be validated. * * @return bool True if details are usable */ public function isValid($assume = true) { try { Assert::boolean($assume, 'assume should be a boolean, got: `%s`'); } catch (\InvalidArgumentException $e) { throw E::wrap($e); } return $this->specKnown ? $this->specResult : $assume; }
/** * @param string|SortCode $sortCode * @param string $accountNumber */ public function __construct($sortCode, $accountNumber) { try { if (!$sortCode instanceof SortCode) { Assert::string($sortCode, 'Sort code must be a string or instance of SortCode'); $sortCode = SortCode::create($sortCode); } Assert::string($accountNumber, 'Account number must be a string'); Assert::regex($accountNumber, '{^(?:.*\\d.*){6}$}', 'Account number must contain at least 6 digits'); } catch (\InvalidArgumentException $e) { throw E::wrap($e); } $this->sortCode = $sortCode; $this->accountNumber = preg_replace('{[^0-9]}', '', $accountNumber); }
/** * @param BankAccountInterface $bankAccount * @param string|SortCode $sortCode * @param string $accountNumber */ public function __construct(BankAccountInterface $bankAccount, $sortCode, $accountNumber) { try { if (!$sortCode instanceof SortCode) { Assert::string($sortCode, 'Sort code must be a string or instance of SortCode'); $sortCode = SortCode::create($sortCode); } Assert::string($accountNumber, 'Account number must be a string'); } catch (\InvalidArgumentException $e) { throw E::wrap($e); } $this->bankAccount = $bankAccount; $this->sortCode = $sortCode; $this->accountNumber = preg_replace('{[^0-9]}', '', $accountNumber); if (self::LENGTH !== strlen($this->accountNumber)) { throw Exception\AccountNumberInvalidException::create($accountNumber); } }
/** * @param string $format * * @return string */ public function format($format) { try { Assert::string($format, 'Format must be a string, got: %s'); } catch (\InvalidArgumentException $e) { throw E::wrap($e); } return sprintf($format, $this->parts[0], $this->parts[1], $this->parts[2]); }
/** * Perform evaluation of the supplied sort code and account number. * * This will normalize the supplied input and then perform modulus check. * * @param string $sortCode * @param string $accountNumber * * @return Result */ public function lookup($sortCode, $accountNumber) { try { Assert::string($sortCode, 'Sort code must be a string'); Assert::string($accountNumber, 'Account number must be a string'); } catch (\InvalidArgumentException $e) { throw E::wrap($e); } $account = new BankAccount($sortCode, $accountNumber); $account = $this->normalizeBankAccount($account); try { $valid = $this->spec->check($account); $validated = true; } catch (CannotValidateException $e) { $validated = false; $valid = null; } return new Result($account, $validated, $valid); }