bornfight/jsonapi-documentation
Install via Composer:
composer require vendor/package-name
The package provides string manipulation utilities (e.g., pluralization, singularization, camelCase conversion) using Doctrine Inflector (replacing Symfony Inflector in v0.1.2). For quick testing, use the facade:
use PackageName\Facades\Inflector;
Inflector::pluralize('tax'); // Returns "taxes" (fixed from "tacies")
Inflector::singularize('productDetails'); // Returns "productDetail" (fixed from "productDetailss")
Model Naming Conventions Dynamically generate table names or model names from class names:
$modelName = Inflector::classify('user_profile'); // "UserProfile"
$tableName = Inflector::tableize($modelName); // "user_profiles"
API/Route Labeling Auto-generate human-readable labels for API responses or route names:
$label = Inflector::humanize('is_active'); // "Is Active"
Form/Validation Messages Localize error messages dynamically:
$message = Inflector::titleize('invalid ' . Inflector::pluralize('credit_card')); // "Invalid Credit Cards"
Service Providers: Bind the Inflector facade in AppServiceProvider for global access.
Blade Directives: Create custom directives for template-friendly usage:
Blade::directive('plural', function ($expression) {
return "<?php echo \\PackageName\\Inflector::pluralize({$expression}); ?>";
});
Usage: @plural('tax') → "taxes".
Eloquent Events: Use inflection in model observers for consistent naming:
public function created(User $user) {
$user->update(['slug' => Inflector::slugify($user->name)]);
}
Inflector::pluralize('child') → "children" (correct, vs. Symfony’s "childs").Inflector::singularize('people') → "person" (correct, vs. Symfony’s "people").Edge Cases: For unexpected outputs, check the Doctrine Inflector docs or test manually:
dd(Inflector::getRules()); // Inspect active rules.
Custom Rules: Extend the inflector by publishing the config:
php artisan vendor:publish --provider="PackageName\ServiceProvider"
Then modify config/inflector.php to add custom rules (e.g., for acronyms like "NASA" → "NASAs").
Performance: Doctrine Inflector is faster than Symfony’s for large-scale operations (e.g., batch processing).
Str::of() for locale-aware inflection:
Str::of('tax')->plural(); // Uses Doctrine Inflector.
Inflector::testMode() to mock rules in PHPUnit:
Inflector::testMode(true);
Inflector::addRule('special_case', 'regex', 'replacement');
How can I help you explore Laravel packages today?