PHP code example of artyuum / request-dto-mapper-bundle

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

    

artyuum / request-dto-mapper-bundle example snippets


class PostPayload {    
    /**
     * @Assert\Sequentially({
     *     @Assert\NotBlank,
     *     @Assert\Type("string")
     * })
     *
     * @var string|null
     */
    public $content;
}

use Artyum\RequestDtoMapperBundle\Attribute\Dto;
use Artyum\RequestDtoMapperBundle\Extractor\JsonExtractor;

class CreatePostController extends AbstractController
{
    #[Dto(extractor: JsonExtractor::class, subject: PostPayload::class, validate: true)]
    public function __invoke(PostPayload $postPayload): Response
    {
        // At this stage, your DTO has automatically been mapped (from the JSON input) and validated.
        // Your controller can safely be executed knowing that the submitted content
        // matches your 

public function __invoke(#[Dto(extractor: JsonExtractor::class, validate: true)] PostPayload $postPayload): Response
{
}

public function __invoke(#[Dto] PostPayload $postPayload): Response
{
}

use Artyum\RequestDtoMapperBundle\Extractor\ExtractorInterface;
use Symfony\Component\HttpFoundation\Request;

class CustomExtractor implements ExtractorInterface
{
    // you can optionally inject dependencies
    public function __construct() {
    }

    public function extract(Request $request): array
    {
        // your custom extraction logic here 
    }
}

#[Dto(extractor: CustomExtractor::class)]

#[Dto(subject: PostPayload::class)]
public function __invoke(PostPayload $postPayload): Response
{
}

public function __invoke(#[Dto] PostPayload $postPayload): Response
{
}

#[Dto(methods: 'GET')]

#[Dto(methods: ['GET'])]

use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;

#[Dto(denormalizerOptions: [ObjectNormalizer::DISABLE_TYPE_ENFORCEMENT => true])]

#[Dto(validate: true)]

$request->attributes->get('_constraint_violations')

#[Dto(validationGroups: ['creation'])]

#[Dto(throwOnViolation: false)]