Skip to content
This repository has been archived by the owner on Jun 19, 2022. It is now read-only.

Commit

Permalink
update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
fd6130 committed Jun 5, 2021
1 parent 3b0a666 commit 3008455
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 249 deletions.
125 changes: 1 addition & 124 deletions src/Resources/doc/dto_mapper.md
Original file line number Diff line number Diff line change
@@ -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, <Your Entity 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, <Your Entity Class>::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, <Your Entity From Database>);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->flush();
return $this->json("Update success");
}
```
3 changes: 0 additions & 3 deletions src/Resources/doc/file_upload.md

This file was deleted.

28 changes: 1 addition & 27 deletions src/Resources/doc/pagination.md
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
```
You can change default limit in `fd_hsl.yaml`.
97 changes: 2 additions & 95 deletions src/Resources/doc/transformer.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

```
Create a transformer class without entity `php bin/console make:hsl:transformer --no-entity`.

0 comments on commit 3008455

Please sign in to comment.