Skip to content
This repository was archived by the owner on Nov 26, 2022. It is now read-only.

Commit ea63d3f

Browse files
committed
improve bugfix
1 parent 3a3f086 commit ea63d3f

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

src/Parser/QueryString.php

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
use function rawurlencode;
3939
use function sprintf;
4040
use function str_replace;
41+
use function str_split;
4142
use function strpos;
4243
use function substr;
4344
use const PHP_QUERY_RFC1738;
@@ -96,21 +97,17 @@ private function __construct()
9697
*/
9798
public static function parse($query, string $separator = '&', int $enc_type = PHP_QUERY_RFC3986): array
9899
{
99-
$query = self::prepareQuery($query, $separator, $enc_type);
100+
$query = self::prepareQuery($query, $enc_type);
100101
if (null === $query) {
101102
return [];
102103
}
103104

104-
if (is_bool($query)) {
105-
return [[$query ? '1' : '0', null]];
106-
}
107-
108105
if ('' === $query) {
109106
return [['', null]];
110107
}
111108

112109
$retval = [];
113-
foreach ((array) explode($separator, $query) as $pair) {
110+
foreach (self::getPairs($query, $separator) as $pair) {
114111
$retval[] = self::parsePair((string) $pair, self::DECODE_PAIR_VALUE);
115112
}
116113

@@ -122,22 +119,24 @@ public static function parse($query, string $separator = '&', int $enc_type = PH
122119
*
123120
* @param null|mixed $query
124121
*
125-
* @throws TypeError If the query is not stringable or the null value
126122
* @throws MalformedUriComponent If the query string is invalid
123+
* @throws TypeError If the query is not stringable or the null value
127124
* @throws UnknownEncoding If the encoding type is invalid
128-
*
129-
* @return null|string|bool
130125
*/
131-
private static function prepareQuery($query, string $separator, int $enc_type)
126+
private static function prepareQuery($query, int $enc_type): ?string
132127
{
133128
if (!isset(self::ENCODING_LIST[$enc_type])) {
134129
throw new UnknownEncoding(sprintf('Unknown Encoding: %s', $enc_type));
135130
}
136131

137-
if (null === $query || is_bool($query)) {
132+
if (null === $query) {
138133
return $query;
139134
}
140135

136+
if (is_bool($query)) {
137+
return true === $query ? '1' : '0';
138+
}
139+
141140
if (!is_scalar($query) && !method_exists($query, '__toString')) {
142141
throw new TypeError(sprintf('The query must be a scalar, a stringable object or the `null` value, `%s` given', gettype($query)));
143142
}
@@ -158,6 +157,19 @@ private static function prepareQuery($query, string $separator, int $enc_type)
158157
return $query;
159158
}
160159

160+
private static function getPairs(string $query, string $separator): array
161+
{
162+
if ('' === $separator) {
163+
return str_split($query);
164+
}
165+
166+
if (false === strpos($query, $separator)) {
167+
return [$query];
168+
}
169+
170+
return (array) explode($separator, $query);
171+
}
172+
161173
/**
162174
* Returns the key/value pair from a query string pair.
163175
*/
@@ -313,17 +325,13 @@ private static function encodeMatches(array $matches): string
313325
*/
314326
public static function extract($query, string $separator = '&', int $enc_type = PHP_QUERY_RFC3986): array
315327
{
316-
$query = self::prepareQuery($query, $separator, $enc_type);
328+
$query = self::prepareQuery($query, $enc_type);
317329
if (null === $query || '' === $query) {
318330
return [];
319331
}
320332

321-
if (is_bool($query)) {
322-
return [$query ? '1' : '0' => ''];
323-
}
324-
325333
$retval = [];
326-
foreach ((array) explode($separator, $query) as $pair) {
334+
foreach (self::getPairs($query, $separator) as $pair) {
327335
$retval[] = self::parsePair((string) $pair, self::PRESERVE_PAIR_VALUE);
328336
}
329337

tests/QueryStringTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,12 @@ public function testParse($query, string $separator, array $expected, int $encod
161161
public function parserProvider(): array
162162
{
163163
return [
164+
'empty separator' => [
165+
'foo',
166+
'',
167+
[['f', null], ['o', null], ['o', null]],
168+
PHP_QUERY_RFC3986,
169+
],
164170
'stringable object' => [
165171
new class() {
166172
public function __toString()

0 commit comments

Comments
 (0)