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

Commit 3008455

Browse files
author
fd6130
committed
update docs
1 parent 3b0a666 commit 3008455

File tree

4 files changed

+4
-249
lines changed

4 files changed

+4
-249
lines changed

src/Resources/doc/dto_mapper.md

Lines changed: 1 addition & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,131 +1,8 @@
11
# Dto Mapper
22

3-
We use dto to handle/validate user input value and use Mapper to map the dto into entity.
3+
We use dto to validate user input value and use Mapper to map the dto into entity.
44

55
## Usage
66

77
To create a new dto class, execute the command `php bin/console make:hsl:dto`.
88

9-
**mapper require you to have an existing entity.**
10-
11-
**Command will generate these for you, you don't have to copy paste from here.**
12-
13-
Dto class:
14-
15-
```
16-
namespace App\Dto\Input;
17-
18-
use Fd\HslBundle\DtoRequestInterface;
19-
use Symfony\Component\HttpFoundation\File\File;
20-
use Symfony\Component\HttpFoundation\Request;
21-
use Symfony\Component\Validator\Constraints as Assert;
22-
23-
class ExampleInput implements DtoRequestInterface
24-
{
25-
/**
26-
* If you need validation, use @Assert.
27-
*
28-
* @Assert\NotBlank(message="name cannot be blank.")
29-
*/
30-
public $name;
31-
32-
public function __construct(Request $request)
33-
{
34-
$this->name = $request->get('name');
35-
36-
}
37-
}
38-
```
39-
40-
Mapper class:
41-
42-
```
43-
namespace App\Dto\Mapper;
44-
45-
use App\Dto\Input\ExampleInput;
46-
use AutoMapperPlus\AutoMapperPlusBundle\AutoMapperConfiguratorInterface;
47-
use AutoMapperPlus\Configuration\AutoMapperConfigInterface;
48-
use AutoMapperPlus\CustomMapper\CustomMapper;
49-
use Doctrine\ORM\EntityManagerInterface;
50-
51-
class ExampleMapperConfig extends CustomMapper implements AutoMapperConfiguratorInterface
52-
{
53-
/**
54-
* For use in across methods
55-
*
56-
* @var ExampleInput
57-
*/
58-
private $source;
59-
60-
/**
61-
* For use in across methods
62-
*
63-
* @var your entity class
64-
*/
65-
private $destination;
66-
67-
private $entityManager;
68-
69-
public function __construct(EntityManagerInterface $entityManager)
70-
{
71-
$this->entityManager = $entityManager;
72-
}
73-
74-
public function configure(AutoMapperConfigInterface $config): void
75-
{
76-
$config->registerMapping(ExampleInput::class, <Your Entity Class>::class)
77-
->useCustomMapper($this);
78-
}
79-
80-
/**
81-
* @param ExampleInput $source
82-
* @param your entity class $destination
83-
*/
84-
public function mapToObject($source, $destination)
85-
{
86-
// uncomment these if you want to use these variable at other method within this class.
87-
// $this->source = $source;
88-
// $this->destination = $destination;
89-
90-
// do the mapping from source to destination
91-
$destination->setName($source->name);
92-
93-
return $destination;
94-
}
95-
}
96-
97-
```
98-
99-
### Use in controller
100-
101-
```
102-
use AutoMapperPlus\AutoMapperInterface;
103-
use Fd\HslBundle\Fractal\FractalTrait;
104-
105-
// your controller class
106-
107-
public function create(ExampleInput $dto, AutoMapperInterface $mapper)
108-
{
109-
$newEntity = $mapper->map($dto, <Your Entity Class>::class)
110-
111-
$entityManager = $this->getDoctrine()->getManager();
112-
$entityManager->persist($newEntity);
113-
$entityManager->flush();
114-
115-
return $this->json("Create success");
116-
}
117-
118-
public function update(ExampleInput $dto, AutoMapperInterface $mapper)
119-
{
120-
// You can fetch entity from database and map from dto.
121-
// For example: $repository->find()
122-
$mapper->mapToObject($dto, <Your Entity From Database>);
123-
124-
$entityManager = $this->getDoctrine()->getManager();
125-
$entityManager->flush();
126-
127-
return $this->json("Update success");
128-
}
129-
130-
131-
```

src/Resources/doc/file_upload.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Resources/doc/pagination.md

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,4 @@
22

33
To paginate your result, inject `PaginatorInterface` to your controller and call `paginate()` with query builder/array and transformer class.
44

5-
```
6-
use Fd\HslBundle\Fractal\FractalTrait;
7-
use Fd\HslBundle\Pagination\PaginatorInterface;
8-
use League\Fractal\Manager;
9-
10-
// your controller class
11-
12-
use FractalTrait;
13-
14-
public function __construct(Manager $manager)
15-
{
16-
$this->fractal = $manager;
17-
}
18-
19-
public function index(PaginatorInterface $paginator)
20-
{
21-
$entityManager = $this->getDoctrine()->getManager();
22-
23-
$repository = $entityManager->getRepository(SomeClass::class);
24-
25-
//You can pass array or query builder to first parameter.
26-
$data = $paginator->paginate($repository->findAll(), SomeTransformer::class);
27-
28-
return $this->json($this->fractal()->createData($data)->toArray());
29-
}
30-
31-
```
5+
You can change default limit in `fd_hsl.yaml`.

src/Resources/doc/transformer.md

Lines changed: 2 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -2,101 +2,8 @@
22

33
You can customize your result using transformer class.
44

5-
65
## Usage
76

8-
To create a new transformer class, execute the command `php bin/console make:hsl:transformer`.
9-
10-
**That command require you to have an existing entity.**
11-
12-
Or you can create a transformer class without entity `php bin/console make:hsl:transformer --no-entity`.
13-
14-
**Command will generate these for you, you don't have to copy paste from here.**
15-
16-
```
17-
namespace App\Transformer;
18-
19-
use League\Fractal\TransformerAbstract;
20-
21-
/**
22-
* Transformer are use to decorate your custom output data before serialize it to JSON.
23-
*
24-
* Fractal docs: https://fractal.thephpleague.com/transformers
25-
* Bundle docs: https://github.com/samjarrett/FractalBundle
26-
*/
27-
class ExampleTransformer extends TransformerAbstract
28-
{
29-
/**
30-
* List of resources possible to include
31-
*
32-
* @var array
33-
*/
34-
protected $availableIncludes = [];
35-
36-
/**
37-
* List of resources to automatically include
38-
*
39-
* @var array
40-
*/
41-
protected $defaultIncludes = [];
42-
43-
/*
44-
* Add whatever properties & methods you need to hold the
45-
* data for this message class.
46-
*/
47-
public function transform(?$example)
48-
{
49-
// Decorate your return data in array form.
50-
return $example ? [
51-
'id' => $example->getId()
52-
] : null;
53-
}
54-
55-
/**
56-
* Write this function if you have declare something in $availableIncludes or $defaultIncludes
57-
*
58-
* Example: If you include 'user', the method name and its parameter will be 'public function includeUser(User $user)'
59-
*/
60-
// public function includeExample(/** entity class */)
61-
// {
62-
// return $this->item(/** entity class */, /** transformer class */);
63-
// }
64-
65-
}
66-
```
67-
68-
### Use in controller
69-
70-
```
71-
use App\Transformer\ExampleTransformer;
72-
use Fd\HslBundle\Fractal\FractalTrait;
73-
use League\Fractal\Manager;
74-
use League\Fractal\Resource\Collection;
75-
use League\Fractal\Resource\Item;
76-
77-
// your controller class
78-
79-
use FractalTrait;
80-
81-
public function __construct(Manager $manager)
82-
{
83-
$this->fractal = $manager;
84-
}
85-
86-
public function index()
87-
{
88-
$entityManager = $this->getDoctrine()->getManager();
89-
90-
$repository = $entityManager->getRepository(SomeClass::class);
91-
92-
// As a collection
93-
$data = new Collection($repository->findAll(), ExampleTransformer::class);
94-
95-
// Or as an item
96-
// $data = new Item($repository->findAll(), ExampleTransformer::class);
97-
98-
return $this->json($this->fractal()->createData($data)->toArray());
99-
}
100-
7+
Create a new transformer class, execute the command `php bin/console make:hsl:transformer`.
1018

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

0 commit comments

Comments
 (0)