bernard/normalt
Extra normalizers for Symfony’s Serializer plus an AggregateNormalizer delegator that selects the first supporting normalizer/denormalizer. Focuses on object-to-array normalization and array-to-object denormalization, with options like Doctrine and reflection-based normalizers.
symfony/serializer (bundled since Laravel 5.5). This ensures minimal architectural disruption, as Normalt operates within the existing serialization stack.JsonResource, GraphQL resolvers) where normalization is a bottleneck.DoctrineNormalizer bridges Laravel’s Eloquent ORM with Symfony’s normalization pipeline, enabling seamless entity hydration/dehydration. Critical for:
toArray() in JsonResource).AggregateNormalizer allows mixing Normalt’s normalizers with Symfony’s (e.g., GetSetMethodNormalizer) or custom ones, future-proofing the integration.NormalizerInterface changes).
NormalizerInterface and DenormalizerInterface).AppServiceProvider:
$this->app->bind(NormalizerInterface::class, function ($app) {
return new AggregateNormalizer([
new DoctrineNormalizer($app->make(EntityManager::class)),
new GetSetMethodNormalizer(),
]);
});
DoctrineNormalizer works with Doctrine’s EntityManager. Workaround: Use Doctrine’s EntityManager for Eloquent models (via doctrine/dbal or illuminate/database).DoctrineNormalizer. Laravel’s illuminate/database already includes Doctrine classes, so no additional dependencies.RecursiveReflectionNormalizer uses reflection, which can be slow for deep object graphs (e.g., User->Posts->Comments). Benchmark against:
Arrayable trait.ObjectNormalizer (with caching enabled).ObjectNormalizer). Solution: Combine with Symfony’s ObjectNormalizer or implement a custom CircularReferenceHandler.Validator).NormalizerInterface be made compatible with Symfony 5.4+? If not, is a fork/patch feasible?Arrayable or Symfony’s ObjectNormalizer for our object graphs?DoctrineNormalizer work with Eloquent models, or do we need to use Doctrine’s EntityManager directly?toArray() in JsonResource with Normalt’s DoctrineNormalizer for Eloquent models:
public function toArray($request)
{
return resolve(NormalizerInterface::class)->normalize($this->resource);
}
User objects to arrays for GraphQL responses).$normalizer = app(NormalizerInterface::class);
$cachedData = $normalizer->normalize($model);
cache()->put("model:$id", $cachedData, now()->addHour());
symfony/serializer bundle, enabling reuse of existing encoder/decoder configurations.Validator for denormalization safety (e.g., validate arrays before denormalizing).Arrayable: Limited to normalization; no denormalization or ORM integration.toArray() methods, custom serializers).toArray(), Arrayable).composer.json (with Symfony 5 patch if needed).AppServiceProvider:
$this->app->bind(NormalizerInterface::class, function ($app) {
return new AggregateNormalizer([
new DoctrineNormalizer($app->make(EntityManager::class)),
new GetSetMethodNormalizer(),
// Add custom normalizers if needed
]);
});
toArray() in a non-critical JsonResource with Normalt’s normalizer.toArray() methods across API resources.DoctrineNormalizer for entities).NormalizerInterface and DenormalizerInterface implementations to match Symfony 5.4+.Serializer component (e.g., symfony/serializer:^5.4).RecursiveReflectionNormalizer to handle PHP 8’s constructor property promotion.EntityManager. For Eloquent, use Doctrine’s EntityManager via doctrine/dbal or wrap Eloquent models in Doctrine entities temporarily.How can I help you explore Laravel packages today?