mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-02-12 14:58:42 +00:00
Adding comments
This commit is contained in:
parent
8b4f4fc275
commit
5f98dbea94
|
|
@ -15,7 +15,6 @@
|
||||||
*/
|
*/
|
||||||
namespace phpOMS\Datatypes;
|
namespace phpOMS\Datatypes;
|
||||||
|
|
||||||
use phpOMS\Localization\ISO3166TwoEnum;
|
|
||||||
use phpOMS\Validation\Base\IbanEnum;
|
use phpOMS\Validation\Base\IbanEnum;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -33,13 +32,37 @@ use phpOMS\Validation\Base\IbanEnum;
|
||||||
*/
|
*/
|
||||||
class Iban implements \Serializable
|
class Iban implements \Serializable
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Iban.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
private $iban = '';
|
private $iban = '';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor.
|
||||||
|
*
|
||||||
|
* @param string $iban Iban
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
*/
|
||||||
public function __construct(string $iban)
|
public function __construct(string $iban)
|
||||||
{
|
{
|
||||||
$this->parse($iban);
|
$this->parse($iban);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parsing iban string
|
||||||
|
*
|
||||||
|
* @param string $iban Iban to parse
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
*/
|
||||||
private function parse(string $iban)
|
private function parse(string $iban)
|
||||||
{
|
{
|
||||||
if (!\phpOMS\Validation\Base\Iban::isValid($iban)) {
|
if (!\phpOMS\Validation\Base\Iban::isValid($iban)) {
|
||||||
|
|
@ -49,22 +72,42 @@ class Iban implements \Serializable
|
||||||
$this->iban = self::normalize($iban);
|
$this->iban = self::normalize($iban);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get 2 digit country code
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
*/
|
||||||
public function getCountry() : string
|
public function getCountry() : string
|
||||||
{
|
{
|
||||||
$code = substr($this->iban, 0, 2);
|
return substr($this->iban, 0, 2);
|
||||||
|
|
||||||
if (!ISO3166TwoEnum::isValidValue($code)) {
|
|
||||||
throw new \Exception('Invalid country code');
|
|
||||||
}
|
|
||||||
|
|
||||||
return $code;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get normalized iban length
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
*/
|
||||||
public function getLength() : int
|
public function getLength() : int
|
||||||
{
|
{
|
||||||
return strlen($this->iban);
|
return strlen($this->iban);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get sequence specified in the layout
|
||||||
|
*
|
||||||
|
* @param string $sequence Sequence identifier
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
*/
|
||||||
private function getSequence(string $sequence) : string
|
private function getSequence(string $sequence) : string
|
||||||
{
|
{
|
||||||
$country = $this->getCountry();
|
$country = $this->getCountry();
|
||||||
|
|
@ -80,66 +123,161 @@ class Iban implements \Serializable
|
||||||
return substr($this->iban, $start, $end - $start);
|
return substr($this->iban, $start, $end - $start);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get iban checksum
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
*/
|
||||||
public function getChecksum() : string
|
public function getChecksum() : string
|
||||||
{
|
{
|
||||||
return $this->getSequence('k');
|
return $this->getSequence('k');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get national checksum
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
*/
|
||||||
public function getNationalChecksum() : string
|
public function getNationalChecksum() : string
|
||||||
{
|
{
|
||||||
return $this->getSequence('x');
|
return $this->getSequence('x');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get branch code
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
*/
|
||||||
public function getBranchCode() : string
|
public function getBranchCode() : string
|
||||||
{
|
{
|
||||||
return $this->getSequence('s');
|
return $this->getSequence('s');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get account type (cheque account, savings etc.)
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
*/
|
||||||
public function getAccountType() : string
|
public function getAccountType() : string
|
||||||
{
|
{
|
||||||
return $this->getSequence('t');
|
return $this->getSequence('t');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get currency
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
*/
|
||||||
public function getCurrency() : string
|
public function getCurrency() : string
|
||||||
{
|
{
|
||||||
return $this->getSequence('m');
|
return $this->getSequence('m');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getBicBankCode() : string
|
/**
|
||||||
{
|
* Get bank code
|
||||||
return $this->getSequence('a');
|
*
|
||||||
}
|
* @return string
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
*/
|
||||||
public function getBankCode() : string
|
public function getBankCode() : string
|
||||||
{
|
{
|
||||||
return $this->getSequence('b');
|
return $this->getSequence('b');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get account
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
*/
|
||||||
public function getAccount() : string
|
public function getAccount() : string
|
||||||
{
|
{
|
||||||
return $this->getSequence('n');
|
return $this->getSequence('n');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get holder's kennital
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
*/
|
||||||
public function getHoldersKennital() : string
|
public function getHoldersKennital() : string
|
||||||
{
|
{
|
||||||
return $this->getSequence('i');
|
return $this->getSequence('i');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get owner account number
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
*/
|
||||||
public function getOwnerAccountNumber() : string
|
public function getOwnerAccountNumber() : string
|
||||||
{
|
{
|
||||||
return $this->getSequence('n');
|
return $this->getSequence('n');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get BIC
|
||||||
|
*
|
||||||
|
* Only very rarely used in iban
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
*/
|
||||||
public function getBicCode() : string
|
public function getBicCode() : string
|
||||||
{
|
{
|
||||||
return $this->getSequence('a');
|
return $this->getSequence('a');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pretty print iban
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
*/
|
||||||
public function prettyPrint() : string
|
public function prettyPrint() : string
|
||||||
{
|
{
|
||||||
return wordwrap($this->iban, 4, ' ', true);
|
return wordwrap($this->iban, 4, ' ', true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Normalize iban
|
||||||
|
*
|
||||||
|
* @param string $iban Iban to normalize
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
*/
|
||||||
public static function normalize(string $iban) : string
|
public static function normalize(string $iban) : string
|
||||||
{
|
{
|
||||||
return strtoupper(str_replace(' ', '', $iban));
|
return strtoupper(str_replace(' ', '', $iban));
|
||||||
|
|
|
||||||
|
|
@ -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 <d.eichhorn@oms.com>
|
||||||
*/
|
*/
|
||||||
// todo: this is bad see wiki for better checks...
|
|
||||||
public static function isValid($value) : bool
|
public static function isValid($value) : bool
|
||||||
{
|
{
|
||||||
$value = \phpOMS\Datatypes\Iban::normalize($value);
|
$value = \phpOMS\Datatypes\Iban::normalize($value);
|
||||||
|
|
@ -82,6 +86,16 @@ abstract class Iban extends ValidatorAbstract
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validate checksum
|
||||||
|
*
|
||||||
|
* @param string $iban Iban to validate
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
*/
|
||||||
private static function validateChecksum(string $iban) : bool
|
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,
|
$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;
|
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 <d.eichhorn@oms.com>
|
||||||
|
*/
|
||||||
private static function validateZeros(string $iban, string $layout) : bool
|
private static function validateZeros(string $iban, string $layout) : bool
|
||||||
{
|
{
|
||||||
if (strpos($layout, '0') === false) {
|
if (strpos($layout, '0') === false) {
|
||||||
|
|
@ -127,6 +152,17 @@ abstract class Iban extends ValidatorAbstract
|
||||||
return true;
|
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 <d.eichhorn@oms.com>
|
||||||
|
*/
|
||||||
private static function validateNumeric(string $iban, string $layout) : bool
|
private static function validateNumeric(string $iban, string $layout) : bool
|
||||||
{
|
{
|
||||||
if (strpos($layout, 'n') === false) {
|
if (strpos($layout, 'n') === false) {
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ namespace phpOMS\Validation\Base;
|
||||||
use phpOMS\Datatypes\Enum;
|
use phpOMS\Datatypes\Enum;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Country codes ISO list.
|
* Iban layout definition.
|
||||||
*
|
*
|
||||||
* @category Framework
|
* @category Framework
|
||||||
* @package phpOMS\Localization
|
* @package phpOMS\Localization
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ namespace phpOMS\Validation\Base;
|
||||||
use phpOMS\Datatypes\Enum;
|
use phpOMS\Datatypes\Enum;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Address type enum.
|
* Iban error type enum.
|
||||||
*
|
*
|
||||||
* @category Framework
|
* @category Framework
|
||||||
* @package phpOMS\Datatypes
|
* @package phpOMS\Datatypes
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user