PHP code example of gepur-it / request-converter-bundle

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

    

gepur-it / request-converter-bundle example snippets




namespace App\RequestModel;

use Symfony\Component\Validator\Constraints as Assert;

/**
 * Class MyRequestModel
 * @package App\RequestModel
 */
class MyRequestModel
{
    /**
     * @Assert\Type(type="integer")
     * @Assert\NotNull()
     * @Assert\GreaterThan(value="0")
     */
    public $firstNumber;
   

    // yes, you can use default values
    /**
     * @Assert\Type(type="integer")
     * @Assert\NotNull()
     * @Assert\GreaterThanOrEqual(value="-1")
     */
    public $secondNumber = 0;

}




namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use GepurIt\RequestConverterBundle\Annotations\RequestDTO;
use Symfony\Component\HttpFoundation\Response;
use App\RequestModel\MyRequestModel;

/**
 * Class MyController
 * @package App\Controller
 */
class MyController extends AbstractController
{
     /**
     * @Route("/my_path", name="my_route")
     * @return Response
     * @RequestDTO("App\RequestModel\MyRequestModel")
     */
    public function myAction(MyRequestModel $requestModel)
    {
        // here you can work with object $requestModel.
        doSomething($requestModel->firstNumber);
        ...
    }  
}


...

     /**
     * @Route("/my_path", name="my_route")
     * @return Response
     * @RequestDTO(model="App\RequestModel\MyRequestModel", name="myName")
     */
    public function myAction(MyRequestModel $myName)
    {
        // here you can work with object $myName.
        doSomething($myName->firstNumber);
        ...
    }  

...


...

     /**
     * @Route("/my_path", name="my_route")
     * @return Response
     * @RequestDTO(model="App\RequestModel\MyRequestModel", validate=false)
     */
    public function myAction(MyRequestModel $myName)
    {
        // here you can work with object $myName.
        doSomething($myName->firstNumber);
        ...
    }  

...




namespace App\RequestModel;

use Symfony\Component\Validator\Constraints as Assert;
use GepurIt\RequestConverterBundle\Contract\RequestModelServiceInterface;

/**
 * Class MyRequestModel
 * @package App\RequestModel
 */
class MyRequestModel implements RequestModelServiceInterface
{
    /**
     * @Assert\Type(type="integer")
     * @Assert\NotNull()
     * @Assert\GreaterThan(value="0")
     */
    public $firstNumber;
   

    // yes, you can use default values
    /**
     * @Assert\Type(type="integer")
     * @Assert\NotNull()
     * @Assert\GreaterThanOrEqual(value="-1")
     */
    public $secondNumber = 0;

    /**
     * {@inheritDoc}
     **/
    public function handle()
    {
        //calculate here
    }

}




...

class MyRequestModel implements RequestModelServiceInterface
{
    ...


    /**
     * @Assert\LessThanOrEqual(value="10")
     */
    public $sum = 0;


    /**
     * {@inheritDoc}
     **/
    public function handle()
    {
        $this->sum = $this->firstNumber + $this->secondNumber;
    }
}




...

class MyRequestModel implements RequestModelServiceInterface
{
    /** @var MyCalculator $calculator */
    private $calculator;


    ...

    
    public function __construct(MyCalculator $calculator) 
    {
        $this->calculator = $calculator;
    }

    /**
     * @Assert\LessThanOrEqual(value="10")
     */
    public $sum = 0;


    /**
     * {@inheritDoc}
     **/
    public function handle()
    {
        $this->sum = $this->calculator->sum($this->firstNumber, $this->secondNumber);
    }
}