Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SearchParams Refactored
<?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"])) {
throw new \Exception("Username is required.");
}
if (empty($params["name"])) {
$first = $params["first_name"] ?? '';
$last = $params["last_name"] ?? '';
$params["name"] = "$first $last";
}
return new static($params["username"], $params["name"]);
}
public function toArray() : array {
return [
"username" => $this->username,
"name" => $this->name
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.