- How do I install phrity/util-transformer in a Laravel project?
- Install it with Composer from your Laravel project root by running composer require phrity/util-transformer. The package is a framework-agnostic PHP library, so it does not require a Laravel service provider or auto-discovery step by default. After installation, register any transformer chains you need in a service provider or resolve them directly where used.
- Does phrity/util-transformer have Laravel-native integrations?
- No, the package does not ship Laravel-specific integrations such as Eloquent casts, Blade directives, or Laravel type hints. You can still use it in Laravel by registering transformers in the service container and calling them from middleware, form requests, controllers, or API resources. The SymfonyNormalizerWrapper helps if you need to bridge to Symfony serializer components that are already common in Laravel HTTP tooling.
- Which PHP and Laravel versions does phrity/util-transformer support?
- Phrity Util Transformer is a framework-agnostic Composer library rather than a Laravel package, so Laravel compatibility is mainly determined by your application's PHP and Composer constraints. Check the package's composer.json and release history on GitHub or Packagist for the required PHP version and any recent breaking changes. Because the project is relatively immature, pin an exact version in composer.json and review updates before upgrading.
- What Laravel use cases is phrity/util-transformer best suited for?
- It fits request payload normalization before validation, API resource serialization, JSON decoding, nested array flattening, and converting enums or DateTime values to strings. You can use it in form requests, middleware, controllers, or JsonResource classes where you need consistent type transformation. It is especially useful when you want composable canTransform and transform checks instead of ad hoc casting logic.
- How do canTransform() and transform() work in phrity/util-transformer?
- Every transformer exposes canTransform($subject, $targetType = null) to report whether it supports a conversion and transform($subject, $targetType = null) to perform it. You typically check canTransform first, then call transform, and you can pass an optional target type such as Type::ARRAY or Type::STRING. Resolvers like FirstMatchResolver let you stack several transformers and use the first one that can handle the subject.
- Can phrity/util-transformer handle nested arrays and recursive data?
- Yes, the RecursionResolver can apply a transformer chain recursively, which is useful for nested JSON payloads, Eloquent relationship arrays, and complex form data. It can replace manual recursive normalization logic in API responses or request parsing. For deeply nested or very large structures, add depth limits or chunk processing and benchmark memory use before using it in high-throughput endpoints.
- How do I convert enums and DateTime values to strings with this package?
- Use EnumConverter and DateTimeConverter inside a resolver such as FirstMatchResolver, along with other converters like ReadableConverter or StringableConverter as needed. The resolver checks canTransform for each converter and applies the first matching transform, optionally targeting Type::STRING. BasicTypeConverter can handle other PHP type conversions when no specialized converter applies.
- How can I register phrity/util-transformer in Laravel's service container?
- Create a service provider and bind a transformer interface or concrete resolver to a composed chain of converters, for example a FirstMatchResolver containing DateTimeConverter, EnumConverter, and BasicTypeConverter. Inject that binding into controllers, form requests, middleware, or API resources so the same transformation rules are reused across the app. You can also expose it through a facade if your team prefers that style, but the package itself does not ship Laravel-specific providers.
- What performance issues should I expect with recursive transformers in Laravel?
- Recursive transformations can add latency and memory overhead when processing deeply nested arrays, large JSON payloads, or paginated Eloquent collections. Benchmark representative payloads and impose depth limits or chunking where possible. The package does not provide built-in async transformation, so very large jobs should be moved to Laravel queues or handled outside the request cycle.
- How does phrity/util-transformer compare with Laravel API resources or symfony/serializer?
- Phrity Util Transformer is a lower-level type transformation and resolution library, not a full serialization framework, so it complements rather than fully replaces Laravel API resources or symfony/serializer. It overlaps with tools like spatie/array-to-object, nesbot/carbon for date handling, and Symfony's serializer for normalization, but its main value is composable canTransform and transform chains. The SymfonyNormalizerWrapper helps bridge to Symfony serializer components if your Laravel app already uses them.