Skip to content

Commit

Permalink
New method StringHelper::parseStr() which keep dots in variable name
Browse files Browse the repository at this point in the history
  • Loading branch information
ElGigi committed Oct 18, 2021
1 parent 9cca8aa commit 3a839fb
Show file tree
Hide file tree
Showing 5 changed files with 101 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.3.0] - 2021-10-18

### Added

- New method `StringHelper::parseStr()` which keep dots in variable name

## [1.2.0] - 2021-05-03

### Changed
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ Many PHP functions used in the Berlioz framework, which you can use in your deve

Truncate string.

- `b_parse_str(string $str, bool $keepDots = true): array`

Parses the string into variables.

- `b_pascal_case(string $str): string`

Get pascal case convention of string.
Expand Down
39 changes: 39 additions & 0 deletions src/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,45 @@ public static function truncate(
return $str;
}

/**
* Parses the string into variables.
*
* Similar to `parse_str()` function but keep dots and spaces.
*
* @param string $str
* @param bool $keepDots
*
* @return array
* @see https://www.php.net/manual/function.parse-str.php
*/
public static function parseStr(string $str, bool $keepDots = true): array
{
if (false === $keepDots) {
$result = [];
parse_str($str, $result);

return $result;
}

$final = [];
$variables = explode('&', $str);

foreach ($variables as $variable) {
$result = [];
parse_str($variable, $result);

$split = preg_split('/(\[.*\])?=/', $variable, 2);

if (false === is_array($split)) {
continue;
}

$final = b_array_merge_recursive($final, [$split[0] => reset($result)]);
}

return $final;
}

/**
* Get pascal case convention of string.
*
Expand Down
16 changes: 16 additions & 0 deletions src/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,22 @@ function b_str_truncate(
return StringHelper::truncate($str, $nbCharacters, $where, $separator);
}

/**
* Parses the string into variables.
*
* Similar to `parse_str()` function but keep dots and spaces.
*
* @param string $str
* @param bool $keepDots
*
* @return array
* @see https://www.php.net/manual/function.parse-str.php
*/
function b_parse_str(string $str, bool $keepDots = true): array
{
return StringHelper::parseStr($str, $keepDots);
}

/**
* Get pascal case convention of string.
*
Expand Down
37 changes: 36 additions & 1 deletion tests/StringHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ public function testRemoveAccents()
{
$this->assertEquals('This is a wrong sentence!', StringHelper::removeAccents('Thîs îs à wrong séntènce!'));
$this->assertEquals('I a lublu PHP!', StringHelper::removeAccents('И я люблю PHP!'));
$this->assertEquals('This is a wrong sentence!', StringHelper::removeAccents("Thîs îs à wrong s\xC3\xA9ntènce!"));
$this->assertEquals(
'This is a wrong sentence!',
StringHelper::removeAccents("Thîs îs à wrong s\xC3\xA9ntènce!")
);
$this->assertEquals('', StringHelper::removeAccents(utf8_decode("Thîs îs à wrong s\xC3\xA9ntènce!")));
}

Expand Down Expand Up @@ -123,6 +126,38 @@ public function testTruncate()
);
}

public function testParseStr()
{
$this->assertEquals(
['foo.bar' => ['foo.bar' => ['baz']], 'foo_bar' => 'qux'],
StringHelper::parseStr('foo.bar[foo.bar][0]=baz&foo_bar=qux')
);
$this->assertEquals(
['first' => 'value', 'arr' => ['foo bar', 'baz']],
StringHelper::parseStr('first=value&arr[]=foo+bar&arr[]=baz')
);
$this->assertEquals(
['foo' => 'baz'],
StringHelper::parseStr('foo=bar&foo=baz')
);
}

public function testParseStr_dontKeepDots()
{
$this->assertEquals(
['foo_bar' => 'qux'],
StringHelper::parseStr('foo.bar[foo.bar][0]=baz&foo_bar=qux', false)
);
$this->assertEquals(
['first' => 'value', 'arr' => ['foo bar', 'baz']],
StringHelper::parseStr('first=value&arr[]=foo+bar&arr[]=baz', false)
);
$this->assertEquals(
['foo' => 'baz'],
StringHelper::parseStr('foo=bar&foo=baz', false)
);
}

public function testPascalCase()
{
$this->assertEquals('MyVariable123foo', StringHelper::pascalCase('my_variable_123foo'));
Expand Down

0 comments on commit 3a839fb

Please sign in to comment.