phrity/util-transformer
Lightweight PHP utility for transforming values between types. Provides transformers with canTransform()/transform(), plus resolvers to chain and recurse converters. Includes JSON/flatten decoders and converters for basic types, DateTime, enums, Stringable, Throwable and more.
Strengths:
canTransform()/transform() interface is intuitive and mirrors Laravel’s own type handling (e.g., in API resources, form requests, or validation). It can seamlessly integrate with Laravel’s Illuminate\Support\Collection, Illuminate\Database\Eloquent\Model, or custom DTOs.SymfonyNormalizerWrapper bridges this package with Laravel’s ecosystem (e.g., symfony/serializer is already used in Laravel’s HTTP message components). This reduces friction for teams familiar with Symfony components.RecursionResolver is particularly valuable for Laravel applications dealing with nested data (e.g., Eloquent relationships, JSON APIs, or form data normalization). It can replace manual recursive logic in API responses or request payloads.FlattenDecoder and JsonDecoder address common Laravel use cases, such as flattening nested arrays for storage (e.g., Redis, databases) or parsing JSON payloads in API consumers.Weaknesses:
RecursionResolver) may introduce latency for deeply nested structures. Benchmarking would be critical for high-throughput APIs.toArray()/toJson() logic in Illuminate\Http\Resources\Json\JsonResource with this package’s transformers for consistent serialization.StringResolver or FirstMatchResolver to normalize request payloads before validation (e.g., converting DateTime strings to Carbon instances).FlattenDecoder for Elasticsearch mappings).phpunit assertions or Pest tests to normalize expected/actual values.StringResolver for API response headers).$this->app->bind(TransformerInterface::class, function ($app) {
return new FirstMatchResolver([
new DateTimeConverter(),
new EnumConverter(),
// ...
]);
});
DateTime to a Boolean). Static analysis tools (e.g., PHPStan) should be used to validate transformer chains.composer.json and monitor for updates.Carbon instances) may require custom transformers, adding maintenance overhead.spatie/array-to-object, nesbot/carbon) that overlap with this package’s functionality?canTransform() checks in high-traffic APIs?Assert::assertTransformsTo()) that can be extended?Type constants map to Laravel’s native types (e.g., Carbon, Collection)?JsonResource serialization with RecursionResolver + FirstMatchResolver for consistent output formats.StringResolver or JsonDecoder in middleware/form requests to normalize input (e.g., convert DateTime strings to Carbon).FlattenDecoder for storing nested data in relational databases or JsonDecoder for JSON columns.StringResolver for Redis/Memcached.SymfonyNormalizerWrapper can unify Laravel’s use of symfony/serializer with this package’s transformers.laravel/validation to normalize data before rules are applied.laravel/pint or phpunit to normalize test outputs.toArray() logic in JsonResource with RecursionResolver.// Before
public function toArray($request) {
return [
'id' => $this->id,
'created_at' => $this->created_at->format('Y-m-d'),
'relationship' => $this->relationship->toArray(),
];
}
// After
public function toArray($request) {
$transformer = app(TransformerInterface::class);
return $transformer->transform($this->resource, Type::ARRAY);
}
JsonDecoder.FlattenDecoder for database storage optimizations.deprecated() helper.symfony/serializer is a soft dependency (only for SymfonyNormalizerWrapper). Laravel already includes this via illuminate/http.Type constants to include Laravel-specific types (e.g., Type::CARBON).CarbonConverter, CollectionToArray).JsonResource serialization logic.FlattenDecoder/JsonDecoder for database/storage.JsonResource classes).FirstMatchResolver enforce uniform type handling across the application.How can I help you explore Laravel packages today?