PHP code example of n7 / symfony-http-bundle

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

    

n7 / symfony-http-bundle example snippets


use N7\SymfonyHttpBundle\Interfaces\RequestPayloadInterface;

final class CreateUserRequest implements RequestPayloadInterface
{
    #[Constraints\NotBlank]
    #[Constraints\Type('string')]
    private string $username;

    #[Constraints\NotBlank]
    #[Constraints\Type('integer')]
    private int $age;
    
    #[Constraints\NotBlank]
    #[Constraints\Type('boolean')]
    private bool $terms;
}

#[Route('/api/endpoint')]
public function create(CreateUserRequest $request): Response
{
    dd($request);
}

use N7\SymfonyHttpBundle\Annotations\ValueMutatorInterface;

#[\Attribute]
class StringLimitMutator implements ValueMutatorInterface
{
    public function mutate($value, array $payload, string $property)
    {
        if (! is_string($value)) {
            return $value;
        }
    
        return mb_substr($value, 0, 50);
    }
}

final class TestRequest implements RequestPayloadInterface
{
    #[Constraints\NotBlank]
    #[Constraints\Type('string')]
    #[StringLimitMutator]
    private string $address;
}

use JMS\Serializer\Annotation as Serializer;

final class Request implements RequestPayloadInterface
{
    #[Constraints\NotBlank]
    #[NestedObject(NestedObject::class)]
    private NestedObject $select2;

    #[Constraints\NotBlank]
    #[NestedObjects(NestedObject::class)]
    #[Serializer\Type("array<...\NestedObject>")]
    private array $list;
}

final class Request implements RequestPayloadInterface
{
    /**
     * @var float[]
     */
    #[Constraints\NotBlank]
    #[Serializer\Type("array")]
    private array $amounts;
}

use N7\SymfonyValidatorsBundle\Options\AllowExtraFields;
use N7\SymfonyValidatorsBundle\Options\AllowMissingFields;

#[AllowExtraFields]
#[AllowMissingFields]
final class Request implements RequestPayloadInterface
{

}