Skip to content

Factory

Shesh Ghimire edited this page Jan 18, 2026 · 15 revisions

Factory is one of the core API to generate Payment Cards based on the payload data that follows PaymentCardFactory Schema.

It accepts following data-types to create Cards:

  1. (HIGHLY RECOMMENDED) a JSON file (can be an array of objects or a object of objects).
  2. an indexed or associative PHP array that contains an associative arrays.
  3. a PHP file that has data format described in #2 but returns:
$payload     = 'path/to/cards.json';
$cardFactory = new PaymentCardFactory($payload);

Set/Reset Global Card Class

Concrete Payment Card class can be used that will be instantiated instead of base Payment Card Type class class when factory creates Card. For eg: creating concrete classes such as CreditCard, DebitCard, TravelCard, etc to differentiate it based on your application logic.

💡 Order priority for using concrete class (that accepts two parameters) when Cards are created:

use TheWebSolver\Codegarage\Test\Resource\NapasCard;
use TheWebSolver\Codegarage\PaymentCard\PaymentCardType;
use TheWebSolver\Codegarage\PaymentCard\PaymentCardFactory;

PaymentCardFactory::setGlobalCardClass(NapasCard::class);

$payload     = 'path/to/cards.json';
$cardFactory = new PaymentCardFactory($payload);

foreach ($cardFactory->createCards() as $card) {
    $isNapasCardInstance = $card instanceof NapasCard; // true
}

// Globally set "NapasCard" class can be cleared. It will fallback to
// default "PaymentCardType" class unless global Card class is set again after reset is invoked.
PaymentCardFactory::resetGlobalCardClass();

foreach ($cardFactory->createCards() as $card) {
    $isNapasCardInstance = $card instanceof PaymentCardType; // true
}

Create Payment Card

LazyLoad

$payload     = 'path/to/cards.json';
$cardFactory = new PaymentCardFactory($payload);
$loader      = $cardFactory->lazyload();
$card1       = $loader->current(); // First resolved Card instance.
$card1Key    = $loader->key();     // First key of the resolved Card instance.

$loader->next();             // Move to second Card.
$card2 = $loader->current(); // Second resolved Card instance.
$loader->next();             // Move to third Card.

// ...and so on unless all Cards are loaded. This
// is just for demonstration. In real usage,
// maybe wrap it in a while loop.
if ( $loader->valid() ) {
    $cardN = $loader->current(); // Last resolved Card instance.
    $loader->next();
} else {
    $cardN = $loader->current(); // null.
}

Direct

$payload     = 'path/to/cards.json';
$cardFactory = new PaymentCardFactory($payload);

// Returns Card instance that has payload index key: "mastercard".
$card = $cardFactory->create('mastercard');

Helper Method

This method instantiates factory statically.

$payload     = 'path/to/cards.json';
$cardFactory = PaymentCardFactory::createFromFile($payload);
$lazyload    = $cardFactory->lazyload();

Schema

The payload data must follow the schema described below to create Card instance:

Key Data Type Requirement Polyfill
type string optional Credit Card
classname class-string<PaymentCard> optional PaymentCardType
checkLuhn bool optional true
name string mandatory -
alias string mandatory -
breakpoint list<int> mandatory -
code array{name:string,size:int} mandatory -
length list<int|list<int>> mandatory -
idRange list<int|list<int>> mandatory -

Accepted Parameters

  • Concrete class (either from $payload or factory or base) accepts following two parameters via its constructor in the given order:
    1. $type
    2. $checkLuhn
  • Parameter values can either be from the payload or polyfilled if not found in the payload. These values can then be accessed via their respective getter methods.

Clone this wiki locally