Skip to content

Commit bc8d395

Browse files
committed
Add str_increment and str_decrement functions
1 parent 4051889 commit bc8d395

File tree

3 files changed

+111
-3
lines changed

3 files changed

+111
-3
lines changed

src/Php83/Php83.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Symfony\Polyfill\Php83;
1313

1414
/**
15-
* @author Ion Bazan <ion.bazan@gmail.com>
15+
* @author Ion Bazan <ion.bazan@gmail.com>, Pierre Ambroise <pierre27.ambroise@gmail.com>
1616
*
1717
* @internal
1818
*/
@@ -82,4 +82,42 @@ public static function mb_str_pad(string $string, int $length, string $pad_strin
8282
return mb_substr(str_repeat($pad_string, $leftPaddingLength), 0, $leftPaddingLength, $encoding).$string.mb_substr(str_repeat($pad_string, $rightPaddingLength), 0, $rightPaddingLength, $encoding);
8383
}
8484
}
85+
86+
public static function str_increment(string $string): string
87+
{
88+
if ('' === $string) {
89+
throw new \ValueError('str_increment(): Argument #1 ($string) cannot be empty');
90+
}
91+
92+
if (1 === preg_match('/[^\x00-\x7F]/', $string)) {
93+
throw new \ValueError('str_increment(): Argument #1 ($string) must be composed only of alphanumeric ASCII characters');
94+
}
95+
96+
return ++$string;
97+
}
98+
99+
public static function str_decrement(string $string): string
100+
{
101+
if ('' === $string) {
102+
throw new \ValueError('str_decrement(): Argument #1 ($string) cannot be empty');
103+
}
104+
105+
if (1 === preg_match('/[^\x00-\x7F]/', $string)) {
106+
throw new \ValueError('str_decrement(): Argument #1 ($string) must be composed only of alphanumeric ASCII characters');
107+
}
108+
109+
if ($string === 'A' || $string === '0') {
110+
throw new \ValueError(sprintf('Argument #1 ($string) "%s" is out of decrement range', $string));
111+
}
112+
113+
if (!in_array(substr($string, -1), ['A', '0'])){
114+
return join('', array_slice(str_split($string), 0, -1)) . chr(ord(substr($string, -1)) - 1);
115+
}
116+
117+
if (strlen($string) === 1) {
118+
return '';
119+
}
120+
121+
return self::str_decrement(substr($string, 0, -1)) . 'Z';
122+
}
85123
}

src/Php83/bootstrap.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ function mb_str_pad(string $string, int $length, string $pad_string = ' ', int $
2727
function stream_context_set_options($context, array $options): bool { return stream_context_set_option($context, $options); }
2828
}
2929

30+
if (!function_exists('str_increment')) {
31+
function str_increment(string $string): string { return p\Php83::str_increment($string); }
32+
}
33+
34+
if (!function_exists('str_decrement')) {
35+
function str_decrement(string $string): string { return p\Php83::str_decrement($string); }
36+
}
37+
3038
if (\PHP_VERSION_ID >= 80100) {
3139
return require __DIR__.'/bootstrap81.php';
3240
}

tests/Php83/Php83Test.php

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function testMbStrPad(string $expectedResult, string $string, int $length
4646
public function testMbStrPadInvalidArguments(string $expectedError, string $string, int $length, string $padString, int $padType, string $encoding = null): void
4747
{
4848
$this->expectException(\ValueError::class);
49-
$this->expectErrorMessage($expectedError);
49+
$this->expectExceptionMessage($expectedError);
5050

5151
mb_str_pad($string, $length, $padString, $padType, $encoding);
5252
}
@@ -161,7 +161,7 @@ public static function jsonDataProvider(): iterable
161161
public function testJsonValidateInvalidOptionsProvided(int $depth, int $flags, string $expectedError)
162162
{
163163
$this->expectException(\ValueError::class);
164-
$this->expectErrorMessage($expectedError);
164+
$this->expectExceptionMessage($expectedError);
165165
json_validate('{}', $depth, $flags);
166166
}
167167

@@ -202,4 +202,66 @@ public function testDateTimeExceptionClassesExist()
202202
$this->assertTrue(class_exists(\DateMalformedIntervalStringException::class));
203203
$this->assertTrue(class_exists(\DateMalformedPeriodStringException::class));
204204
}
205+
206+
/**
207+
* @dataProvider strIncrementProvider
208+
*/
209+
public function testStrIncrement(string $result, string $string)
210+
{
211+
$this->assertSame($result, str_increment($string));
212+
}
213+
214+
/**
215+
* @dataProvider strIncrementProvider
216+
*/
217+
public function testStrDecrements(string $string, string $result)
218+
{
219+
$this->assertSame($result, str_decrement($string));
220+
}
221+
222+
public static function strIncrementProvider(): iterable
223+
{
224+
yield ['ABD', 'ABC'];
225+
yield ['EB', 'EA'];
226+
yield ['AAA', 'ZZ'];
227+
yield ['AAA2', 'AAA1'];
228+
}
229+
230+
/**
231+
* @dataProvider strInvalidIncrementProvider
232+
*/
233+
public function testInvalidStrIncrements(string $errorMessage, string $string)
234+
{
235+
$this->expectException(\ValueError::class);
236+
$this->expectExceptionMessage($errorMessage);
237+
238+
str_increment($string);
239+
}
240+
241+
242+
public static function strInvalidIncrementProvider(): iterable
243+
{
244+
yield ['str_increment(): Argument #1 ($string) cannot be empty', ""];
245+
yield ['str_increment(): Argument #1 ($string) must be composed only of alphanumeric ASCII characters', '我喜歡雞肉'];
246+
yield ['str_increment(): Argument #1 ($string) must be composed only of alphanumeric ASCII characters', '書左'];
247+
}
248+
249+
250+
/**
251+
* @dataProvider strInvalidDecrementProvider
252+
*/
253+
public function testInvalidStrDecrements(string $errorMessage, string $string)
254+
{
255+
$this->expectException(\ValueError::class);
256+
$this->expectExceptionMessage($errorMessage);
257+
258+
str_decrement($string);
259+
}
260+
261+
public static function strInvalidDecrementProvider(): iterable
262+
{
263+
yield ['str_decrement(): Argument #1 ($string) cannot be empty', ''];
264+
yield ['str_decrement(): Argument #1 ($string) must be composed only of alphanumeric ASCII characters', '我喜歡雞肉'];
265+
yield ['str_decrement(): Argument #1 ($string) "A" is out of decrement range', 'A'];
266+
}
205267
}

0 commit comments

Comments
 (0)