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

Commit ec6b7f5

Browse files
authored
Merge pull request #3 from fd6130/updates
Update for version 1.1.0
2 parents 8e6835d + 34e735c commit ec6b7f5

File tree

10 files changed

+84
-55
lines changed

10 files changed

+84
-55
lines changed

README.md

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,12 @@
11
# hsl-bundle
22

3-
hsl-bundle provide you some nice feature that you might / might not needed:
3+
hsl-bundle provide you some nice feature that can help you to speed up your development:
44

55
- maker command for DTOs (DTOs are use for POST and PUT request)
66
- maker command for Transformers (customize your result)
77
- maker command for API CRUD Controller (boilerplate code for your need)
88
- pagination (it is integrated during the controller creation)
99

10-
## For php 8 user
11-
12-
There is one library that will cause error in php 8, for temporarily solution please add this code to your composer.json before require this bundle:
13-
14-
```json
15-
"repositories": [
16-
{
17-
"type": "vcs",
18-
"url": "https://github.com/fd6130/api-filter-bundle.git"
19-
}
20-
]
2110
```
2211
2312
## Prerequisites

composer.json

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "fd6130/hsl-bundle",
3-
"description": "This bundle provide some ease of development for Symfony beginner.",
3+
"description": "This bundle provide some ease of development for Symfony beginner. You can now create a CRUD controller very fast and it comes with DTO validation, mapping and pagination.",
4+
"keywords": ["Symfony", "symfony", "bundle", "DTO", "dto", "dto validation", "dto mapping", "pagination", "controller"],
45
"type": "symfony-bundle",
56
"license": "MIT",
67
"autoload": {
@@ -10,34 +11,24 @@
1011
},
1112
"require": {
1213
"php": ">=7.2.5",
13-
"samj/fractal-bundle": "^4.0",
1414
"babdev/pagerfanta-bundle": "^3.0",
1515
"mark-gerarts/automapper-plus-bundle": "^1.2",
16-
"fd6130/api-filter-bundle": "1.2.*",
17-
"symfony-bundles/json-request-bundle": "^4.0",
16+
"monterhealth/api-filter-bundle": "1.2.*",
1817
"pagerfanta/pagerfanta": "^3.0",
18+
"symfony-bundles/json-request-bundle": "^4.0",
19+
"samj/fractal-bundle": "^4.0",
1920
"symfony/validator": "^4.4|^5.0",
20-
"doctrine/annotations": "^1.13"
21+
"symfony/yaml": "^4.4|^5.0",
22+
"doctrine/inflector": "^2.0"
2123
},
2224
"require-dev": {
23-
"symfony/maker-bundle": "^1.0",
24-
"easycorp/easy-deploy-bundle": "^1.0"
25+
"symfony/maker-bundle": "^1.0"
2526
},
2627
"suggest": {
28+
"easycorp/easy-deploy-bundle": "For easy deploy",
2729
"vich/uploader-bundle": "For file upload",
2830
"lexik/jwt-authentication-bundle": "Install this if you need JWT authentication",
29-
"etnynl/jwt-refresh-token-bundle": "Install this if you need JWT refresh token",
30-
"symfony/webpack-encore-bundle": "Install this if you need Webpack to manage your assets",
31-
"twig/cssinliner-extra": "For email css",
32-
"twig/extra-bundle": "For email css",
33-
"twig/inky-extra": "For email css",
3431
"nesbot/carbon": "Easy format your datetime",
3532
"nelmio/cors-bundle": "CORS management"
36-
},
37-
"repositories": [
38-
{
39-
"type": "vcs",
40-
"url": "https://github.com/fd6130/api-filter-bundle.git"
41-
}
42-
]
33+
}
4334
}

src/Dto/DtoRequestResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function resolve(Request $request, ArgumentMetadata $argument)
6060
$errorMessages [] = $error->getPropertyPath() . ' => ' . $error->getMessage();
6161
}
6262

63-
throw new DtoValidationException(sprintf("Fail to pass DTO validation at '%s'", get_class($dto)), $errorMessages);
63+
throw new DtoValidationException('', $errorMessages);
6464
}
6565

6666
yield $dto;

src/Dto/Validator/DtoValidator.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Fd\HslBundle\Dto\Validator;
4+
5+
use Fd\HslBundle\Dto\DtoRequestInterface;
6+
use Fd\HslBundle\Exception\DtoValidationException;
7+
use Symfony\Component\Validator\Validator\ValidatorInterface;
8+
9+
class DtoValidator implements DtoValidatorInterface
10+
{
11+
private $validator;
12+
13+
public function __construct(ValidatorInterface $validator)
14+
{
15+
$this->validator = $validator;
16+
}
17+
18+
/**
19+
* @param DtoRequestInterface $dto The data transfer object class
20+
*
21+
* @throws DtoValidationException
22+
*/
23+
public function validate(DtoRequestInterface $dto)
24+
{
25+
$errors = $this->validator->validate($dto);
26+
27+
if(count($errors) > 0)
28+
{
29+
$errorMessages = [];
30+
31+
foreach($errors as $error)
32+
{
33+
$errorMessages [] = $error->getPropertyPath() . ' => ' . $error->getMessage();
34+
}
35+
36+
throw new DtoValidationException($errorMessages);
37+
}
38+
}
39+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Fd\HslBundle\Dto\Validator;
4+
5+
use Fd\HslBundle\Dto\DtoRequestInterface;
6+
7+
interface DtoValidatorInterface
8+
{
9+
/**
10+
* @param DtoRequestInterface $dto The data transfer object class
11+
*
12+
* @throws DtoValidationException
13+
*/
14+
public function validate(DtoRequestInterface $dto);
15+
}

src/Exception/DtoValidationException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ class DtoValidationException extends HttpException
1010
protected $context = [];
1111

1212
/**
13-
* @param null|string|array $message Message for this error
13+
* @param string|array $message
1414
*/
1515
public function __construct(?string $message = '', array $context = [])
1616
{
17-
$message = $message ? $message : 'DTO validation fail.';
17+
$message = !empty($message) ? $message : 'DTO validation fail.';
1818
$this->context = $context;
1919

2020
parent::__construct(400, $message, null, [] , 0);

src/Maker/MakeHslCrud.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ public function __construct(DoctrineHelper $doctrineHelper)
3535
{
3636
$this->doctrineHelper = $doctrineHelper;
3737

38-
if (class_exists(InflectorFactory::class)) {
39-
$this->inflector = InflectorFactory::create()->build();
40-
}
38+
$this->inflector = InflectorFactory::create()->build();
4139
}
4240

4341
public static function getCommandName(): string
@@ -47,7 +45,7 @@ public static function getCommandName(): string
4745

4846
public static function getCommandDescription(): string
4947
{
50-
return 'Creates a new controller class';
48+
return 'Create a generic CRUD controller.';
5149
}
5250

5351
/**
@@ -174,19 +172,11 @@ public function configureDependencies(DependencyBuilder $dependencies)
174172

175173
private function pluralize(string $word): string
176174
{
177-
if (null !== $this->inflector) {
178-
return $this->inflector->pluralize($word);
179-
}
180-
181-
return LegacyInflector::pluralize($word);
175+
return $this->inflector->pluralize($word);
182176
}
183177

184178
private function singularize(string $word): string
185179
{
186-
if (null !== $this->inflector) {
187-
return $this->inflector->singularize($word);
188-
}
189-
190-
return LegacyInflector::singularize($word);
180+
return $this->inflector->singularize($word);
191181
}
192182
}

src/Maker/MakeHslDto.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,9 @@ public static function getCommandName(): string
4242

4343
public static function getCommandDescription(): string
4444
{
45-
return 'Creates a new dto class';
45+
return 'Create a data transfer object class for mapping purpose.';
4646
}
4747

48-
4948
public function configureCommand(Command $command, InputConfiguration $inputConf)
5049
{
5150
$command

src/Maker/MakeHslTransformer.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,9 @@ public static function getCommandName(): string
4949

5050
public static function getCommandDescription(): string
5151
{
52-
return 'Creates a new transformer class';
52+
return 'Create a transformer class for transforming data to your flavour.';
5353
}
5454

55-
5655
public function configureCommand(Command $command, InputConfiguration $inputConf)
5756
{
5857
$command

src/Resources/config/services.yaml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,24 @@ services:
1414
tags: ["maker.command"]
1515

1616
# Dto services
17-
fdhsl.dto.dto_request_resolver:
18-
class: Fd\HslBundle\Dto\DtoRequestResolver
17+
Fd\HslBundle\Dto\DtoRequestResolver:
1918
arguments: ["@validator"]
2019
tags:
2120
- { name: "controller.argument_value_resolver", priority: 50 }
2221

22+
fdhsl.dto.validator.dto_validator:
23+
class: Fd\HslBundle\Dto\Validator\DtoValidator
24+
arguments: ["@validator"]
25+
Fd\HslBundle\Dto\Validator\DtoValidator:
26+
arguments: ["@validator"]
27+
Fd\HslBundle\Dto\Validator\DtoValidatorInterface: "@fdhsl.dto.validator.dto_validator"
28+
2329
# paginator
2430
fdhsl.pagination.paginator:
2531
class: Fd\HslBundle\Pagination\Paginator
2632
arguments: ["@router", "@request_stack"]
27-
33+
Fd\HslBundle\Pagination\Paginator:
34+
arguments: ["@router", "@request_stack"]
2835
Fd\HslBundle\Pagination\PaginatorInterface: "@fdhsl.pagination.paginator"
2936

3037
# DtoValidationException

0 commit comments

Comments
 (0)