Skip to content

Commit 345a476

Browse files
vjiksamdark
andauthored
Add StringHelper::split()
Co-authored-by: Alexander Makarov <sam@rmcreative.ru>
1 parent adce6e4 commit 345a476

File tree

5 files changed

+78
-4
lines changed

5 files changed

+78
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
## 1.1.1 under development
55

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

88
## 1.1.0 November 13, 2020
99

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ Overall the helper has the following method groups.
8181
- base64UrlEncode
8282
- base64UrlDecode
8383

84+
### Other
85+
86+
- split
87+
8488
## NumericHelper usage
8589

8690
Numeric helper methods are static so usage is like the following:

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
"ext-mbstring": "*"
2222
},
2323
"require-dev": {
24-
"phpunit/phpunit": "^9.4",
25-
"roave/infection-static-analysis-plugin": "^1.5",
24+
"phpunit/phpunit": "^9.5",
25+
"roave/infection-static-analysis-plugin": "^1.6",
2626
"spatie/phpunit-watcher": "^1.23",
27-
"vimeo/psalm": "^4.2"
27+
"vimeo/psalm": "^4.3"
2828
},
2929
"autoload": {
3030
"psr-4": {

src/StringHelper.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,4 +473,20 @@ public static function base64UrlDecode(string $input): string
473473
{
474474
return base64_decode(strtr($input, '-_', '+/'));
475475
}
476+
477+
/**
478+
* Split a string to array with non-empty lines.
479+
* Whitespace from the beginning and end of a each line will be stripped.
480+
*
481+
* @param string $string The input string.
482+
* @param string $separator The boundary string. It is a part of regular expression
483+
* so should be taken into account or properly escaped with {@see preg_quote()}.
484+
*
485+
* @return array
486+
*/
487+
public static function split(string $string, string $separator = '\R'): array
488+
{
489+
$string = preg_replace('(^\s*|\s*$)', '', $string);
490+
return preg_split('~\s*' . $separator . '\s*~', $string, -1, PREG_SPLIT_NO_EMPTY);
491+
}
476492
}

tests/StringHelperTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,4 +367,58 @@ public function testReplaceSubstring(string $expected, array $arguments): void
367367
{
368368
$this->assertSame($expected, StringHelper::replaceSubstring(...$arguments));
369369
}
370+
371+
public function dataSplit(): array
372+
{
373+
return [
374+
['', []],
375+
[' ', []],
376+
[" \n\n \n ", []],
377+
[' A B', ['A B']],
378+
['A B ', ['A B']],
379+
['A B', ['A B']],
380+
['Home', ['Home']],
381+
[' Hello World! ', ['Hello World!']],
382+
[
383+
"A \r B \r\r \r C",
384+
['A', 'B', 'C'],
385+
],
386+
[
387+
"A \n B \n\n \n C",
388+
['A', 'B', 'C'],
389+
],
390+
[
391+
"A \r\n B \r\n\r\n \r\n C",
392+
['A', 'B', 'C'],
393+
],
394+
[
395+
"A \v B \v \v C",
396+
['A', 'B', 'C'],
397+
],
398+
[
399+
"A \n Hello World! \n \n C",
400+
['A', 'Hello World!', 'C'],
401+
],
402+
[
403+
"\0\nA\nB",
404+
["\0", 'A', 'B'],
405+
],
406+
];
407+
}
408+
409+
/**
410+
* @dataProvider dataSplit
411+
*
412+
* @param string $string
413+
* @param array $expected
414+
*/
415+
public function testSplit(string $string, array $expected): void
416+
{
417+
$this->assertSame($expected, StringHelper::split($string));
418+
}
419+
420+
public function testSplitWithSeparator(): void
421+
{
422+
$this->assertSame(['A', 'B', 'C'], StringHelper::split(' A 2 B3C', '\d'));
423+
}
370424
}

0 commit comments

Comments
 (0)