netmex/hydrator-bundle
最新稳定版本:0.2.0
Composer 安装命令:
composer require netmex/hydrator-bundle
包简介
README 文档
README
About
The Netmex HydratorBundle is a Symfony bundle designed to simplify the process of hydrating data transfer objects (DTOs) from arrays or JSON inputs with built-in support for data transformation and validation using Symfony constraints.
It enables you to:
- Define mappers that transform and validate input data.
- Easily create reusable transformers to convert raw data (e.g., capitalization, formatting).
- Handle validation errors gracefully.
- Integrate seamlessly into Symfony controllers for clean and maintainable code.
Installation
composer require netmex/hydrator-bundle
Usage
Create a mapper class that implements
Netmex\HydratorBundle\Contracts\MapperDefinitionInterface.
Example Hydrator
<?php namespace App\Hydrator; use App\Entity\MyClass; use Netmex\HydratorBundle\Contracts\BuilderInterface; use Netmex\HydratorBundle\Contracts\MapperDefinitionInterface; use Netmex\HydratorBundle\Options\OptionsResolver; use Symfony\Component\Validator\Constraints\NotBlank; use Symfony\Component\Validator\Constraints\Length; use App\Transformer\CapitalizationTransformer; class Hydrator implements MapperDefinitionInterface { public function process(BuilderInterface $builder): void { $builder ->add('key', CapitalizationTransformer::class, [ NotBlank::class => null, Length::class => ['min' => 6, 'max' => 7], ]); } public function options(OptionsResolver $resolver): void { $resolver->setDefaults([ 'model' => MyClass::class, ]); } }
Create a Transformer
<?php namespace App\Transformer; use Netmex\HydratorBundle\Contracts\TransformerInterface; class CapitalizationTransformer implements TransformerInterface { public function transform(string $data): string { return strtoupper($data); } }
Injecting the Mapper into a Controller
<?php namespace App\Controller; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\Routing\Annotation\Route; use App\Hydrator\Hydrator; use Netmex\HydratorBundle\Contracts\MapperInterface; use Netmex\HydratorBundle\Exception\ValidationFailedException; class ExampleController { #[Route('/example', name: 'app_example')] public function index(Request $request, MapperInterface $hydrator): Response { $jsonContent = $request->getContent(); $data = json_decode($jsonContent, true); if (json_last_error() !== JSON_ERROR_NONE) { return new JsonResponse(['error' => 'Invalid JSON'], 400); } try { $result = $hydrator->build(Hydrator::class, $data); } catch (ValidationFailedException $e) { return new JsonResponse(['errors' => (string) $e->getViolations()], 400); } return new JsonResponse($result); } }
Recommended Directory Layout
src/
├── Hydrator/
│ └── Hydrator.php
├── Transformer/
│ └── CapitalizationTransformer.php
└── Controller/
└── ExampleController.php
Exception Handling
try { $data = $hydrator->build(Hydrator::class, $requestData); } catch (ValidationFailedException $e) { return new JsonResponse(['errors' => (string) $e->getViolations()], 400); }
More about Constraints
See Symfony Validation Constraints for available options.
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-04