diff --git a/src/Resources/doc/dto_mapper.md b/src/Resources/doc/dto_mapper.md index 72bc53b..6c219fc 100755 --- a/src/Resources/doc/dto_mapper.md +++ b/src/Resources/doc/dto_mapper.md @@ -1,131 +1,8 @@ # Dto Mapper -We use dto to handle/validate user input value and use Mapper to map the dto into entity. +We use dto to validate user input value and use Mapper to map the dto into entity. ## Usage To create a new dto class, execute the command `php bin/console make:hsl:dto`. -**mapper require you to have an existing entity.** - -**Command will generate these for you, you don't have to copy paste from here.** - -Dto class: - -``` -namespace App\Dto\Input; - -use Fd\HslBundle\DtoRequestInterface; -use Symfony\Component\HttpFoundation\File\File; -use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\Validator\Constraints as Assert; - -class ExampleInput implements DtoRequestInterface -{ - /** - * If you need validation, use @Assert. - * - * @Assert\NotBlank(message="name cannot be blank.") - */ - public $name; - - public function __construct(Request $request) - { - $this->name = $request->get('name'); - - } -} -``` - -Mapper class: - -``` -namespace App\Dto\Mapper; - -use App\Dto\Input\ExampleInput; -use AutoMapperPlus\AutoMapperPlusBundle\AutoMapperConfiguratorInterface; -use AutoMapperPlus\Configuration\AutoMapperConfigInterface; -use AutoMapperPlus\CustomMapper\CustomMapper; -use Doctrine\ORM\EntityManagerInterface; - -class ExampleMapperConfig extends CustomMapper implements AutoMapperConfiguratorInterface -{ - /** - * For use in across methods - * - * @var ExampleInput - */ - private $source; - - /** - * For use in across methods - * - * @var your entity class - */ - private $destination; - - private $entityManager; - - public function __construct(EntityManagerInterface $entityManager) - { - $this->entityManager = $entityManager; - } - - public function configure(AutoMapperConfigInterface $config): void - { - $config->registerMapping(ExampleInput::class, ::class) - ->useCustomMapper($this); - } - - /** - * @param ExampleInput $source - * @param your entity class $destination - */ - public function mapToObject($source, $destination) - { - // uncomment these if you want to use these variable at other method within this class. - // $this->source = $source; - // $this->destination = $destination; - - // do the mapping from source to destination - $destination->setName($source->name); - - return $destination; - } -} - -``` - -### Use in controller - -``` -use AutoMapperPlus\AutoMapperInterface; -use Fd\HslBundle\Fractal\FractalTrait; - -// your controller class - -public function create(ExampleInput $dto, AutoMapperInterface $mapper) -{ - $newEntity = $mapper->map($dto, ::class) - - $entityManager = $this->getDoctrine()->getManager(); - $entityManager->persist($newEntity); - $entityManager->flush(); - - return $this->json("Create success"); -} - -public function update(ExampleInput $dto, AutoMapperInterface $mapper) -{ - // You can fetch entity from database and map from dto. - // For example: $repository->find() - $mapper->mapToObject($dto, ); - - $entityManager = $this->getDoctrine()->getManager(); - $entityManager->flush(); - - return $this->json("Update success"); -} - - -``` \ No newline at end of file diff --git a/src/Resources/doc/file_upload.md b/src/Resources/doc/file_upload.md deleted file mode 100755 index e6ef72e..0000000 --- a/src/Resources/doc/file_upload.md +++ /dev/null @@ -1,3 +0,0 @@ -# File Upload - -We use VichUploaderBundle to manage file upload. \ No newline at end of file diff --git a/src/Resources/doc/pagination.md b/src/Resources/doc/pagination.md index 9f5cb45..e140024 100755 --- a/src/Resources/doc/pagination.md +++ b/src/Resources/doc/pagination.md @@ -2,30 +2,4 @@ To paginate your result, inject `PaginatorInterface` to your controller and call `paginate()` with query builder/array and transformer class. -``` -use Fd\HslBundle\Fractal\FractalTrait; -use Fd\HslBundle\Pagination\PaginatorInterface; -use League\Fractal\Manager; - -// your controller class - -use FractalTrait; - -public function __construct(Manager $manager) -{ - $this->fractal = $manager; -} - -public function index(PaginatorInterface $paginator) -{ - $entityManager = $this->getDoctrine()->getManager(); - - $repository = $entityManager->getRepository(SomeClass::class); - - //You can pass array or query builder to first parameter. - $data = $paginator->paginate($repository->findAll(), SomeTransformer::class); - - return $this->json($this->fractal()->createData($data)->toArray()); -} - -``` \ No newline at end of file +You can change default limit in `fd_hsl.yaml`. \ No newline at end of file diff --git a/src/Resources/doc/transformer.md b/src/Resources/doc/transformer.md index 92ae235..b12b479 100755 --- a/src/Resources/doc/transformer.md +++ b/src/Resources/doc/transformer.md @@ -2,101 +2,8 @@ You can customize your result using transformer class. - ## Usage -To create a new transformer class, execute the command `php bin/console make:hsl:transformer`. - -**That command require you to have an existing entity.** - -Or you can create a transformer class without entity `php bin/console make:hsl:transformer --no-entity`. - -**Command will generate these for you, you don't have to copy paste from here.** - -``` -namespace App\Transformer; - -use League\Fractal\TransformerAbstract; - -/** - * Transformer are use to decorate your custom output data before serialize it to JSON. - * - * Fractal docs: https://fractal.thephpleague.com/transformers - * Bundle docs: https://github.com/samjarrett/FractalBundle - */ -class ExampleTransformer extends TransformerAbstract -{ - /** - * List of resources possible to include - * - * @var array - */ - protected $availableIncludes = []; - - /** - * List of resources to automatically include - * - * @var array - */ - protected $defaultIncludes = []; - - /* - * Add whatever properties & methods you need to hold the - * data for this message class. - */ - public function transform(?$example) - { - // Decorate your return data in array form. - return $example ? [ - 'id' => $example->getId() - ] : null; - } - - /** - * Write this function if you have declare something in $availableIncludes or $defaultIncludes - * - * Example: If you include 'user', the method name and its parameter will be 'public function includeUser(User $user)' - */ -// public function includeExample(/** entity class */) -// { -// return $this->item(/** entity class */, /** transformer class */); -// } - -} -``` - -### Use in controller - -``` -use App\Transformer\ExampleTransformer; -use Fd\HslBundle\Fractal\FractalTrait; -use League\Fractal\Manager; -use League\Fractal\Resource\Collection; -use League\Fractal\Resource\Item; - -// your controller class - -use FractalTrait; - -public function __construct(Manager $manager) -{ - $this->fractal = $manager; -} - -public function index() -{ - $entityManager = $this->getDoctrine()->getManager(); - - $repository = $entityManager->getRepository(SomeClass::class); - - // As a collection - $data = new Collection($repository->findAll(), ExampleTransformer::class); - - // Or as an item - // $data = new Item($repository->findAll(), ExampleTransformer::class); - - return $this->json($this->fractal()->createData($data)->toArray()); -} - +Create a new transformer class, execute the command `php bin/console make:hsl:transformer`. -``` \ No newline at end of file +Create a transformer class without entity `php bin/console make:hsl:transformer --no-entity`. \ No newline at end of file