Skip to content

Commit 57536d2

Browse files
committed
Strings, Arrays: $flags replaced with parameters
1 parent c330f89 commit 57536d2

File tree

6 files changed

+72
-14
lines changed

6 files changed

+72
-14
lines changed

src/Utils/Arrays.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,13 @@ public static function renameKey(array &$array, string|int $oldKey, string|int $
186186

187187

188188
/**
189-
* Returns only those array items, which matches a regular expression $pattern.
189+
* Returns only those array items, which matches a regular expression $pattern. Parameter $flags is deprecated.
190190
* @param string[] $array
191191
* @return string[]
192192
*/
193-
public static function grep(array $array, string $pattern, int $flags = 0): array
193+
public static function grep(array $array, string $pattern, int $flags = 0, bool $invert = false): array
194194
{
195+
$flags |= $invert ? PREG_GREP_INVERT : 0;
195196
return Strings::pcre('preg_grep', [$pattern, $array, $flags]);
196197
}
197198

src/Utils/Strings.php

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -468,20 +468,33 @@ private static function pos(string $haystack, string $needle, int $nth = 1): ?in
468468

469469
/**
470470
* Splits a string into array by the regular expression. Parenthesized expression in the delimiter are captured.
471-
* Argument $flags can be any combination of PREG_SPLIT_NO_EMPTY and PREG_OFFSET_CAPTURE flags.
471+
* Parameter $flags is deprecated.
472472
*/
473-
public static function split(string $subject, string $pattern, int $flags = 0): array
474-
{
475-
return self::pcre('preg_split', [$pattern, $subject, -1, $flags | PREG_SPLIT_DELIM_CAPTURE]);
473+
public static function split(
474+
string $subject,
475+
string $pattern,
476+
int $flags = 0,
477+
bool $captureOffset = false,
478+
bool $noEmpty = false,
479+
): array {
480+
$flags |= ($captureOffset ? PREG_SPLIT_OFFSET_CAPTURE : 0) | ($noEmpty ? PREG_SPLIT_NO_EMPTY : 0) | PREG_SPLIT_DELIM_CAPTURE;
481+
return self::pcre('preg_split', [$pattern, $subject, -1, $flags]);
476482
}
477483

478484

479485
/**
480486
* Checks if given string matches a regular expression pattern and returns an array with first found match and each subpattern.
481-
* Argument $flags can be any combination of PREG_OFFSET_CAPTURE and PREG_UNMATCHED_AS_NULL flags.
487+
* Parameter $flags is deprecated.
482488
*/
483-
public static function match(string $subject, string $pattern, int $flags = 0, int $offset = 0): ?array
484-
{
489+
public static function match(
490+
string $subject,
491+
string $pattern,
492+
int $flags = 0,
493+
int $offset = 0,
494+
bool $captureOffset = false,
495+
bool $unmatchedAsNull = false,
496+
): ?array {
497+
$flags |= ($captureOffset ? PREG_OFFSET_CAPTURE : 0) | ($unmatchedAsNull ? PREG_UNMATCHED_AS_NULL : 0);
485498
if ($offset > strlen($subject)) {
486499
return null;
487500
}
@@ -492,11 +505,19 @@ public static function match(string $subject, string $pattern, int $flags = 0, i
492505

493506

494507
/**
495-
* Finds all occurrences matching regular expression pattern and returns a two-dimensional array. Result is array of matches (ie uses by default PREG_SET_ORDER).
496-
* Argument $flags can be any combination of PREG_OFFSET_CAPTURE, PREG_UNMATCHED_AS_NULL and PREG_PATTERN_ORDER flags.
508+
* Finds all occurrences matching regular expression pattern and returns a two-dimensional array.
509+
* Result is array of matches (ie uses by default PREG_SET_ORDER). Parameter $flags is deprecated.
497510
*/
498-
public static function matchAll(string $subject, string $pattern, int $flags = 0, int $offset = 0): array
499-
{
511+
public static function matchAll(
512+
string $subject,
513+
string $pattern,
514+
int $flags = 0,
515+
int $offset = 0,
516+
bool $captureOffset = false,
517+
bool $unmatchedAsNull = false,
518+
bool $patternOrder = false,
519+
): array {
520+
$flags |= ($captureOffset ? PREG_OFFSET_CAPTURE : 0) | ($unmatchedAsNull ? PREG_UNMATCHED_AS_NULL : 0) | ($patternOrder ? PREG_PATTERN_ORDER : 0);
500521
if ($offset > strlen($subject)) {
501522
return [];
502523
}
@@ -524,7 +545,7 @@ public static function replace(
524545
if (!is_callable($replacement, false, $textual)) {
525546
throw new Nette\InvalidStateException("Callback '$textual' is not callable.");
526547
}
527-
$flags = ($captureOffset ? PREG_OFFSET_CAPTURE : 0) | ($unmatchedAsNull ? PREG_UNMATCHED_AS_NULL : 0);
548+
$flags = ($captureOffset ? PREG_OFFSET_CAPTURE : 0) | ($unmatchedAsNull ? PREG_UNMATCHED_AS_NULL : 0);
528549
return self::pcre('preg_replace_callback', [$pattern, $replacement, $subject, $limit, 0, $flags]);
529550

530551
} elseif (is_array($pattern) && is_string(key($pattern))) {

tests/Utils/Arrays.grep().phpt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,8 @@ Assert::same([
2121
0 => 'a',
2222
2 => 'c',
2323
], Arrays::grep(['a', '1', 'c'], '#\d#', PREG_GREP_INVERT));
24+
25+
Assert::same([
26+
0 => 'a',
27+
2 => 'c',
28+
], Arrays::grep(['a', '1', 'c'], '#\d#', invert: true));

tests/Utils/Strings.match().phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Assert::same(['hell', 'l'], Strings::match('hello world!', '#([e-l])+#'));
2020
Assert::same(['hell'], Strings::match('hello world!', '#[e-l]+#'));
2121

2222
Assert::same([['hell', 0]], Strings::match('hello world!', '#[e-l]+#', PREG_OFFSET_CAPTURE));
23+
Assert::same([['hell', 0]], Strings::match('hello world!', '#[e-l]+#', captureOffset: true));
2324

2425
Assert::same(['ll'], Strings::match('hello world!', '#[e-l]+#', 0, 2));
2526

tests/Utils/Strings.matchAll().phpt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,28 @@ Assert::same([
3232
[['k', 14], ['k', 14], ['', 15]],
3333
], Strings::matchAll('žluťoučký kůň!', '#([a-z])([a-z]*)#u', PREG_OFFSET_CAPTURE));
3434

35+
Assert::same([
36+
[['lu', 2], ['l', 2], ['u', 3]],
37+
[['ou', 6], ['o', 6], ['u', 7]],
38+
[['k', 10], ['k', 10], ['', 11]],
39+
[['k', 14], ['k', 14], ['', 15]],
40+
], Strings::matchAll('žluťoučký kůň!', '#([a-z])([a-z]*)#u', captureOffset: true));
41+
3542
Assert::same([
3643
[['lu', 2], ['ou', 6], ['k', 10], ['k', 14]],
3744
[['l', 2], ['o', 6], ['k', 10], ['k', 14]],
3845
[['u', 3], ['u', 7], ['', 11], ['', 15]],
3946
], Strings::matchAll('žluťoučký kůň!', '#([a-z])([a-z]*)#u', PREG_OFFSET_CAPTURE | PREG_PATTERN_ORDER));
4047

48+
Assert::same([
49+
[['lu', 2], ['ou', 6], ['k', 10], ['k', 14]],
50+
[['l', 2], ['o', 6], ['k', 10], ['k', 14]],
51+
[['u', 3], ['u', 7], ['', 11], ['', 15]],
52+
], Strings::matchAll('žluťoučký kůň!', '#([a-z])([a-z]*)#u', captureOffset: true, patternOrder: true));
53+
4154
Assert::same([['l'], ['k'], ['k']], Strings::matchAll('žluťoučký kůň', '#[e-l]+#u', 0, 2));
4255

4356
Assert::same([['ll', 'l']], Strings::matchAll('hello world!', '#[e-l]+#', PREG_PATTERN_ORDER, 2));
57+
Assert::same([['ll', 'l']], Strings::matchAll('hello world!', '#[e-l]+#', patternOrder: true, offset: 2));
4458

4559
Assert::same([], Strings::matchAll('hello world!', '', 0, 50));

tests/Utils/Strings.split().phpt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,26 @@ Assert::same([
2929
'c',
3030
], Strings::split('a, b, c', '#(,)\s*#', PREG_SPLIT_NO_EMPTY));
3131

32+
Assert::same([
33+
'a',
34+
',',
35+
'b',
36+
',',
37+
'c',
38+
], Strings::split('a, b, c', '#(,)\s*#', noEmpty: true));
39+
3240
Assert::same([
3341
['a', 0],
3442
[',', 1],
3543
['b', 3],
3644
[',', 4],
3745
['c', 6],
3846
], Strings::split('a, b, c', '#(,)\s*#', PREG_SPLIT_OFFSET_CAPTURE));
47+
48+
Assert::same([
49+
['a', 0],
50+
[',', 1],
51+
['b', 3],
52+
[',', 4],
53+
['c', 6],
54+
], Strings::split('a, b, c', '#(,)\s*#', captureOffset: true));

0 commit comments

Comments
 (0)