-
Notifications
You must be signed in to change notification settings - Fork 0
Factory
Factory is one of the core API to generate Payment Cards based on the payload data that follows CardFactory 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 CardFactory($payload);
$payload = 'path/to/cards.json';
$cardFactory = (new CardFactory())->withPayload($payload);
Concrete Payment Card class can be used that will be instantiated instead of the default CardType class when factory creates Card.
💡 Order priority for using concrete class (that accepts two parameters) when Cards are created:
- The $payload data's
classname
Schema Key value - Globally set to the factory if not found in $payload
- CardType base class if not found in factory
use TheWebSolver\Codegarage\PaymentCard\CardType;
use TheWebSolver\Codegarage\Test\Resource\NapasCard;
use TheWebSolver\Codegarage\PaymentCard\CardFactory;
CardFactory::setGlobalCardClass(NapasCard::class);
$payload = 'path/to/cards.json';
$cardFactory = new CardFactory($payload);
foreach ($cardFactory->createCards() as $card) {
$isNapasCardInstance = $card instanceof NapasCard; // true
}
// Globally set "NapasCard" class can be cleared. It will fallback to
// default "CardType" class unless global Card class is set again.
CardFactory::resetGlobalCardClass();
foreach ($cardFactory->createCards() as $card) {
$isNapasCardInstance = $card instanceof CardType; // true
}
$payload = 'path/to/cards.json';
$cardFactory = new CardFactory($payload);
if $payload is an object of objects, $lazyLoadedCards
can access the key also.
$lazyLoadedCards = $cardFactory->lazyLoadCards();
$card1 = $lazyLoadedCards->current(); // First resolved Card instance.
$card1Key = $lazyLoadedCards->key(); // First key of the resolved Card instance.
$lazyLoadedCards->next(); // Move to second Card.
$card2 = $lazyLoadedCards->current(); // Second resolved Card instance.
$lazyLoadedCards->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 ( $lazyLoadedCards->valid() ) {
$cardN = $lazyLoadedCards->current(); // Last resolved Card instance.
$lazyLoadedCards->next();
} else {
$cardN = $lazyLoadedCards->current(); // null.
}
// Without preserving keys.
$lazyLoadedCards = $cardFactory->lazyLoadCards(preserveKeys: false);
$payload = 'path/to/cards.json';
$cardFactory = new CardFactory($payload);
if $payload is an object of objects, then keys will be preserved and $cards will be an associative array.
$cards = $cardFactory->createCards();
But this feature can be disabled by passing parameter preserveKeys
value as false
.
$cards = $cardFactory->createCards(preserveKeys: false);
$phpFilePath = 'path/to/phpPayload.php';
$cards = CardFactory::createFromPhpFile(path: $phpFilePath);
$jsonFilePath = 'path/to/jsonPayload.json';
$cards = CardFactory::createFromJsonFile(path: $jsonFilePath);
// Alternatively, use:
$cards = CardFactory::createFromFile(path: $phpOrJsonFilePath);
// For lazyload version (applies any of the above static methods), use:
$lazyLoadedCards = CardFactory::createFromFile(path: $filePath, lazyload: true);
// For lazyload without keys version (applies any of the above static methods), use:
$lazyLoadedCards = CardFactory::createFromFile(path: $filePath, preserveKeys: false, lazyload: true);
$payload = 'path/to/cards.json';
$cardFactory = new CardFactory($payload);
$card = $cardFactory->createCard(); // Using $payload first block of JSON object data.
if $payload is an object of objects, then object key can be used to get a specific Card.
$payload = 'path/to/cards.json';
$cardFactory = new CardFactory($payload);
$card = $cardFactory->createCard('mastercard'); // Using $payload block of JSON object data whose key is "mastercard".
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< CardInterface>
|
optional | CardType |
checkLuhn | bool |
optional | true |
name | string |
mandatory | - |
alias | string |
mandatory | - |
breakpoint | array<string|int> |
mandatory | - |
code | array{0:string,1:int} |
mandatory | - |
length | array<int,string|int|array<int,string|int>> |
mandatory | - |
idRange | array<int,string|int|array<int,string|int>> |
mandatory | - |
- Concrete class (either from $payload or factory or base) accepts following two parameters via its constructor:
- $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.