Prepare request params for POST request, convert to multipart when needed

@param array $data

@return array
2395
private function prepareRequestParams(array $data)
    {
        $has_resource = false;
        $multipart = [];

        foreach ($data as $key => $value) {
            if (!is_array($value)) {
                $multipart[] = ['name' => $key, 'contents' => $value];
                continue;
            }

            foreach ($value as $multiKey => $multiValue) {
                is_resource($multiValue) && $has_resource = true;
                $multiName = $key . '[' . $multiKey . ']' . (is_array($multiValue) ? '[' . key($multiValue) . ']' : '') . '';
                $multipart[] = ['name' => $multiName, 'contents' => (is_array($multiValue) ? reset($multiValue) : $multiValue)];
            }
        }

        if ($has_resource) {
            return ['multipart' => $multipart];
        }

        return ['form_params' => $data];
    }
282848
private static function postData(string $uri, array $data): array
    {
        self::initialize();

        $response = self::$client->post('api/' . self::$version . '/' . $uri, [
            'form_params' => $data,
        ]);

        return json_decode($response->getBody(), TRUE);
    }
849844
protected function prepareData($data)
    {
        $data['auth_key'] = $this->api_key;
        $data['format'] = $this->api_format;
        $data['pretty'] = $this->api_pretty;

        return $data;
    }
482792
public function loadArray($data)
    {
        if (\is_string($data) && is_file($data)) {
            $data = require $data;
        }

        if (!\is_array($data)) {
            throw new \InvalidArgumentException('param type error! must is array.');
        }

        return $this->bindData($this->data, $data);
    }
577992
public static function getManualInputPosts($data)
    {
        $posts = array();

        foreach ($data as $key => $item) {
            $posts[] = array_merge((array)$item, array(
                'ID' => $key,
                'post_name' => $key,
                'post_excerpt' => $item->post_content
            ));
        }

        $posts = json_decode(json_encode($posts));

        return $posts;
    }
273730
protected function prepareData(array $data)
	{
		$data = array_merge(array(
			'name'       => null,
			'email'      => null,
			'username'   => null,
			'id'         => null,
			'text'       => null,
			'ip'         => null,
			'user_agent' => null
		), $data);

		$data['original_text'] = $data['text'];

		if ($this->stringProcessor)
		{
			$data['text'] = $this->stringProcessor->prepare($data['text']);
		}

		return $data;
	}
873825
protected function prepareRawPayload($data)
    {
        $this->clearRequestParams();
        $this->addHeader('Content-Length', strlen($data));
        $this->addOption(CURLOPT_POSTFIELDS, $data);
    }
674158
private function prepareParameters($data)
    {
        // Parameters are sorted by name, using lexicographical byte value
        // ordering. Ref: Spec: 9.1.1 (1).
        uksort($data, 'strcmp');

        foreach ($data as $key => $value) {
            if ($value === null) {
                unset($data[$key]);
            }
        }

        return $data;
    }
875388
protected function formatWeappMessage(array $data = [])
    {
        $params = $this->baseFormat($data, $this->weappMessage);

        $params['data'] = $this->formatData($params['data'] ?? []);

        return $params;
    }
515932
public function buildPostData($data)
    {
        $binary_data = false;
        if (is_array($data)) {
            // Return JSON-encoded string when the request's content-type is JSON.
            if (isset($this->headers['Content-Type']) &&
                preg_match($this->jsonPattern, $this->headers['Content-Type'])) {
                $json_str = json_encode($data);
                if (!($json_str === false)) {
                    $data = $json_str;
                }
            } else {
                // Manually build a single-dimensional array from a multi-dimensional array as using curl_setopt($ch,
                // CURLOPT_POSTFIELDS, $data) doesn't correctly handle multi-dimensional arrays when files are
                // referenced.
                if (self::is_array_multidim($data)) {
                    $data = self::array_flatten_multidim($data);
                }

                // Modify array values to ensure any referenced files are properly handled depending on the support of
                // the @filename API or CURLFile usage. This also fixes the warning "curl_setopt(): The usage of the
                // @filename API for file uploading is deprecated. Please use the CURLFile class instead". Ignore
                // non-file values prefixed with the @ character.
                foreach ($data as $key => $value) {
                    if (is_string($value) && strpos($value, '@') === 0 && is_file(substr($value, 1))) {
                        $binary_data = true;
                        if (class_exists('CURLFile')) {
                            $data[$key] = new \CURLFile(substr($value, 1));
                        }
                    } elseif ($value instanceof \CURLFile) {
                        $binary_data = true;
                    }
                }
            }
        }

        if (!$binary_data && (is_array($data) || is_object($data))) {
            $data = http_build_query($data, '', '&');
        }

        return $data;
    }
1225758
public function parseData(array $data) : array
    {
        if ($this->getTransformer() instanceof RequestTransformerContract) {
            return $this->getTransformer()->transformRequestData($data);
        }

        return $data;
    }
27521
public function multipart(array $data = [], $files = false) {

        if(is_array($files)) {

            foreach($files as $name => $file) {

                $data[$name] = $this->file($file);

            }

        }

        $this->params = $data;

        return $this->header(['Content-Type' => 'multipart/form-data']);
    }
258565
protected function postfields($data)
    {
        if (is_array($data)) {
            //if mulitdimensional array
            if ($this->isArrayMultiDim($data)) {
                // build bultidimensial query
                $data = $this->httpBuildMultiQuery($data);
            } else {
                //loop through array
                foreach ($data as $key => $value) {

                    // Fix "Notice: Array to string conversion" when $value in
                    // curl_setopt($ch, CURLOPT_POSTFIELDS, $value) is an array
                    // that contains an empty array.
                    if (is_array($value) && empty($value)) {
                        $data[$key] = '';
                    }
                    // Fix "curl_setopt(): The usage of the @filename API for
                    // file uploading is deprecated. Please use the CURLFile
                    // class instead".
                    elseif (is_string($value) && strpos($value, '@') === 0) {
                        // add file
                        $data[$key] = new CURLFile(substr($value, 1));
                    }
                }
            }
        }

        return $data;
    }
1028049
private function prepareContent($data)
    {
        $content = '';

        foreach ($data as $row) {
            $content .= $this->convertToJson($row) . static::ROW_DELIMITER;
        }

        return $content;
    }
1260209
protected function parseQueryString($data) {
        $params = explode('&', $data);

        foreach ($params as $param) {
            $parts = explode('=', $param, 2);
            $name  = trim(urldecode($parts[0])); if (!strlen($name)) continue;
            //$name  = str_replace(['.', ' '], '_', $name);                             // replace as the PHP implementation does
            $value = sizeof($parts)==1 ? '' : urldecode($parts[1]);

            // TODO: process multi-dimensional arrays

            if (($open=strpos($name, '[')) && ($close=strpos($name, ']')) && strlen($name)==$close+1) {
                // name is an array index
                $name = trim(substr($name, 0, $open));
                $key  = trim(substr($name, $open+1, $close-$open-1));

                if (!strlen($key)) {
                    $_GET[$name][] = $_REQUEST[$name][] = $value;
                }
                else {
                    $_GET[$name][$key]                                    = $value;
                    !isset($_POST[$name][$key]) && $_REQUEST[$name][$key] = $value;   // GET must not over-write POST
                }
            }
            else {
                // name is not an array index
                $_GET[$name]                              = $value;
                !isset($_POST[$name]) && $_REQUEST[$name] = $value;                  // GET must not over-write POST
            }
        }
    }
859833
public function decode($data)
    {
        $add = $convert = array();

        if (is_array($data)) {
            $params = $data;
        } else {
            $parts = explode(';', $data, 2);
            if (isset($parts[0]) && (strpos($parts[0], '=') === false)) {
                $this->setContentParamValue($parts[0]);
                $param = isset($parts[1]) ? $parts[1] : null;
            } else {
                $param = $data;
            }

            if (empty($param)) {
                $params = array();
            } else {
                $decode = new Horde_Mime_ContentParam_Decode();
                $params = $decode->decode($param);
            }
        }

        $to_add = array();

        foreach ($params as $name => $val) {
            /* Asterisk at end indicates encoded value. */
            if (substr($name, -1) == '*') {
                $name = substr($name, 0, -1);
                $encoded = true;
            } else {
                $encoded = false;
            }

            /* This asterisk indicates continuation parameter. */
            if ((($pos = strrpos($name, '*')) !== false) &&
                is_numeric($order = substr($name, $pos + 1))) {
                $name = substr($name, 0, $pos);
                $to_add[Horde_String::lower($name)][$order] = $val;
            } else {
                $to_add[$name] = array($val);
            }

            if ($encoded) {
                $convert[$name] = true;
            }
        }

        foreach ($to_add as $key => $val) {
            ksort($val);
            $add[$key] = implode('', $val);
        }

        foreach (array_keys($convert) as $name) {
            $val = $add[$name];
            $quote = strpos($val, "'");

            if ($quote === false) {
                $add[$name] = urldecode($val);
            } else {
                $orig_charset = substr($val, 0, $quote);
                if (Horde_String::lower($orig_charset) == 'iso-8859-1') {
                    $orig_charset = 'windows-1252';
                }

                /* Ignore language. */
                $quote = strpos($val, "'", $quote + 1);
                substr($val, $quote + 1);
                $add[$name] = Horde_String::convertCharset(
                    urldecode(substr($val, $quote + 1)),
                    $orig_charset,
                    'UTF-8'
                );
            }
        }

        /* MIME parameters are supposed to be encoded via RFC 2231, but many
         * mailers do RFC 2045 encoding instead. However, if we see at least
         * one RFC 2231 encoding, then assume the sending mailer knew what
         * it was doing and didn't send any parameters RFC 2045 encoded. */
        if (empty($convert)) {
            foreach ($add as $key => $val) {
                $add[$key] = Horde_Mime::decode($val);
            }
        }

        if (count($add)) {
            foreach ($add as $key => $val) {
                /* When parsing a content-param string, lowercase all
                 * parameter names to normalize. Only maintain case of
                 * parameters explicitly added by calling code. */
                $this[Horde_String::lower($key)] = $val;
            }
        } elseif (is_string($data)) {
            $this->setContentParamValue($parts[0]);
        }
    }
1163072
protected static function saveExtraParams(array $data)
    {
        $final_data = array();
        if (count($data) > 0) {
            Logger::log('Saving extra configuration parameters');
            foreach ($data as $key => $value) {
                if (null !== $value || $value !== '') {
                    $final_data[$key] = $value;
                }
            }
        }
        return $final_data;
    }