Skip to content

Commit

Permalink
Fix StringHelper::parseStr() with encoded brackets
Browse files Browse the repository at this point in the history
  • Loading branch information
ElGigi committed Jan 18, 2022
1 parent 4472b85 commit 9eacc46
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. This projec
to [Semantic Versioning] (http://semver.org/). For change log format,
use [Keep a Changelog] (http://keepachangelog.com/).

## [1.6.1] - 2022-01-18

### Fixed

- `StringHelper::parseStr()` with encoded brackets

## [1.6.0] - 2022-01-10

### Added
Expand Down
2 changes: 1 addition & 1 deletion src/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ public static function parseStr(string $str, bool $keepDots = true): array
$result = [];
parse_str($variable, $result);

$split = preg_split('/(\[.*\])?=/', $variable, 2);
$split = preg_split('/((%5B|\[).*(%5D|\]))?=/i', $variable, 2);

if (false === is_array($split)) {
continue;
Expand Down
28 changes: 28 additions & 0 deletions tests/StringHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@ public function testParseStr()
['first' => 'value', 'arr' => ['foo bar', 'baz']],
StringHelper::parseStr('first=value&arr[]=foo+bar&arr[]=baz')
);
$this->assertEquals(
['first' => 'value', 'arr' => ['foo bar', 'baz']],
StringHelper::parseStr('first=value&arr%5B%5D=foo+bar&arr%5b%5d=baz')
);
$this->assertEquals(
['first' => 'value', 'arr' => [1 => 'foo bar', 2 => 'baz']],
StringHelper::parseStr('first=value&arr[1]=foo+bar&arr[2]=baz')
);
$this->assertEquals(
['foo' => 'baz'],
StringHelper::parseStr('foo=bar&foo=baz')
Expand All @@ -148,6 +156,14 @@ public function testParseStr()
['foo' => 'qux', 'bar' => ''],
StringHelper::parseStr('foo=qux&bar')
);
$this->assertEquals(
['first' => 'value', 'arr' => ['foo' => 'bar', 'qux' => 'baz']],
StringHelper::parseStr('first=value&arr%5Bfoo%5D=bar&arr[qux]=baz')
);
$this->assertEquals(
['first' => 'value', 'arr' => ['foo bar', 'baz']],
StringHelper::parseStr('first=value&arr%5B%5D=foo+bar&arr%5b%5d=baz')
);

$this->assertEquals([], StringHelper::parseStr(''));
$this->assertEquals([], StringHelper::parseStr('=foo'));
Expand All @@ -163,6 +179,10 @@ public function testParseStr_dontKeepDots()
['first' => 'value', 'arr' => ['foo bar', 'baz']],
StringHelper::parseStr('first=value&arr[]=foo+bar&arr[]=baz', false)
);
$this->assertEquals(
['first' => 'value', 'arr' => ['foo bar', 'baz']],
StringHelper::parseStr('first=value&arr%5B%5D=foo+bar&arr%5b%5d=baz', false)
);
$this->assertEquals(
['foo' => 'baz'],
StringHelper::parseStr('foo=bar&foo=baz', false)
Expand All @@ -175,6 +195,14 @@ public function testParseStr_dontKeepDots()
['foo' => 'qux', 'bar' => ''],
StringHelper::parseStr('foo=qux&bar', false)
);
$this->assertEquals(
['first' => 'value', 'arr' => ['foo' => 'bar', 'qux' => 'baz']],
StringHelper::parseStr('first=value&arr%5Bfoo%5D=bar&arr[qux]=baz', false)
);
$this->assertEquals(
['first' => 'value', 'arr' => ['foo bar', 'baz']],
StringHelper::parseStr('first=value&arr%5B%5D=foo+bar&arr%5b%5d=baz', false)
);

$this->assertEquals([], StringHelper::parseStr('', false));
$this->assertEquals([], StringHelper::parseStr('=foo', false));
Expand Down

0 comments on commit 9eacc46

Please sign in to comment.