This file contains 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 | |
if (empty($data["username"])) { | |
throw new \Exception("Username is required."); | |
} | |
if (empty($data["name"])) { | |
$first = $data["first_name"] ?? ''; | |
$last = $data["last_name"] ?? ''; | |
$data["name"] = "$first $last"; | |
} | |
$params = new SearchParams($data['username'], $data['name']); |
This file contains 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 | |
final class SearchParams { | |
private $username; | |
private $name; | |
private function __construct(string $username, string $name) { | |
$this->username = $username; | |
$this->name = $name; | |
} | |
public static function fromArray(array $params) : SearchParams { | |
if (empty($params["username"])) { |
This file contains 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 | |
final class User extends Requester { | |
public function search(SearchParams $params) : array { | |
return $this->request($params->toArray())->toArray(); | |
} | |
} |
This file contains 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 | |
final class Payment { | |
public function subscribe(array $transaction) : array { | |
// 1 | |
$transaction = [ | |
'payment_method' => $transaction['payment_method'] ?? 'credit-card', | |
'amount' => $transaction['total'] ?? $transaction['subtotal'] ?? 0.0, | |
'currency' => $transaction['currency_code'] ?? 'USD' | |
] + $transaction; | |
$model = new TransactionModel(); |
This file contains 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 | |
final class Payment { | |
public function subscribe(PostParams $data) : Transaction { | |
$transaction = Transaction::fromPost($data); | |
$model = new TransactionModel(); | |
$model->create($transaction); | |
$processor = $model->getProcessor($transaction); // a Processor object | |
OlderNewer