Skip to content

Commit

Permalink
Add StringHelper::split()
Browse files Browse the repository at this point in the history
Co-authored-by: Alexander Makarov <sam@rmcreative.ru>
  • Loading branch information
vjik and samdark committed Dec 15, 2020
1 parent adce6e4 commit 345a476
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -3,7 +3,7 @@

## 1.1.1 under development

- no changes in this release.
- Enh #62: Add method `StringHelper::split` that split a string to array with non-empty lines (vjik)

## 1.1.0 November 13, 2020

Expand Down
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -81,6 +81,10 @@ Overall the helper has the following method groups.
- base64UrlEncode
- base64UrlDecode

### Other

- split

## NumericHelper usage

Numeric helper methods are static so usage is like the following:
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Expand Up @@ -21,10 +21,10 @@
"ext-mbstring": "*"
},
"require-dev": {
"phpunit/phpunit": "^9.4",
"roave/infection-static-analysis-plugin": "^1.5",
"phpunit/phpunit": "^9.5",
"roave/infection-static-analysis-plugin": "^1.6",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^4.2"
"vimeo/psalm": "^4.3"
},
"autoload": {
"psr-4": {
Expand Down
16 changes: 16 additions & 0 deletions src/StringHelper.php
Expand Up @@ -473,4 +473,20 @@ public static function base64UrlDecode(string $input): string
{
return base64_decode(strtr($input, '-_', '+/'));
}

/**
* Split a string to array with non-empty lines.
* Whitespace from the beginning and end of a each line will be stripped.
*
* @param string $string The input string.
* @param string $separator The boundary string. It is a part of regular expression
* so should be taken into account or properly escaped with {@see preg_quote()}.
*
* @return array
*/
public static function split(string $string, string $separator = '\R'): array
{
$string = preg_replace('(^\s*|\s*$)', '', $string);
return preg_split('~\s*' . $separator . '\s*~', $string, -1, PREG_SPLIT_NO_EMPTY);
}
}
54 changes: 54 additions & 0 deletions tests/StringHelperTest.php
Expand Up @@ -367,4 +367,58 @@ public function testReplaceSubstring(string $expected, array $arguments): void
{
$this->assertSame($expected, StringHelper::replaceSubstring(...$arguments));
}

public function dataSplit(): array
{
return [
['', []],
[' ', []],
[" \n\n \n ", []],
[' A B', ['A B']],
['A B ', ['A B']],
['A B', ['A B']],
['Home', ['Home']],
[' Hello World! ', ['Hello World!']],
[
"A \r B \r\r \r C",
['A', 'B', 'C'],
],
[
"A \n B \n\n \n C",
['A', 'B', 'C'],
],
[
"A \r\n B \r\n\r\n \r\n C",
['A', 'B', 'C'],
],
[
"A \v B \v \v C",
['A', 'B', 'C'],
],
[
"A \n Hello World! \n \n C",
['A', 'Hello World!', 'C'],
],
[
"\0\nA\nB",
["\0", 'A', 'B'],
],
];
}

/**
* @dataProvider dataSplit
*
* @param string $string
* @param array $expected
*/
public function testSplit(string $string, array $expected): void
{
$this->assertSame($expected, StringHelper::split($string));
}

public function testSplitWithSeparator(): void
{
$this->assertSame(['A', 'B', 'C'], StringHelper::split(' A 2 B3C', '\d'));
}
}

0 comments on commit 345a476

Please sign in to comment.