Created
January 3, 2024 22:16
-
-
Save hotmeteor/52b63204e235d9c188ce9039b694eb41 to your computer and use it in GitHub Desktop.
Laravel APA title case macro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Providers\Macros; | |
class ApaTitle | |
{ | |
public function __invoke() | |
{ | |
return function ($string) { | |
// Define minor words to be lowercase | |
$minorWords = ['and', 'as', 'but', 'for', 'if', 'nor', 'or', 'so', 'yet', 'a', 'an', 'the', 'as', 'at', 'by', 'for', 'in', 'of', 'off', 'on', 'per', 'to', 'up', 'via']; | |
// Split the string into words | |
$words = explode(' ', $string); | |
// Capitalize the first word | |
$words[0] = ucfirst(mb_strtolower($words[0])); | |
// Capitalize major words and lowercase minor words | |
foreach ($words as $key => $word) { | |
// Skip the first word | |
if ($key === 0) { | |
continue; | |
} | |
// Check if the word is a major word or a minor word | |
$lowercaseWord = mb_strtolower($word); | |
if (in_array($lowercaseWord, $minorWords) && mb_strlen($lowercaseWord) <= 3) { | |
$words[$key] = $lowercaseWord; | |
} else { | |
$words[$key] = ucfirst($lowercaseWord); | |
} | |
} | |
// Join the words back into a string | |
return implode(' ', $words); | |
}; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Providers\Macros; | |
use Illuminate\Support\Str; | |
class ApaTitleStringable | |
{ | |
public function __invoke() | |
{ | |
return function () { | |
return new static(Str::apaTitle($this->value)); | |
}; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Providers; | |
use App\Providers\Macros\ApaTitle; | |
use App\Providers\Macros\ApaTitleStringable; | |
use Illuminate\Support\Collection; | |
use Illuminate\Support\ServiceProvider; | |
use Illuminate\Support\Str; | |
use Illuminate\Support\Stringable; | |
class AppServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Register any application services. | |
*/ | |
public function register(): void | |
{ | |
Collection::make($this->strMacros()) | |
->reject(fn($class, $macro) => Str::hasMacro($macro)) | |
->each(fn($class, $macro) => Str::macro($macro, app($class)())); | |
Collection::make($this->stringableMacros()) | |
->reject(fn($class, $macro) => Stringable::hasMacro($macro)) | |
->each(fn($class, $macro) => Stringable::macro($macro, app($class)())); | |
} | |
/** | |
* Bootstrap any application services. | |
*/ | |
public function boot(): void | |
{ | |
} | |
protected function strMacros(): array | |
{ | |
return [ | |
'apaTitle' => ApaTitle::class, | |
]; | |
} | |
protected function stringableMacros(): array | |
{ | |
return [ | |
'apaTitle' => ApaTitleStringable::class, | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment