<?php
 namespace App\Utils\CreditCard; class NumberDetails { private const COMPANIES_CODES = [ 'amex' => 'AX', 'unionpay' => 'UP', 'diners_club' => 'DC', 'discover' => 'DS', 'jcb' => 'JC', 'maestro' => 'maestro', 'mastercard' => 'CA', 'visa_electron' => 'VI', 'visa' => 'VI', 'forbrugsforeningen' => 'forbrugs', 'dankort' => 'dankort', 'elo' => 'EL', 'hipercard' => 'hipercard', ]; private string $originalNumber; private string $cleanNumber; public function __construct(string $originaNumber='') { $this->originalNumber = $originaNumber; $this->cleanNumber = $this->cleanNumber($originaNumber); } private function cleanNumber($originalNumber) { return preg_replace('/[^0-9]/', '', $originalNumber); } static public function create(string $originalNumber='') :self { return new self($originalNumber); } public function getStrippedNumber() :string { return str_replace([' ', '-', '.', '/'], '', $this->originalNumber); } public function getMaskedNumber($maskingPlaceholder='X') :string { $initalCcNumber = $this->getStrippedNumber(); if (strlen($initalCcNumber)<=10) { return $initalCcNumber; } return substr($initalCcNumber, 0, 6) . str_repeat($maskingPlaceholder, strlen($initalCcNumber) -6 -4) . substr($initalCcNumber, -4); } public function isValid() { return $this->getDetails()['valid']; } public function getDetails() :array { $cardDetails = \Freelancehunt\Validators\CreditCard::validCreditCard($this->cleanNumber); return [ 'valid' => $cardDetails['valid'], 'companyName' => $cardDetails['type'], 'companyCode' => self::COMPANIES_CODES[$cardDetails['type']]??null, ]; } } 