Skip to content

Commit 75d902b

Browse files
authored
refactor(support): move LanguageHelper#join to ArrayHelper and StringHelper (#612)
1 parent 219bda6 commit 75d902b

File tree

7 files changed

+65
-45
lines changed

7 files changed

+65
-45
lines changed

src/Tempest/Support/src/ArrayHelper.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,28 @@ public function unwrap(): self
631631
return new self($array);
632632
}
633633

634+
/**
635+
* Joins all values using the specified `$glue`. The last item of the string is separated by `$finalGlue`.
636+
*/
637+
public function join(string $glue = ', ', ?string $finalGlue = ' and '): StringHelper
638+
{
639+
if ($finalGlue === '' || is_null($finalGlue)) {
640+
return $this->implode($glue);
641+
}
642+
643+
if ($this->isEmpty()) {
644+
return str('');
645+
}
646+
647+
$parts = $this->pop($last);
648+
649+
if ($parts->isNotEmpty()) {
650+
return $parts->implode($glue)->append($finalGlue, $last);
651+
}
652+
653+
return str($last);
654+
}
655+
634656
public function dump(mixed ...$dumps): self
635657
{
636658
lw($this->array, ...$dumps);

src/Tempest/Support/src/LanguageHelper.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,6 @@
1010

1111
final class LanguageHelper
1212
{
13-
/**
14-
* @param string[] $parts
15-
*/
16-
public static function join(array|ArrayHelper $parts): string
17-
{
18-
$parts = arr($parts)->pop($last);
19-
20-
if ($parts->isNotEmpty()) {
21-
return $parts->implode(', ') . ' ' . 'and' . ' ' . $last;
22-
}
23-
24-
return $last;
25-
}
26-
2713
public static function pluralize(string $value, int|array|Countable $count = 2): string
2814
{
2915
return get(Pluralizer::class)->pluralize($value, $count);

src/Tempest/Support/src/StringHelper.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -480,17 +480,21 @@ public function explode(string $separator = ' '): ArrayHelper
480480
/**
481481
* Implode the array into a string by a separator.
482482
*
483-
* @param array|ArrayHelper $array The array to implode.
484-
* @param string $separator The separator to implode the array by.
483+
* @param array|ArrayHelper $parts The array to implode.
484+
* @param string $glue The separator to implode the array by.
485485
*
486486
* @return self The imploded string.
487487
*/
488-
public static function implode(array|ArrayHelper $array, string $separator = ' '): self
488+
public static function implode(array|ArrayHelper $parts, string $glue = ' '): self
489489
{
490-
$array = ($array instanceof ArrayHelper)
491-
? $array->toArray()
492-
: $array;
490+
return arr($parts)->implode($glue);
491+
}
493492

494-
return new self(implode($separator, $array));
493+
/**
494+
* Joins all values using the specified `$glue`. The last item of the string is separated by `$finalGlue`.
495+
*/
496+
public static function join(array|ArrayHelper $parts, string $glue = ', ', ?string $finalGlue = ' and '): self
497+
{
498+
return arr($parts)->join($glue, $finalGlue);
495499
}
496500
}

src/Tempest/Support/tests/ArrayHelperTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Tempest\Support\Tests;
66

77
use InvalidArgumentException;
8+
use PHPUnit\Framework\Attributes\TestWith;
89
use PHPUnit\Framework\TestCase;
910
use function Tempest\Support\arr;
1011
use Tempest\Support\ArrayHelper;
@@ -144,6 +145,21 @@ public function test_implode(): void
144145
$this->assertEquals(str('a,b,c'), arr(['a', 'b', 'c'])->implode(','));
145146
}
146147

148+
#[TestWith([['Jon', 'Jane'], 'Jon and Jane'])]
149+
#[TestWith([['Jon', 'Jane', 'Jill'], 'Jon, Jane and Jill'])]
150+
public function test_join(array $initial, string $expected): void
151+
{
152+
$this->assertEquals($expected, arr($initial)->join());
153+
}
154+
155+
#[TestWith([['Jon', 'Jane'], ', ', ' and maybe ', 'Jon and maybe Jane'])]
156+
#[TestWith([['Jon', 'Jane', 'Jill'], ' + ', ' and ', 'Jon + Jane and Jill'])]
157+
#[TestWith([['Jon', 'Jane', 'Jill'], ' + ', null, 'Jon + Jane + Jill'])]
158+
public function test_join_with_glues(array $initial, string $glue, ?string $finalGlue, string $expected): void
159+
{
160+
$this->assertTrue(arr($initial)->join($glue, $finalGlue)->equals($expected));
161+
}
162+
147163
public function test_pop(): void
148164
{
149165
$array = arr(['a', 'b', 'c'])->pop($value);

src/Tempest/Support/tests/LanguageHelperTest.php

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/Tempest/Support/tests/StringHelperTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,21 @@ public function test_implode(): void
420420
$this->assertSame('john doe', StringHelper::implode(arr(['john', 'doe']))->toString());
421421
}
422422

423+
#[TestWith([['Jon', 'Jane'], 'Jon and Jane'])]
424+
#[TestWith([['Jon', 'Jane', 'Jill'], 'Jon, Jane and Jill'])]
425+
public function test_join(array $initial, string $expected): void
426+
{
427+
$this->assertEquals($expected, StringHelper::join($initial));
428+
}
429+
430+
#[TestWith([['Jon', 'Jane'], ', ', ' and maybe ', 'Jon and maybe Jane'])]
431+
#[TestWith([['Jon', 'Jane', 'Jill'], ' + ', ' and ', 'Jon + Jane and Jill'])]
432+
#[TestWith([['Jon', 'Jane', 'Jill'], ' + ', null, 'Jon + Jane + Jill'])]
433+
public function test_join_with_glues(array $initial, string $glue, ?string $finalGlue, string $expected): void
434+
{
435+
$this->assertTrue(StringHelper::join($initial, $glue, $finalGlue)->equals($expected));
436+
}
437+
423438
public function test_excerpt(): void
424439
{
425440
$content = str('a

src/Tempest/Validation/src/Exceptions/ValidationException.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use Exception;
88
use function Tempest\Support\arr;
9-
use Tempest\Support\LanguageHelper;
109
use Tempest\Validation\Rule;
1110

1211
final class ValidationException extends Exception
@@ -18,7 +17,7 @@ public function __construct(object $object, public readonly array $failingRules)
1817
foreach ($this->failingRules as $field => $failingRulesForField) {
1918
/** @var Rule $failingRuleForField */
2019
foreach ($failingRulesForField as $failingRuleForField) {
21-
$messages[$field][] = LanguageHelper::join(arr($failingRuleForField->message()));
20+
$messages[$field][] = arr($failingRuleForField->message())->join()->toString();
2221
}
2322
}
2423

0 commit comments

Comments
 (0)