-
Notifications
You must be signed in to change notification settings - Fork 0
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:
- (HIGHLY RECOMMENDED) a JSON file (can be an array of objects or a object of objects).
- an indexed or associative PHP array that contains an associative arrays.
- a PHP file that has data format described in #2 but returns:
- data itself (RECOMMENDED).
- a closure.
- an invocable anonymous class.
$payload = 'path/to/cards.json';
$cardFactory = new PaymentCardFactory($payload);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:
- The $payload data's
classnameSchema Key value - Globally set to the factory if not found in $payload
- base Payment Card Type class if not found in factory
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
}$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.
}$payload = 'path/to/cards.json';
$cardFactory = new PaymentCardFactory($payload);
// Returns Card instance that has payload index key: "mastercard".
$card = $cardFactory->create('mastercard');This method instantiates factory statically.
$payload = 'path/to/cards.json';
$cardFactory = PaymentCardFactory::createFromFile($payload);
$lazyload = $cardFactory->lazyload();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 | - |
- Concrete class (either from $payload or factory or base) accepts following two parameters via its constructor in the given order:
- $type
- $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.