dantleech/symfony-form-array-to-delimited-string-transformer
Symfony Form data transformer that converts delimited text fields to arrays and back. Trims whitespace, ignores empty values, and supports custom delimiters plus configurable output padding for formatted tag/keyword input.
Form component, which lacks direct Laravel integration. Laravel’s form handling (e.g., FormRequest, livewire/forms) requires abstraction or a custom wrapper, increasing complexity for minimal gain.explode()/implode() for basic transformations.collect() methods for array manipulation.spatie/laravel-tags for tagging systems.DataTransformerInterface in forms) but offers limited advantage in Laravel unless:
DataTransformerInterface must be adapted to Laravel’s FormRequest or Illuminate\Http\Request lifecycle.FormFactory vs. Laravel’s service container).DelimitedStringTransformer) and use it directly in Laravel.collect()->implode()) for 90% of use cases.explode() behavior vs. transformer logic).text column for delimited strings vs. JSON for arrays).form component for a single transformer risks:
symfony/form vs. Laravel’s bundled Symfony components).'one,,two') may not align with Laravel’s validation (e.g., explode() + array_filter()).explode()/implode().;).'%' as a delimiter).implode('; ', $array) vs. new ArrayToDelimitedStringTransformer(';', 1, 1).spatie/laravel-tags) already solving this?trait DelimitedStringTransformer {
public function transform(array $array, string $delimiter = ','): string {
return implode(" {$delimiter} ", $array);
}
public function reverse(string $string, string $delimiter): array {
return array_filter(explode($delimiter, trim($string)));
}
}
public $transformer).["a", "b"]) to delimited strings for storage.app/Services/DelimitedStringTransformer) to avoid Symfony dependencies.namespace App\Services;
class DelimitedStringTransformer {
public function __construct(
private string $delimiter = ',',
private int $padding = 0
) {}
public function transform(array $array): string {
return implode(" {$this->delimiter} ", $array);
}
public function reverse(string $string): array {
return array_filter(explode($this->delimiter, trim($string)));
}
}
TEXT for long strings).explode() behavior vs. transformer logic).explode()/implode() usage in the codebase.FormRequest validation.explode() calls with the new service.laravelcollective/html).explode()/implode() or use spatie/laravel-tags.symfony/form directly. Use the standalone service approach.composer require --dev for testing.AppServiceProvider:
$this->app->singleton(DelimitedStringTransformer::class, function ($app) {
return new \App\Services\DelimitedStringTransformer(';', 1);
});
public function prepareForValidation() {
$this->merge([
'tags' => app(DelimitedStringTransformer::class)->reverse($this->input('tags'))
]);
}
use App\Services\DelimitedStringTransformer;
public function mount() {
$this->transformer = new DelimitedStringTransformer(';');
}
explode()/implode().implode()/explode()). Maintenance is minimal if wrapped properly.README.md (e.g., Livewire/Blade integration).How can I help you explore Laravel packages today?