PHP code example of nelexa / request-dto-bundle
1. Go to this page and download the library: Download nelexa/request-dto-bundle library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once ('vendor/autoload.php' );
nelexa / request-dto-bundle example snippets
use Nelexa \RequestDtoBundle \Dto \RequestObjectInterface ;
use Symfony \Component \Validator \Constraints as Assert ;
class UserRegistrationRequest implements RequestObjectInterface
{
public ?string $login = null ;
public ?string $password = null ;
public ?string $email = null ;
}
declare (strict_types=1 );
use Symfony \Bundle \FrameworkBundle \Controller \AbstractController ;
use Symfony \Component \HttpFoundation \Response ;
use Symfony \Component \Routing \Annotation \Route ;
use Symfony \Component \Validator \ConstraintViolationListInterface ;
class AppController extends AbstractController
{
public function registration (
UserRegistrationRequest $userRegistrationRequest,
ConstraintViolationListInterface $errors
) : Response {
$data = ['success' => $errors->count() === 0 ];
if ($errors->count() > 0 ){
$data['errors' ] = $errors;
}
else {
$data['data' ] = $userRegistrationRequest;
}
return $this ->json($data);
}
}
...
public function registration (
UserRegistrationRequest $userRegistrationRequest,
?ConstraintViolationListInterface $errors
) : Response {
return $this ->json(
[
'success' => $errors === null ,
'errors' => $errors,
]
);
}
...
use Symfony \Bundle \FrameworkBundle \Controller \AbstractController ;
use Symfony \Component \HttpFoundation \Response ;
use Symfony \Component \Routing \Annotation \Route ;
class AppController extends AbstractController {
public function registration (UserRegistrationRequest $userRegistrationRequest) : Response {
return $this ->json(['success' => true ]);
}
}
use Nelexa \RequestDtoBundle \Dto \ConstructRequestObjectInterface ;
use Symfony \Bundle \FrameworkBundle \Controller \AbstractController ;
use Symfony \Component \HttpFoundation \Request ;
use Symfony \Component \HttpFoundation \Response ;
use Symfony \Component \Validator \Constraints as Assert ;
use Symfony \Component \Validator \ConstraintViolationListInterface ;
class ExampleDTO implements ConstructRequestObjectInterface
{
private int $page;
private string $phone;
public function __construct (Request $request)
{
$this ->page = $request->request->getInt('p' , 1 );
$phone = (string) $request->request->get('phone' );
$phone = preg_replace('~\D~' , '' , $phone);
$this ->phone = (string) $phone;
}
public function getPage () : int
{
return $this ->page;
}
public function getPhone () : string
{
return $this ->phone;
}
}
class AppController extends AbstractController
{
public function exampleAction (
ExampleDTO $dto,
ConstraintViolationListInterface $errors
) : Response {
$data = [
'page' => $dto->getPage(),
'phone' => $dto->getPhone(),
'errors' => $errors,
];
return $this ->json($data, $errors->count() === 0 ? 200 : 400 );
}
}