Skip to content

Instantly share code, notes, and snippets.

@ackintosh
Last active July 27, 2018 02:29
Show Gist options
  • Save ackintosh/9fcadbb8b09b0ff89915067df1589949 to your computer and use it in GitHub Desktop.
Save ackintosh/9fcadbb8b09b0ff89915067df1589949 to your computer and use it in GitHub Desktop.
issue
<?php
// You only have to pass the necessary arguments
WrapperForExampleApi::search(['arg2' => 'x', 'arg4' => 'x']);
/**
* hand-written wrapper class for ExampleApi
*/
class WrapperForExampleApi
{
public static function search()
{
$args = self::defaultArguments();
$passed = func_get_args()[0];
$merged = array_merge($args, $passed);
return call_user_func_array([new ExampleApi, 'search'], $merged);
}
private static function defaultArguments()
{
$exampleApi = new ExampleApi;
$method = new \ReflectionMethod($exampleApi, 'search');
$arguments = [];
foreach ($method->getParameters() as $parameter) {
$arguments[$parameter->getName()] = $parameter->getDefaultValue();
}
return $arguments;
}
}
/**
* auto-generated api class
*/
class ExampleApi
{
public function search($arg1 = null, $arg2 = null, $arg3 = null, $arg4 = null)
{
var_dump('$arg1 = ' . $arg1);
var_dump('$arg2 = ' . $arg2);
var_dump('$arg3 = ' . $arg3);
var_dump('$arg4 = ' . $arg4);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment