new pisilinux web sites
This commit is contained in:
@@ -0,0 +1,331 @@
|
||||
<?php namespace ZN\Validation\CreditCard;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
|
||||
class Formats implements FormatsInterface
|
||||
{
|
||||
/**
|
||||
* Maestro Format
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $maestro =
|
||||
[
|
||||
'type' => 'maestro',
|
||||
'pattern' => '/^(51|52|53|54|55|22|23|24|25|26|27)/',
|
||||
'length' => [12, 13, 14, 15, 16, 18, 19],
|
||||
'cvcLength' => [3],
|
||||
'luhn' => true
|
||||
];
|
||||
|
||||
/**
|
||||
* Forbrugs Foreningen Format
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $forbrugsforeningen =
|
||||
[
|
||||
'type' => 'forbrugsforeningen',
|
||||
'pattern' => '/^600/',
|
||||
'length' => [16],
|
||||
'cvcLength' => [3],
|
||||
'luhn' => true
|
||||
];
|
||||
|
||||
/**
|
||||
* Dankort
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $dankort =
|
||||
[
|
||||
'type' => 'dankort',
|
||||
'pattern' => '/^(5019|4175|4571|4)/',
|
||||
'length' => [16],
|
||||
'cvcLength' => [3],
|
||||
'luhn' => true
|
||||
];
|
||||
|
||||
/**
|
||||
* Visa Format
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $visa =
|
||||
[
|
||||
'type' => 'visa',
|
||||
'pattern' => '/^4/',
|
||||
'length' => [13, 16, 19],
|
||||
'cvcLength' => [3],
|
||||
'luhn' => true
|
||||
];
|
||||
|
||||
/**
|
||||
* Mastercard Format
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $mastercard =
|
||||
[
|
||||
'type' => 'mastercard',
|
||||
'pattern' => '/^(51|52|53|54|55|22|23|24|25|26|27)/',
|
||||
'length' => [16],
|
||||
'cvcLength' => [3],
|
||||
'luhn' => true
|
||||
];
|
||||
|
||||
/**
|
||||
* Amex Format
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $amex =
|
||||
[
|
||||
'type' => 'amex',
|
||||
'pattern' => '/^(34|37)/',
|
||||
'length' => [15],
|
||||
'cvcLength' => [3, 4],
|
||||
'luhn' => true
|
||||
];
|
||||
|
||||
/**
|
||||
* Diners Club Format
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $dinersclub =
|
||||
[
|
||||
'type' => 'dinersClub',
|
||||
'pattern' => '/^(300|301|302|303|304|305|309|36|38|39|54|55)/',
|
||||
'length' => [14, 16],
|
||||
'cvcLength' => [3, 4],
|
||||
'luhn' => true
|
||||
];
|
||||
|
||||
/**
|
||||
* Discover Format
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $discover =
|
||||
[
|
||||
'type' => 'discover',
|
||||
'pattern' => '/^(6011|622|644|645|656|647|648|649|65)/',
|
||||
'length' => [16, 19],
|
||||
'cvcLength' => [3, 4],
|
||||
'luhn' => true
|
||||
];
|
||||
|
||||
/**
|
||||
* Unionpay Format
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $unionpay =
|
||||
[
|
||||
'type' => 'unionpay',
|
||||
'pattern' => '/^(62)/',
|
||||
'length' => [16, 17, 18, 19],
|
||||
'cvcLength' => [3],
|
||||
'luhn' => true
|
||||
];
|
||||
|
||||
/**
|
||||
* JCB Format
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $jcb =
|
||||
[
|
||||
'type' => 'jcb',
|
||||
'pattern' => '/^(352|353|354|355|356|357|358)/',
|
||||
'length' => [16,17,18,19],
|
||||
'cvcLength' => [3, 4],
|
||||
'luhn' => true
|
||||
];
|
||||
|
||||
/**
|
||||
* DinersClub CarteBlanche
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $carteblanche =
|
||||
[
|
||||
'type' => 'carteblanche',
|
||||
'pattern' => '/^(300|301|302|303|304|305)/',
|
||||
'length' => [14],
|
||||
'cvcLength' => [3],
|
||||
'luhn' => true
|
||||
];
|
||||
|
||||
/**
|
||||
* InterPayment
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $interpayment =
|
||||
[
|
||||
'type' => 'interpayment',
|
||||
'pattern' => '/^4/',
|
||||
'length' => [16,17,18,19],
|
||||
'cvcLength' => [3],
|
||||
'luhn' => true
|
||||
];
|
||||
|
||||
/**
|
||||
* NSPK MIR
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $mir =
|
||||
[
|
||||
'type' => 'mir',
|
||||
'pattern' => '/^(2200|2201|2202|2203|2204)/',
|
||||
'length' => [16],
|
||||
'cvcLength' => [3, 4],
|
||||
'luhn' => true
|
||||
];
|
||||
|
||||
/**
|
||||
* Troy
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $troy =
|
||||
[
|
||||
'type' => 'troy',
|
||||
'pattern' => '/^(979200|979289)/',
|
||||
'length' => [16],
|
||||
'cvcLength' => [3, 4],
|
||||
'luhn' => true
|
||||
];
|
||||
|
||||
/**
|
||||
* UATP
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $uatp =
|
||||
[
|
||||
'type' => 'uatp',
|
||||
'pattern' => '/^1/',
|
||||
'length' => [15],
|
||||
'cvcLength' => [3],
|
||||
'luhn' => true
|
||||
];
|
||||
|
||||
/**
|
||||
* Verve
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $verve =
|
||||
[
|
||||
'type' => 'verve',
|
||||
'pattern' => '/^(506|650)/',
|
||||
'length' => [16, 19],
|
||||
'cvcLength' => [3],
|
||||
'luhn' => true
|
||||
];
|
||||
|
||||
/**
|
||||
* BMO ABM Card
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $bmoabm =
|
||||
[
|
||||
'type' => 'bmoabm',
|
||||
'pattern' => '/^(500)/',
|
||||
'length' => [16],
|
||||
'cvcLength' => [3],
|
||||
'luhn' => false
|
||||
];
|
||||
|
||||
/**
|
||||
* CIBC Convenience Card
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $cibc =
|
||||
[
|
||||
'type' => 'cibc',
|
||||
'pattern' => '/^(4506)/',
|
||||
'length' => [16],
|
||||
'cvcLength' => [3],
|
||||
'luhn' => false
|
||||
];
|
||||
|
||||
/**
|
||||
* HSBC Canada Card
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $hsbc =
|
||||
[
|
||||
'type' => 'hsbc',
|
||||
'pattern' => '/^(56)/',
|
||||
'length' => [16],
|
||||
'cvcLength' => [3],
|
||||
'luhn' => false
|
||||
];
|
||||
|
||||
/**
|
||||
* Royal Bank of Canada Client Card
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $rbc =
|
||||
[
|
||||
'type' => 'rbc',
|
||||
'pattern' => '/^(45)/',
|
||||
'length' => [16],
|
||||
'cvcLength' => [3],
|
||||
'luhn' => false
|
||||
];
|
||||
|
||||
/**
|
||||
* Scotiabank Scotia Card
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $scotia =
|
||||
[
|
||||
'type' => 'scotia',
|
||||
'pattern' => '/^(4536)/',
|
||||
'length' => [16],
|
||||
'cvcLength' => [3],
|
||||
'luhn' => false
|
||||
];
|
||||
|
||||
/**
|
||||
* TD Canada Trust Access Card
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $tdtrust =
|
||||
[
|
||||
'type' => 'tdtrust',
|
||||
'pattern' => '/^(589297)/',
|
||||
'length' => [16],
|
||||
'cvcLength' => [3],
|
||||
'luhn' => false
|
||||
];
|
||||
|
||||
/**
|
||||
* Return all card formats
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getList() : array
|
||||
{
|
||||
return get_class_vars(self::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php namespace ZN\Validation\CreditCard;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
|
||||
interface FormatsInterface
|
||||
{
|
||||
/**
|
||||
* Return all card formats
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getList() : array;
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php namespace ZN\Validation\CreditCard;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
|
||||
class LuhnAlgorithm implements LuhnAlgorithmInterface
|
||||
{
|
||||
/**
|
||||
* Protected luhn algorithm check
|
||||
*
|
||||
* @param string $number
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function check(?string $number = NULL)
|
||||
{
|
||||
$table =
|
||||
[
|
||||
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
|
||||
[0, 2, 4, 6, 8, 1, 3, 5, 7, 9]
|
||||
];
|
||||
|
||||
$total = 0; $transform = 0;
|
||||
|
||||
for( $i = strlen($number ?? '') - 1; $i >= 0; $i-- )
|
||||
{
|
||||
$total += $table[$transform++ & 0x1][$number[$i]];
|
||||
}
|
||||
|
||||
return $total % 10 === 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php namespace ZN\Validation\CreditCard;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
|
||||
interface LuhnAlgorithmInterface
|
||||
{
|
||||
/**
|
||||
* Protected luhn algorithm check
|
||||
*
|
||||
* @param string $number
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function check(?string $number = NULL);
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
<?php namespace ZN\Validation\CreditCard;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
|
||||
class Validator implements ValidatorInterface
|
||||
{
|
||||
/**
|
||||
* Is valid card
|
||||
*
|
||||
* @param string $number
|
||||
* @param string $type = NULL
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function card(string $number, ?string $type = NULL) : bool
|
||||
{
|
||||
$number = preg_replace('/[^0-9]/', '', $number);
|
||||
|
||||
if( empty($type) )
|
||||
{
|
||||
$type = self::creditCardType($number);
|
||||
}
|
||||
|
||||
if( (self::getCardFormats()[$type] ?? NULL) && self::validCard($number, $type) )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is valid cvc
|
||||
*
|
||||
* @param string $cvc
|
||||
* @param string $type
|
||||
*/
|
||||
public static function cvc(string $cvc, ?string $type = NULL) : bool
|
||||
{
|
||||
return ctype_digit($cvc) && (self::getCardFormats()[$type] ?? NULL) && self::validCvcLength($cvc, $type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Is valid date
|
||||
*
|
||||
* @param string $year
|
||||
* @param string $month
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function date(string $year, string $month) : bool
|
||||
{
|
||||
$month = str_pad($month, 2, '0', STR_PAD_LEFT);
|
||||
|
||||
if
|
||||
(
|
||||
! preg_match('/^20\d\d$/', $year) ||
|
||||
! preg_match('/^(0[1-9]|1[0-2])$/', $month) ||
|
||||
( $year < date('Y') || ($year <= date('Y') && $month < date('m')) )
|
||||
)
|
||||
{
|
||||
return false; // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Protected credit card type
|
||||
*
|
||||
* @param string $number
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function creditCardType(string $number) : string
|
||||
{
|
||||
foreach( self::getCardFormats() as $type => $card )
|
||||
{
|
||||
if( preg_match($card['pattern'], $number) )
|
||||
{
|
||||
return $type;
|
||||
}
|
||||
}
|
||||
|
||||
return ''; // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
/**
|
||||
* Protected valid card
|
||||
*
|
||||
* @param string $number
|
||||
* @param string $type
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected static function validCard(string $number, ?string $type = NULL) : bool
|
||||
{
|
||||
return self::validCardFormat($number, $type) && self::validCardNumberLength($number, $type) && self::validLuhnAlgorithm($number, $type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Protected valid card pattern
|
||||
*
|
||||
* @param string $number
|
||||
* @param string $type
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected static function validCardFormat($number, $type) : bool
|
||||
{
|
||||
return preg_match(self::getCardFormats()[$type]['pattern'], $number ?? '');
|
||||
}
|
||||
|
||||
/**
|
||||
* Protected valid card number length
|
||||
*
|
||||
* @param string $number
|
||||
* @param string $type
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected static function validCardNumberLength($number, ?string $type = NULL, $function = 'length') : bool
|
||||
{
|
||||
return in_array(strlen($number ?? ''), self::getCardFormats()[$type][$function]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Protected valid cvc length
|
||||
*
|
||||
* @param int $cvc
|
||||
* @param string $type
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected static function validCvcLength($cvc, ?string $type = NULL) : bool
|
||||
{
|
||||
return self::validCardNumberLength($cvc, $type, 'cvcLength');
|
||||
}
|
||||
|
||||
/**
|
||||
* Protected valid luhn algorithm
|
||||
*
|
||||
* @param string $number
|
||||
* @param string $type
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected static function validLuhnAlgorithm(string $number, ?string $type = NULL) : bool
|
||||
{
|
||||
return self::getCardFormats()[$type]['luhn'] ? LuhnAlgorithm::check($number) : true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Protected get card formats
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected static function getCardFormats() : array
|
||||
{
|
||||
return Formats::getList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php namespace ZN\Validation\CreditCard;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
|
||||
interface ValidatorInterface
|
||||
{
|
||||
/**
|
||||
* Is valid card
|
||||
*
|
||||
* @param string $data
|
||||
* @param string $type = NULL
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function card(string $data, ?string $type = NULL) : bool;
|
||||
|
||||
/**
|
||||
* Is valid cvc
|
||||
*
|
||||
* @param string $cvc
|
||||
* @param string $type
|
||||
*/
|
||||
public static function cvc(string $cvc, ?string $type = NULL) : bool;
|
||||
|
||||
/**
|
||||
* Is valid date
|
||||
*
|
||||
* @param string $year
|
||||
* @param string $month
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function date(string $year, string $month) : bool;
|
||||
}
|
||||
Reference in New Issue
Block a user