diff --git a/src/ArrayStandard.php b/src/ArrayStandard.php index 457342e..4dc7f86 100644 --- a/src/ArrayStandard.php +++ b/src/ArrayStandard.php @@ -152,4 +152,12 @@ public function prefixValues(string|int|float $prefix): array return array_map(fn ($value) => $prefix . $value, $array); } + + // @phpstan-ignore-next-line + public function suffixValues(string|int|float $suffix): array + { + $array = $this->array; + + return array_map(fn ($value) => $value . $suffix, $array); + } } diff --git a/src/functions.php b/src/functions.php index 9f19f17..38dfb82 100644 --- a/src/functions.php +++ b/src/functions.php @@ -92,6 +92,14 @@ function arrayPrefixValues(array $array, string|int|float $prefix): array return (new ArrayStandard($array))->prefixValues($prefix); } +/** + * @phpstan-ignore-next-line + */ +function arraySuffixValues(array $array, string|int|float $suffix): array +{ + return (new ArrayStandard($array))->suffixValues($suffix); +} + /** * @param string|int $key Key(s) to unset. * @phpstan-ignore-next-line diff --git a/tests/ArraySuffixValuesTest.php b/tests/ArraySuffixValuesTest.php new file mode 100644 index 0000000..1c2cc32 --- /dev/null +++ b/tests/ArraySuffixValuesTest.php @@ -0,0 +1,46 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Chevere\Tests; + +use PHPUnit\Framework\TestCase; +use function Chevere\Standard\arraySuffixValues; + +final class ArraySuffixValuesTest extends TestCase +{ + public function dataProvider(): array + { + return [ + ['foo', 'bar'], + [1, 1], + ]; + } + + /** + * @dataProvider dataProvider + */ + public function testFunction($value, $suffix): void + { + $list = [$value]; + $result = arraySuffixValues($list, $suffix); + $this->assertSame([$value . $suffix], $result); + } + + public function testTypeJuggling(): void + { + $list = ['wea']; + $prefix = 1; + $result = arraySuffixValues($list, $prefix); + $this->assertSame(['wea1'], $result); + } +}