Created
December 16, 2015 18:20
-
-
Save schigh/3b52dcf2c7f456545dd3 to your computer and use it in GitHub Desktop.
Complex function wrapper
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 | |
class ParamsWrapper | |
{ | |
public $name; | |
public $email; | |
public $orderDate; | |
public $orderNumber; | |
public $timeZone; | |
public $xorParam = false; | |
public $otherXorParam = false; | |
private $_badConstructor = false; | |
private $errors = []; | |
public function is_valid() | |
{ | |
public function __construct(...$params) | |
{ | |
// do any needed type checking here | |
if ($type_check_fails) { | |
$this->_badConstructor = true; | |
$this->errors[] = 'The constructor failed'; | |
} | |
} | |
// required fields | |
if (!isset( | |
$this->name, | |
$this->email, | |
$this->orderDate, | |
$this->orderNumber | |
)) { | |
$this->errors[] = 'Missing required parameters'; | |
return false; | |
} | |
if (!$this->xorParam xor $this->otherXorParam) { | |
$this->errors[] = 'One must be set, but not both'; | |
return false; | |
} | |
} | |
public function getErrors() | |
{ | |
return $this->errors; | |
} | |
} | |
function doSomethingComplex(ParamsWrapper $params) | |
{ | |
if (!$params->is_valid()) { | |
// handler error | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This would only make sense for methods with very large signatures