Skip to content

Commit

Permalink
add array suffix values
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Dec 20, 2023
1 parent 3055306 commit d91563b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/ArrayStandard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
8 changes: 8 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 46 additions & 0 deletions tests/ArraySuffixValuesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/*
* This file is part of Chevere.
*
* (c) Rodolfo Berrios <rodolfo@chevere.org>
*
* 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);
}
}

0 comments on commit d91563b

Please sign in to comment.