Last active
June 20, 2018 17:39
-
-
Save young-steveo/6a40c1a2f2c386e297b2d70a63aadff3 to your computer and use it in GitHub Desktop.
SearchParams Refactored
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 | |
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