PHP code example of rinsvent / request-bundle

1. Go to this page and download the library: Download rinsvent/request-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/ */

    

rinsvent / request-bundle example snippets


namespace App\DTO\Request;

use Rinsvent\Data2DTO\Attribute\HandleTags;
use Rinsvent\Data2DTO\Attribute\VirtualProperty;
use Symfony\Component\Validator\Constraints as Assert;
use App\Validator as ProjectAssert;

use Rinsvent\Data2DTO\Attribute\PropertyPath;
use Rinsvent\Data2DTOBundle\Service\Transformer\Request\Headers\Header;
use Rinsvent\Data2DTOBundle\Service\Transformer\Request\Headers\UserAgent;
use Rinsvent\Data2DTOBundle\Service\Transformer\Request\Server\Ip;

#[HandleTags(method: 'getTags')]
class SigninRequest
{    
    #[Assert\NotBlank(message: "error.transport.empty")]
    public string $transport;

    #[Assert\NotBlank(message: "error.value.empty")]
    #[Assert\Email(message: "error.email.wrong", groups: "email")]
    #[ProjectAssert\Phone(message: "error.phone.wrong", groups: "phone")]
    public string $value;

    #[Assert\NotBlank(message: "error.code.empty")]
    public string $code;
    #[VirtualProperty]
    public Device $device;

    public function getTags(array $data, array $tags)
    {
        $transport = $data['transport'] ?? null;
        if ($transport) {
            $tags[] = $transport;
        }
        return $tags;
    }
}

class Device
{
    #[Assert\NotBlank(message: "error.device.id.empty")]
    #[Header(property: 'X-Device-Id')]
    public string $deviceId;

    #[Assert\NotBlank(message: "error.device.source.empty")]
    #[Header(property: 'X-Source')]
    public string $source;

    #[Assert\NotBlank(message: "error.device.ip.empty")]
    #[Ip]
    public ?string $ip = null;

    #[Assert\NotBlank(message: "error.device.user_agent.empty")]
    #[UserAgent]
    public string $userAgent;
}
 

namespace App\Controller;

use App\Form\Type\User\DTO\SigninRequest;
use App\Service\Entity\UserService;
use Rinsvent\RequestBundle\Annotation\RequestDTO;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class UserController extends AbstractController
{
    public function __construct(
        private UserService $us
    ) {}

    #[Route('/v1/signin', name: 'signin', methods: ['POST'])]
    #[RequestDTO(className: SigninRequest::class, jsonPath: '$.signin')]
    public function signin(SigninRequest $signinRequest)
    {
        $signinResponse = $this->us->signin($signinRequest);

        return new JsonResponse(
            [],
            Response::HTTP_OK,
            [
                'X-Access-Token' => $signinResponse->getAccessToken(),
                'X-Refresh-Token' => $signinResponse->getRefreshToken(),
            ]
        );
    }
    
    // Вариант с несколькими DTO
    #[Route('/v1/signin', name: 'signin', methods: ['POST'])]
    #[RequestDTO(className: SigninRequest::class, jsonPath: '$.signin')]
    #[RequestDTO(className: Device::class)]
    public function signin2(SigninRequest $signinRequest, Device $device)
    {
        $signinResponse = $this->us->signin2($signinRequest, $device);
        return new JsonResponse();
    }
}