PHP code example of lsbproject / request-bundle

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

    

lsbproject / request-bundle example snippets


use LSBProject\RequestBundle\Request\RequestInterface;

class TestRequest implements RequestInterface
{
    public $fooBar;
}

/**
 * @Route("/test")
 */
public function test(TestRequest $testRequest): Response
{
    return new Response($testRequest->fooBar);
}

use LSBProject\RequestBundle\Request\RequestInterface;
use App\Model\SomeAwesomeClass;

class TestRequest implements RequestInterface
{
    public SomeAwesomeClass $fooBar;
}

use LSBProject\RequestBundle\Configuration\PropConverter;
use LSBProject\RequestBundle\Request\RequestInterface;

class TestRequest implements RequestInterface
{
    /**
     * @var App\Model\SomeAwesomeClass
     */
    public $fooBar;

    // or

    /**
     * @PropConverter(App\Model\SomeAwesomeClass::class)
     */
    public $fooBarBaz;

    // or

    #[PropConverter(App\Model\SomeAwesomeClass::class)]
    public $fooBarBaz;
}

use LSBProject\RequestBundle\Request\RequestInterface;
use LSBProject\RequestBundle\Configuration as LSB;

class TestRequest implements RequestInterface
{
    /**
     * @LSB\PropConverter(App\Model\SomeAwesomeClass::class, converter="awesome_converter", options={"mapping": {"user_id": "id"}})
     */
    public $fooBarBaz;
}

use LSBProject\RequestBundle\Request\RequestInterface;
use LSBProject\RequestBundle\Configuration as LSB;

/**
 * @LSB\RequestStorage({LSB\RequestStorage::BODY, LSB\RequestStorage::PATH})
 */
class TestRequest implements RequestInterface
{
    public $fooBaz;

    /**
     * @LSB\RequestStorage({LSB\RequestStorage::BODY})
     */
    public $fooBar;
}

use LSBProject\RequestBundle\Request\RequestInterface;

class TestRequest implements RequestInterface
{
    /**
     * @Assert\NotBlank()
     */
    public int $userId;
}

use LSBProject\RequestBundle\Request\RequestInterface;

class TestRequest implements RequestInterface
{
    private string $comment;

    public function setComment(string $comment): void
    {
        $this->comment = trim($comment);
    }
}

use LSBProject\RequestBundle\Configuration as LSB;
use LSBProject\RequestBundle\Request\RequestInterface;
use App\Entity\User;

class TestRequest implements RequestInterface
{
    /**
     * @LSB\Entity(App\Entity\User::class, expr="repository.find(id)", mapping={"id": "user_id"})
     */
    public $userA;
    
    // or

    /**
     * @LSB\Entity(options={"id"="user_id"})
     */
    public User $userB;

    // or

    /**
     * @LSB\Entity(options={"mapping": {"user_id": "id"}})
     */
    public User $userC;    
}



namespace LSBProject\RequestBundle\Util\NamingConversion;

final class CamelCaseToSnakeConversion implements NamingConversionInterface
{
    /**
     * {@inheritDoc}
     */
    public function normalize($value)
    {
        return strtolower(preg_replace('/[A-Z]/', '_\\0', lcfirst($value)) ?: '');
    }

    /**
     * {@inheritDoc}
     */
    public function denormalize($value)
    {
        $camelCasedName = preg_replace_callback('/(^|_|\.)+(.)/', function ($match) {
            return ('.' === $match[1] ? '_' : '') . strtoupper($match[2]);
        }, $value);

        return lcfirst($camelCasedName);
    }
}

use LSBProject\RequestBundle\Request\RequestInterface;
use LSBProject\RequestBundle\Configuration as LSB;
use Symfony\Component\HttpFoundation\Request;
use App\Request\DTO\Data;

/**
 * @LSB\RequestStorage({@LSB\RequestStorage::BODY})
 */
class JsonRpcRequest extends Request implements RequestInterface
{
    public string $jsonrpc;

    /**
     * 'method' property is already present in a base Request class, so alias should be used
     *
     * @LSB\PropConverter(name="method")
     */
    public string $methodName;

    public int $id;

    /** @LSB\PropConverter(isDto=true) */
    public Data $params;
}

use LSBProject\RequestBundle\Request\RequestInterface;
use LSBProject\RequestBundle\Configuration as LSB;
use App\Request\DTO\Data;

/**
 * @LSB\RequestStorage({@LSB\RequestStorage::BODY})
 */
class JsonRpcRequest implements RequestInterface
{
    public string $jsonrpc;

    /**
     * 'method' property is already present in a base Request class, so alias should be used
     *
     * @LSB\PropConverter(name="method")
     */
    public string $methodName;

    public int $id;

    /** 
     * @LSB\PropConverter(isDto=true)
     * @var App\Request\DTO\Data[]
     */
    public array $params;
}

use LSBProject\RequestBundle\Configuration as LSB;
use LSBProject\RequestBundle\Request\RequestInterface;

final class TestDiscriminatedRequest implements RequestInterface
{
    #[LSB\PropConverter(isDto: true)]
    #[LSB\Discriminator(
        field: 'type',
        mapping: [
            'foo' => new LSB\PropConverter(class: DiscriminatorParamsFoo::class, isDto: false),
            'bar' => DiscriminatorParamsBar::class
        ]
    )]
    public DiscriminatorParamsFoo|DiscriminatorParamsBar $discriminated;
}

abstract class AbstractDiscriminatorParams
{
    public string $type;
}

final class DiscriminatorParamsBar extends AbstractDiscriminatorParams
{
    public string $bar;
}

final class DiscriminatorParamsFoo extends AbstractDiscriminatorParams
{
    public string $foo;
}

    use LSBProject\RequestBundle\Configuration as LSB;

    //...

    /**
     * @Route("/foo")
     * @LSB\Request("params", storage=@LSB\RequestStorage({@LSB\RequestStorage::HEAD}))
     */
    public function testHeadRequest(TestParamsA $params): Response
    {
        return new Response($params->foo);
    }