PHP code example of vi-tech / dto-bundle

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

    

vi-tech / dto-bundle example snippets


// config/bundles.php
return [
    \ViTech\DataObjectBundle\DataObjectBundle::class => ['all' => true],
];



use Symfony\Component\HttpFoundation\Response;
use ViTech\DataObjectBundle\Object\AbstractObject;

class RegistrationDto extends AbstractObject
{
    /** @var string */
    public $login;
    
    /** @var string */
    public $password;
}

class RegistrationController
{
    public function __invoke(RegistrationDto $registration): Response
    {
        // Register new user using $registration
        // $registration->login contains Request::$request->get('login'). Same for password.

        return new Response();
    }
}




use Symfony\Component\HttpFoundation\Response;
use ViTech\DataObjectBundle\Object\AbstractObject;
use Symfony\Component\Validator\Validator\ValidatorInterface;

class RegistrationDto extends AbstractObject
{
    /**
     * @Assert\NotBlank()
     * @Assert\UniqueLogin()
     *
     * @var string
     */
    public $login;
    
    /**
     * @Assert\NotBlank()
     * @Assert\PasswordRules()
     *
     * @var string
     */
    public $password;
}

class RegistrationController
{
    /** @var ValidatorInterface */
    private $validator;

    public function __invoke(RegistrationDto $registration): Response
    {
        $violations = $this->validator->validate($registration);
        if (count($violations)) {
            // Handle constraints violations
        }

        // register new user using $registration

        return new Response();
    }
}