Skip to content

Instantly share code, notes, and snippets.

@schigh
Created December 16, 2015 18:20
Show Gist options
  • Save schigh/3b52dcf2c7f456545dd3 to your computer and use it in GitHub Desktop.
Save schigh/3b52dcf2c7f456545dd3 to your computer and use it in GitHub Desktop.
Complex function wrapper
<?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
}
}
@schigh
Copy link
Author

schigh commented Dec 16, 2015

This would only make sense for methods with very large signatures

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment