## Getting Started
Install via Composer:
```bash
composer require package-name
For PHP 8.5+ projects, ensure your composer.json requires ^5.0 and PHP 8.5. The package now drops PHP 8.1/Symfony 7.3 support, so verify your environment matches:
"require": {
"php": "^8.5",
"symfony/*": "^8.0"
}
Start with the documentation for core transformers like array_map_transformer or multi_replace_transformer. Example first use case:
use PackageName\Transformers\ArrayMapTransformer;
$transformer = new ArrayMapTransformer(fn($item) => strtoupper($item));
$result = $transformer->transform(['foo', 'bar']); // ['FOO', 'BAR']
Data Sanitization/Transformation:
Use multi_replace_transformer for bulk string replacements:
$replacer = new MultiReplaceTransformer(['old' => 'new']);
$cleaned = $replacer->transform($dirtyData);
Collection Processing: Chain transformers with Laravel Collections:
$collection->map(fn($item) => (new SlugifyTransformer())->transform($item));
Dynamic Transformations:
Leverage sprintf_transformer for template-based formatting:
$formatter = new SprintfTransformer('User %d: %s');
$formatted = $formatter->transform([1, 'John']); // "User 1: John"
$this->app->singleton(SlugifyTransformer::class, fn() => new SlugifyTransformer());
implode_transformer to flatten arrays:
$request->merge(['tags' => (new ImplodeTransformer(', '))->transform($request->tags)]);
AppServiceProvider@boot for global responses:
Response::macro('transform', fn($response, $transformer) =>
$response->setContent($transformer->transform($response->content()))
);
composer.json and server meet the new minimum (^8.5/^8.0). Test thoroughly if using legacy Symfony components.Symfony\Component\HttpFoundation\File\UploadedFile v7 methods).Transformer Order Matters: Chaining transformers may produce unexpected results if order isn’t considered. Example:
// ❌ Fails: Slugify after truncate loses characters
$data->map(fn($s) => (new TruncateTransformer(10))->transform(
(new SlugifyTransformer())->transform($s)
));
Fix: Reverse the order or use a custom transformer.
Performance with Large Data:
array_map_transformer processes items sequentially. For huge arrays, consider batching:
$batchSize = 1000;
array_map([$transformer, 'transform'], array_chunk($data, $batchSize));
SlugifyTransformer Quirks:
--- becomes ---).->slugify($string, ['separator' => '-']) for custom separators.validate() method to your custom transformers:
public function validate($data) {
if (!is_array($data)) throw new \InvalidArgumentException('Expected array');
}
class LoggingTransformer implements TransformerInterface {
public function transform($data) {
\Log::debug('Transformer input', ['data' => $data]);
return $this->transformer->transform($data);
}
}
# config/services.yaml
services:
App\Transformers\CustomTransformer:
tags: ['package_name.transformer']
BaseTransformer for reusable logic:
class TitleCaseTransformer extends BaseTransformer {
public function transform($string) {
return ucwords(strtolower($string));
}
}
$result = app(\Illuminate\Pipeline\Pipeline::class)
->send($data)
->through([
new SlugifyTransformer(),
new TruncateTransformer(50),
])
->thenReturn();
package_name.transforming events to intercept/modify data:
event(new Transforming($data, $transformer));
NO_UPDATE_NEEDED would **not** apply here due to the breaking changes and new features.
How can I help you explore Laravel packages today?