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');
/* Start to develop here. Best regards https://php-download.com/ */
nelexa / request-dto-bundle example snippets
use Nelexa\RequestDtoBundle\Dto\RequestObjectInterface;
use Symfony\Component\Validator\Constraints as Assert;
class UserRegistrationRequest implements RequestObjectInterface
{
/** @Assert\NotBlank() */
public ?string $login = null;
/**
* @Assert\NotBlank()
* @Assert\Length(min="6")
*/
public ?string $password = null;
/**
* @Assert\NotBlank()
* @Assert\Email()
*/
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
{
/**
* @Route("/sign-up", methods={"POST"})
*/
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);
}
}
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class AppController extends AbstractController{
/**
* @Route("/sign-up", methods={"POST"})
*/
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
{
/** @Assert\Range(min=1) */
private int $page;
/**
* @Assert\NotBlank
* @Assert\Regex("~^\d{10,13}$~", message="Invalid phone number")
*/
private string $phone;
public function __construct(Request $request)
{
$this->page = $request->request->getInt('p', 1);
// sanitize phone number
$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);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.