WC_API_Server::sort_callback_params PHP Method

sort_callback_params() protected method

Takes a callback and a list of available params, then filters and sorts by the parameters the method actually needs, using the Reflection API
Since: 2.1
protected sort_callback_params ( callable | array $callback, array $provided ) : array
$callback callable | array the endpoint callback
$provided array the provided request parameters
return array
    protected function sort_callback_params($callback, $provided)
    {
        if (is_array($callback)) {
            $ref_func = new ReflectionMethod($callback[0], $callback[1]);
        } else {
            $ref_func = new ReflectionFunction($callback);
        }
        $wanted = $ref_func->getParameters();
        $ordered_parameters = array();
        foreach ($wanted as $param) {
            if (isset($provided[$param->getName()])) {
                // We have this parameters in the list to choose from
                $ordered_parameters[] = is_array($provided[$param->getName()]) ? array_map('urldecode', $provided[$param->getName()]) : urldecode($provided[$param->getName()]);
            } elseif ($param->isDefaultValueAvailable()) {
                // We don't have this parameter, but it's optional
                $ordered_parameters[] = $param->getDefaultValue();
            } else {
                // We don't have this parameter and it wasn't optional, abort!
                return new WP_Error('woocommerce_api_missing_callback_param', sprintf(__('Missing parameter %s', 'woocommerce'), $param->getName()), array('status' => 400));
            }
        }
        return $ordered_parameters;
    }