public function __call()

in src/Client.php [322:364]


    public function __call($name, $args)
    {
        if (substr($name, -5) === 'Async') {
            $name = substr($name, 0, -5);
            $isAsync = true;
        }

        // api name
        $opName = ucfirst($name);
        $class = Transform\Functions::getTransformClass($opName);
        $fromFunc = "from$opName";
        $toFunc = "to$opName";

        if (
            !\method_exists($class, $fromFunc) ||
            !\method_exists($class, $toFunc)
        ) {
            throw new \BadMethodCallException('Not implement ' . self::class . '::' . $name);
        }

        // args, {Operation}Request request, array options
        $request = isset($args[0]) ? $args[0] : [];
        $options = count($args) > 1 ? $args[1] : [];

        if (!($request instanceof Types\RequestModel)) {
            throw new \InvalidArgumentException('args[0] is not subclass of RequestModel, got ' . \gettype($request));
        }

        if (!\is_array($options)) {
            $options = [];
        }

        // execute
        $input = call_user_func([$class, $fromFunc], $request);
        $promise = $this->client->executeAsync($input, $options)->then(
            function (OperationOutput $output) use ($toFunc, $class) {
                return call_user_func([$class, $toFunc], $output);
            }
        );

        // result
        return !empty($isAsync) ? $promise : $promise->wait();
    }