protected function typecast($argument, $toString = true, $nested = false, $depth = 4)
    {
        if ($nested) {
            $depth--;
            if ($argument instanceof PheryResponse) {
                $argument = array('PR' => $argument->process_merged());
            } elseif ($argument instanceof PheryFunction) {
                $argument = array('PF' => $argument->compile());
            } elseif ($depth > 0 && is_array($argument)) {
                foreach ($argument as $name => $arg) {
                    $argument[$name] = $this->typecast($arg, $toString, $nested, $depth);
                }
            }
        }

        if ($toString && !empty($argument)) {
            if (is_string($argument) && ctype_digit($argument)) {
                if ($this->config['convert_integers'] === true) {
                    $argument = (int)$argument;
                }
            } elseif (is_object($argument) && $this->config['typecast_objects'] === true) {
                $class = get_class($argument);
                if ($class !== false) {
                    $rc = new ReflectionClass(get_class($argument));
                    if ($rc->hasMethod('__toString')) {
                        $argument = "{$argument}";
                    } else {
                        $argument = json_decode(json_encode($argument), true);
                    }
                } else {
                    $argument = json_decode(json_encode($argument), true);
                }
            }
        }

        return $argument;
    }