Skip to content

Commit

Permalink
Added str macro for first and last words of a string
Browse files Browse the repository at this point in the history
  • Loading branch information
nunoxavier committed Jun 20, 2024
1 parent 3e5e51f commit d5cf4c7
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Macros/StrMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Flavorly\LaravelHelpers\Contracts\RegistersMacros;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;

final class StrMacros implements RegistersMacros
Expand All @@ -16,6 +17,7 @@ public static function register(): void
self::normalizeCharacters();
self::acronimize();
self::username();
self::firstAndLastWords();
self::money();
self::spin();
}
Expand Down Expand Up @@ -49,6 +51,19 @@ public static function username(): void
});
}

public static function firstAndLastWords(): void
{
Str::macro('firstAndLastWords', function (string $string): string {
return Str::of($string)
->explode(' ')
->filter()
->values()
->whenNotEmpty(function (Collection $words): string {
return trim($words->first().' '.$words->last());
}, $string);

Check failure on line 63 in src/Macros/StrMacros.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #2 $default of method Illuminate\Support\Collection<int,string>::whenNotEmpty() expects (callable(Illuminate\Support\Collection<int, non-falsy-string>): string)|null, string given.
});
}

public static function spin(): void
{
Str::macro('spin', function (string $string): string {
Expand Down

0 comments on commit d5cf4c7

Please sign in to comment.