alexivanou/text-functions-bundle
Installation
composer require alexivanou/text-functions-bundle
Add to config/app.php under providers:
Alexivanou\TextFunctionsBundle\TextFunctionsServiceProvider::class,
First Use Case Publish the config (if needed) and use the helper functions directly:
use Alexivanou\TextFunctionsBundle\Facades\TextFunctions;
// Pluralize text
echo TextFunctions::pluralize('item', 1); // "1 item"
echo TextFunctions::pluralize('item', 2); // "2 items"
// Convert number to text
echo TextFunctions::num2str(123); // "one hundred twenty-three"
Where to Look First
TextFunctions (for direct usage).TextFunctionsServiceProvider (for customization).config/text-functions.php (if published).Pluralization in Views
// Blade template
<p>{{ TextFunctions::pluralize('user', $count) }} found.</p>
Number-to-Text Conversion
// For user-facing text (e.g., invoices)
$amountText = TextFunctions::num2str($amount);
Custom Rules
Override default pluralization rules in config/text-functions.php:
'plural_rules' => [
'rule' => [
'^(.*)' => '$1s', // Default fallback
'^(ox|box)$' => '$1en', // Special case
],
],
Integration with Forms
// Validate and format numeric inputs
$formattedValue = TextFunctions::num2str($request->input('quantity'));
pluralize() method for language-aware text (e.g., Spanish, Russian).num2str() for static content (e.g., reports).$this->app->instance('text-functions', $mockTextFunctions);
Proprietary License
composer.json for restrictions.Limited Language Support
num2str() defaults to English. For multilingual apps, extend the TextFunctions class or use a dedicated library (e.g., laravel-localization).Config Overrides
config(['text-functions.plural_rules' => array_merge(config('text-functions.plural_rules'), [...])]);
Edge Cases in num2str()
number-to-words.Check Config Values
php artisan config:clear
Then verify with:
dd(config('text-functions'));
Facade Binding Issues
If TextFunctions::pluralize() fails, rebind the facade in a service provider:
$this->app->bind('text-functions', function () {
return new \Alexivanou\TextFunctionsBundle\TextFunctions();
});
Add Custom Functions
Extend the TextFunctions class and bind it in the service provider:
// app/Extensions/CustomTextFunctions.php
class CustomTextFunctions extends \Alexivanou\TextFunctionsBundle\TextFunctions {
public function abbreviate($text, $maxLength) {
// Custom logic
}
}
Override Pluralization Logic Replace the entire pluralization engine by binding a custom implementation:
$this->app->singleton('text-functions', function () {
return new CustomPluralizer();
});
Localization Hooks Use Laravel’s localization system to dynamically switch pluralization rules:
$rules = config("text-functions.plural_rules.{$locale}");
How can I help you explore Laravel packages today?