<?php
use Illuminate\Support\Arr;
$tests = [
'' => [],
'one' => ['one'],
'one and two' => ['one', 'two'],
'one, two and three' => ['one', 'two', 'three'],
'one, two, three and four' => ['one', 'two', 'three', 'four'],
'one, two, three, four and five' => ['one', 'two', 'three', 'four', 'five'],
];
function laravelCollectJoin(array $words, string $separator = ', ', string $conjunction = ' and '): string {
return collect($words)->join($separator, $conjunction);
}
function laravelArrJoin(array $words, string $separator = ', ', string $conjunction = ' and '): string {
return Arr::join($words, $separator, $conjunction);
}
printf("| %20s| 0️⃣ | 1️⃣ | 2️⃣ | 3️⃣ | 4️⃣ | 5️⃣ |\n", 'user \ elements');
$funcs = [
'laravelCollectJoin',
'laravelArrJoin',
];
foreach ($funcs as $func) {
echo "--------------------------------------------------------------------\n";
printf(
"| %20s| %s | %s | %s | %s | %s | %s |\n",
$func,
...array_map(
fn($result, $expected) => $result === $expected ? '✅': '💩',
array_map($func, $tests),
array_keys($tests)
)
);
}
var_export(array_values(array_map($func, $tests)));