Last active
February 14, 2023 08:24
-
-
Save diloabininyeri/fa3df3c1cd362b9304fc469a21268fdc to your computer and use it in GitHub Desktop.
Here is the scenario, let's think that there are different banks, and in a mechanism that will receive requests and process the response, how can we make a structure for all the responses?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * | |
| */ | |
| interface BankTransaction | |
| { | |
| public function getTurkishId(): int; | |
| public function getAmount(): int; | |
| public function getBankId(): int; | |
| public function getTurkishName(): string; | |
| public function toArray(): array; | |
| } | |
| /** | |
| * | |
| */ | |
| interface BankTransactions | |
| { | |
| public function isExists(): bool; | |
| /** | |
| * @return array<BankTransaction> | |
| */ | |
| public function get(): array; | |
| public function toArray(): array; | |
| } | |
| /** | |
| * | |
| */ | |
| class Transactions implements BankTransactions | |
| { | |
| /** | |
| * @var array<BankTransaction> | |
| */ | |
| private array $transactions = []; | |
| public function add(BankTransaction $bankTransaction): void | |
| { | |
| $this->transactions[] = $bankTransaction; | |
| } | |
| public function isExists(): bool | |
| { | |
| return !empty($this->transactions); | |
| } | |
| /** | |
| * @return BankTransaction[] | |
| */ | |
| public function get(): array | |
| { | |
| return $this->transactions; | |
| } | |
| public function toArray(): array | |
| { | |
| return array_map( | |
| static fn(BankTransaction $transaction) => $transaction->toArray(), | |
| $this->transactions | |
| ); | |
| } | |
| } | |
| /** | |
| * | |
| */ | |
| class AkbankResponse implements BankTransaction | |
| { | |
| public function __construct(private readonly array $transaction) | |
| { | |
| } | |
| public function getTurkishId(): int | |
| { | |
| return $this->transaction['turkish_id']; | |
| } | |
| public function getAmount(): int | |
| { | |
| return $this->transaction['amount']; | |
| } | |
| public function getBankId(): int | |
| { | |
| return $this->transaction['bank_id']; | |
| } | |
| public function getTurkishName(): string | |
| { | |
| return $this->transaction['turkish_name']; | |
| } | |
| public function toArray(): array | |
| { | |
| return [ | |
| 'turkish_id' => $this->getTurkishId(), | |
| 'amount' => $this->getAmount(), | |
| 'bank_id' => $this->getBankId(), | |
| 'turkish_name' => $this->getTurkishName(), | |
| ]; | |
| } | |
| } | |
| class XBank implements BankTransaction | |
| { | |
| public function __construct(private readonly array $transactions) | |
| { | |
| } | |
| public function getTurkishId(): int | |
| { | |
| return $this->transactions['id']; | |
| } | |
| public function getAmount(): int | |
| { | |
| return $this->transactions['total']; | |
| } | |
| public function getBankId(): int | |
| { | |
| return $this->transactions['bank_id']; | |
| } | |
| public function getTurkishName(): string | |
| { | |
| return $this->transactions['detail']['name']; | |
| } | |
| public function toArray(): array | |
| { | |
| return [ | |
| 'turkish_id' => $this->getTurkishId(), | |
| 'amount' => $this->getAmount(), | |
| 'bank_id' => $this->getBankId(), | |
| 'turkish_name' => $this->getTurkishName(), | |
| 'detail' => $this->transactions['detail'], | |
| ]; | |
| } | |
| } | |
| interface EachResponseTransaction | |
| { | |
| public function getTransactions(): array; | |
| public function getTransactionMapper(array $transaction): BankTransaction; | |
| } | |
| class XBankRequest implements EachResponseTransaction | |
| { | |
| public function getTransactions(): array | |
| { | |
| return [ | |
| [ | |
| 'total' => 450, | |
| 'bank_id' => 10, | |
| 'id' => 1, | |
| 'detail' => [ | |
| 'name' => 'the bank of Xbank' | |
| ] | |
| ] | |
| ]; | |
| } | |
| public function getTransactionMapper(array $transaction): BankTransaction | |
| { | |
| return new XBank($transaction); | |
| } | |
| } | |
| class AkbankRequest implements EachResponseTransaction | |
| { | |
| public function getTransactions(): array | |
| { | |
| return [ | |
| [ | |
| 'amount' => 45, | |
| 'bank_id' => 1, | |
| 'turkish_id' => 1, | |
| 'turkish_name' => 'Akbank', | |
| ], | |
| [ | |
| 'amount' => 450, | |
| 'bank_id' => 1, | |
| 'turkish_id' => 999, | |
| 'turkish_name' => 'Akbank', | |
| ] | |
| ]; | |
| } | |
| /** | |
| * @param array $transaction | |
| * @return BankTransaction | |
| * | |
| */ | |
| public function getTransactionMapper(array $transaction): BankTransaction | |
| { | |
| return new AkbankResponse($transaction); | |
| } | |
| } | |
| /** | |
| * @param EachResponseTransaction $eachResponseTransaction | |
| * @return BankTransactions | |
| */ | |
| function getTransactions(EachResponseTransaction $eachResponseTransaction): BankTransactions | |
| { | |
| $transactions = new Transactions(); | |
| foreach ($eachResponseTransaction->getTransactions() as $transaction) { | |
| $transactions->add( | |
| $eachResponseTransaction->getTransactionMapper($transaction) | |
| ); | |
| } | |
| return $transactions; | |
| } | |
| $akbankTransactions = getTransactions(new AkbankRequest()); | |
| $akbankTransactions->isExists(); | |
| print_r($akbankTransactions->toArray()); | |
| foreach ($akbankTransactions->get() as $akbankTransaction) { | |
| $akbankTransaction->getAmount(); | |
| $akbankTransaction->getTurkishName(); | |
| $akbankTransaction->getTurkishId(); | |
| } | |
| $xbankTransactions = getTransactions(new XBankRequest()); | |
| $xbankTransactions->isExists(); | |
| print_r($xbankTransactions->toArray()); | |
| foreach ($xbankTransactions->get() as $xbankTransaction) { | |
| $xbankTransaction->getAmount(); | |
| $xbankTransaction->getTurkishName(); | |
| $xbankTransaction->getTurkishId(); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment