static string http_build_query(Context ctx, PhpValue formData, string numericPrefix, string argSeparator, PhpQueryRfc encType, string indexerPrefix) { var result = new StringBuilder(64); var first = true; var enumerator = formData.GetForeachEnumerator(false, default); while (enumerator.MoveNext()) { var key = enumerator.CurrentKey; var value = enumerator.CurrentValue; // the query parameter name (key name) // the parameter name is URL encoded string keyName = key.IsLong(out var l) ? UrlEncode(numericPrefix, encType) + l.ToString() : UrlEncode(key.ToStringOrThrow(ctx), encType); if (indexerPrefix != null) { keyName = indexerPrefix + "%5B" + keyName + "%5D"; // == prefix[key] (url encoded brackets) } // write the query element if (value.IsPhpArray(out var valueArray)) { // value is an array, emit query recursively, use current keyName as an array variable name var queryStr = http_build_query(ctx, valueArray, null, argSeparator, encType, keyName); // emit the query recursively if (string.IsNullOrEmpty(queryStr) == false) { if (!first) { result.Append(argSeparator); } result.Append(queryStr); } } else { // simple value, emit query in a form of (key=value), URL encoded ! if (!first) { result.Append(argSeparator); } result.Append(keyName); result.Append("="); result.Append(UrlEncode(value.ToStringOrThrow(ctx), encType)); // == "keyName=keyValue" } // separator will be used in next loop first = false; } return(result.ToString()); }
private static string http_build_query(Context ctx, PhpValue formData, string numericPrefix, string argSeparator, int encType = 0, string indexerPrefix = null) { var str_builder = new StringBuilder(64); // statistically the length of the result var result = new System.IO.StringWriter(str_builder); bool isNotFirst = false; var enumerator = formData.GetForeachEnumerator(false, default(RuntimeTypeHandle)); while (enumerator.MoveNext()) { var key = enumerator.CurrentKey; var value = enumerator.CurrentValue; // the query parameter name (key name) // the parameter name is URL encoded string keyName = key.IsInteger() ? (numericPrefix != null ? urlencode(numericPrefix) : null) + key.ToLong().ToString() : urlencode(key.ToStringOrThrow(ctx)); if (indexerPrefix != null) { keyName = indexerPrefix + "%5B" + keyName + "%5D"; // == prefix[key] (url encoded brackets) } // write the query element var valueArray = value.ArrayOrNull(); if (valueArray != null) { // value is an array, emit query recursively, use current keyName as an array variable name string queryStr = http_build_query(ctx, (PhpValue)valueArray, null, argSeparator, encType, keyName); // emit the query recursively if (queryStr != null && queryStr.Length > 0) { if (isNotFirst) { result.Write(argSeparator); } result.Write(queryStr); } } else { // simple value, emit query in a form of (key=value), URL encoded ! if (isNotFirst) { result.Write(argSeparator); } if (!value.IsEmpty) { result.Write(keyName + "=" + urlencode(value.ToStringOrThrow(ctx))); // == "keyName=keyValue" } else { result.Write(keyName + "="); // == "keyName=" } } // separator will be used in next loop isNotFirst = true; } result.Flush(); return(str_builder.ToString()); }