Skip to content

Commit a7f4e3b

Browse files
willrowekvz
authored andcommitted
Add encType argument to the PHP http_build_query function (locutusjs#361)
1 parent 1414040 commit a7f4e3b

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/php/url/http_build_query.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = function http_build_query (formdata, numericPrefix, argSeparator) { // eslint-disable-line camelcase
1+
module.exports = function http_build_query (formdata, numericPrefix, argSeparator, encType) { // eslint-disable-line camelcase
22
// discuss at: http://locutus.io/php/http_build_query/
33
// original by: Kevin van Zonneveld (http://kvz.io)
44
// improved by: Legaev Andrey
@@ -9,14 +9,28 @@ module.exports = function http_build_query (formdata, numericPrefix, argSeparato
99
// input by: Dreamer
1010
// bugfixed by: Brett Zamir (http://brett-zamir.me)
1111
// bugfixed by: MIO_KODUKI (http://mio-koduki.blogspot.com/)
12+
// improved by: Will Rowe
1213
// note 1: If the value is null, key and value are skipped in the
1314
// note 1: http_build_query of PHP while in locutus they are not.
1415
// example 1: http_build_query({foo: 'bar', php: 'hypertext processor', baz: 'boom', cow: 'milk'}, '', '&')
1516
// returns 1: 'foo=bar&php=hypertext+processor&baz=boom&cow=milk'
1617
// example 2: http_build_query({'php': 'hypertext processor', 0: 'foo', 1: 'bar', 2: 'baz', 3: 'boom', 'cow': 'milk'}, 'myvar_')
1718
// returns 2: 'myvar_0=foo&myvar_1=bar&myvar_2=baz&myvar_3=boom&php=hypertext+processor&cow=milk'
19+
// example 3: http_build_query({foo: 'bar', php: 'hypertext processor', baz: 'boom', cow: 'milk'}, '', '&', 'PHP_QUERY_RFC3986')
20+
// returns 3: 'foo=bar&php=hypertext%20processor&baz=boom&cow=milk'
1821

19-
var urlencode = require('../url/urlencode')
22+
var encodeFunc
23+
24+
switch (encType) {
25+
case 'PHP_QUERY_RFC3986':
26+
encodeFunc = require('../url/rawurlencode')
27+
break
28+
29+
case 'PHP_QUERY_RFC1738':
30+
default:
31+
encodeFunc = require('../url/urlencode')
32+
break
33+
}
2034

2135
var value
2236
var key
@@ -39,7 +53,7 @@ module.exports = function http_build_query (formdata, numericPrefix, argSeparato
3953
}
4054
return tmp.join(argSeparator)
4155
} else if (typeof val !== 'function') {
42-
return urlencode(key) + '=' + urlencode(val)
56+
return encodeFunc(key) + '=' + encodeFunc(val)
4357
} else {
4458
throw new Error('There was an error processing for http_build_query().')
4559
}

test/languages/php/url/test-http_build_query.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,10 @@ describe('src/php/url/http_build_query.js (tested in test/languages/php/url/test
1919
expect(result).to.deep.equal(expected)
2020
done()
2121
})
22+
it('should pass example 3', function (done) {
23+
var expected = 'foo=bar&php=hypertext%20processor&baz=boom&cow=milk'
24+
var result = http_build_query({foo: 'bar', php: 'hypertext processor', baz: 'boom', cow: 'milk'}, '', '&', 'PHP_QUERY_RFC3986')
25+
expect(result).to.deep.equal(expected)
26+
done()
27+
})
2228
})

0 commit comments

Comments
 (0)