From 5f98dbea94f721a2b9183615cc0ef5a778c5cbe6 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sat, 11 Jun 2016 09:57:17 +0200 Subject: [PATCH] Adding comments --- Datatypes/Iban.php | 164 +++++++++++++++++++++++++++--- Validation/Base/IBAN.php | 40 +++++++- Validation/Base/IbanEnum.php | 2 +- Validation/Base/IbanErrorType.php | 2 +- 4 files changed, 191 insertions(+), 17 deletions(-) diff --git a/Datatypes/Iban.php b/Datatypes/Iban.php index c7e53fee1..1c6bb5547 100644 --- a/Datatypes/Iban.php +++ b/Datatypes/Iban.php @@ -15,7 +15,6 @@ */ namespace phpOMS\Datatypes; -use phpOMS\Localization\ISO3166TwoEnum; use phpOMS\Validation\Base\IbanEnum; /** @@ -33,13 +32,37 @@ use phpOMS\Validation\Base\IbanEnum; */ class Iban implements \Serializable { + /** + * Iban. + * + * @var string + * @since 1.0.0 + */ private $iban = ''; + /** + * Constructor. + * + * @param string $iban Iban + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function __construct(string $iban) { $this->parse($iban); } + /** + * Parsing iban string + * + * @param string $iban Iban to parse + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ private function parse(string $iban) { if (!\phpOMS\Validation\Base\Iban::isValid($iban)) { @@ -49,22 +72,42 @@ class Iban implements \Serializable $this->iban = self::normalize($iban); } + /** + * Get 2 digit country code + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function getCountry() : string { - $code = substr($this->iban, 0, 2); - - if (!ISO3166TwoEnum::isValidValue($code)) { - throw new \Exception('Invalid country code'); - } - - return $code; + return substr($this->iban, 0, 2); } + /** + * Get normalized iban length + * + * @return int + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function getLength() : int { return strlen($this->iban); } + /** + * Get sequence specified in the layout + * + * @param string $sequence Sequence identifier + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ private function getSequence(string $sequence) : string { $country = $this->getCountry(); @@ -80,66 +123,161 @@ class Iban implements \Serializable return substr($this->iban, $start, $end - $start); } + /** + * Get iban checksum + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function getChecksum() : string { return $this->getSequence('k'); } + /** + * Get national checksum + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function getNationalChecksum() : string { return $this->getSequence('x'); } + /** + * Get branch code + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function getBranchCode() : string { return $this->getSequence('s'); } + /** + * Get account type (cheque account, savings etc.) + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function getAccountType() : string { return $this->getSequence('t'); } + /** + * Get currency + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function getCurrency() : string { return $this->getSequence('m'); } - public function getBicBankCode() : string - { - return $this->getSequence('a'); - } - + /** + * Get bank code + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function getBankCode() : string { return $this->getSequence('b'); } + /** + * Get account + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function getAccount() : string { return $this->getSequence('n'); } + /** + * Get holder's kennital + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function getHoldersKennital() : string { return $this->getSequence('i'); } + /** + * Get owner account number + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function getOwnerAccountNumber() : string { return $this->getSequence('n'); } + /** + * Get BIC + * + * Only very rarely used in iban + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function getBicCode() : string { return $this->getSequence('a'); } + /** + * Pretty print iban + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function prettyPrint() : string { return wordwrap($this->iban, 4, ' ', true); } + /** + * Normalize iban + * + * @param string $iban Iban to normalize + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public static function normalize(string $iban) : string { return strtoupper(str_replace(' ', '', $iban)); diff --git a/Validation/Base/IBAN.php b/Validation/Base/IBAN.php index a3404efc4..f72cfcc4f 100644 --- a/Validation/Base/IBAN.php +++ b/Validation/Base/IBAN.php @@ -41,9 +41,13 @@ abstract class Iban extends ValidatorAbstract } /** - * {@inheritdoc} + * @param string $value Iban to validate + * + * @return bool + * + * @since 1.0.0 + * @author Dennis Eichhorn */ - // todo: this is bad see wiki for better checks... public static function isValid($value) : bool { $value = \phpOMS\Datatypes\Iban::normalize($value); @@ -82,6 +86,16 @@ abstract class Iban extends ValidatorAbstract return true; } + /** + * Validate checksum + * + * @param string $iban Iban to validate + * + * @return bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ private static function validateChecksum(string $iban) : bool { $chars = ['a' => 10, 'b' => 11, 'c' => 12, 'd' => 13, 'e' => 14, 'f' => 15, 'g' => 16, 'h' => 17, 'i' => 18, @@ -109,6 +123,17 @@ abstract class Iban extends ValidatorAbstract return $mod == 1; } + /** + * Validate positions that should have zeros + * + * @param string $iban Iban to validate + * @param string $layout Iban layout + * + * @return bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ private static function validateZeros(string $iban, string $layout) : bool { if (strpos($layout, '0') === false) { @@ -127,6 +152,17 @@ abstract class Iban extends ValidatorAbstract return true; } + /** + * Validate positions that should be numeric + * + * @param string $iban Iban to validate + * @param string $layout Iban layout + * + * @return bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ private static function validateNumeric(string $iban, string $layout) : bool { if (strpos($layout, 'n') === false) { diff --git a/Validation/Base/IbanEnum.php b/Validation/Base/IbanEnum.php index 8a73a7438..54fdcc6fc 100644 --- a/Validation/Base/IbanEnum.php +++ b/Validation/Base/IbanEnum.php @@ -18,7 +18,7 @@ namespace phpOMS\Validation\Base; use phpOMS\Datatypes\Enum; /** - * Country codes ISO list. + * Iban layout definition. * * @category Framework * @package phpOMS\Localization diff --git a/Validation/Base/IbanErrorType.php b/Validation/Base/IbanErrorType.php index 19cd419a1..2bac52769 100644 --- a/Validation/Base/IbanErrorType.php +++ b/Validation/Base/IbanErrorType.php @@ -17,7 +17,7 @@ namespace phpOMS\Validation\Base; use phpOMS\Datatypes\Enum; /** - * Address type enum. + * Iban error type enum. * * @category Framework * @package phpOMS\Datatypes