1. Go to this page and download the library: Download jrm/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/ */
use Jrm\RequestBundle\Model\Source;
use Jrm\RequestBundle\Attribute\Collection;
use Jrm\RequestBundle\Attribute\Header;
use Jrm\RequestBundle\Attribute\PathAttribute;
final class MyRequest
{
public function __construct(
#[PathAttribute()]
public readonly int $id,
#[Body('pos_id')]
private readonly string $posId,
#[Header('Content-Type')]
public readonly string $contentType,
#[Assert\Valid]
#[Collection(
type: ProductItem::class,
source: Source::BODY,
path: 'products',
)]
public readonly array $products,
) {
}
}
use Jrm\RequestBundle\MapRequest;
#[Route(
'/do-something/{id}',
name: 'app.do.something',
methods: [Request::METHOD_POST],
)]
final class MyAction
{
public function __invoke(#[MapRequest] MyRequest $request): JsonResponse
{
//do something
return new JsonResponse(null);
}
}
use Jrm\RequestBundle\Attribute\Body;
final class MyRequest
{
public function __construct(
#[Body('request.some_field')]
public readonly string $someField,
#[Body('request.next_field')]
public readonly string $nextField,
) {
}
}
use Jrm\RequestBundle\Attribute\Query;
use Symfony\Component\Validator\Constraints as Assert;
final class MyRequest
{
public function __construct(
#[Assert\NotBlank]
#[Query('some_field')]
public readonly string $field,
) {
}
}
use Jrm\RequestBundle\Attribute\Internal\Item;
use Symfony\Component\Validator\Constraints as Assert;
final class MyCollectionItem
{
public function __construct(
#[Assert\Uuid]
#[Item()]
public readonly string $id,
) {
}
}
use Jrm\RequestBundle\Model\Source;
use Jrm\RequestBundle\Attribute\Collection;
use Symfony\Component\Validator\Constraints as Assert;
final class MyRequest
{
public function __construct(
#[Assert\Valid]
#[Collection(
type: MyCollectionItem::class,
source: Source::BODY,
path: 'sub_ids',
)]
public readonly array $items,
) {
}
}
use Jrm\RequestBundle\Attribute\RequestAttribute;
#[Attribute(Attribute::TARGET_PARAMETER, Attribute::TARGET_PROPERTY)]
final class UserId implements RequestAttribute
{
/**
* @return class-string<UserIdResolver>
*/
public function resolvedBy(): string
{
return UserIdResolver::class;
}
}
use App\Domain\User\Exception\UserNotAuthorizedException;
use App\Domain\User\Model\User;
use Jrm\RequestBundle\Exception\UnexpectedAttributeException;
use Jrm\RequestBundle\Model\Metadata;
use Jrm\RequestBundle\Attribute\RequestAttribute;
use Jrm\RequestBundle\Attribute\ValueResolver;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
final class UserIdResolver implements ValueResolver
{
public function __construct(
private readonly TokenStorageInterface $tokenStorage,
) {
}
public function resolve(
Request $request,
Metadata $metadata,
RequestAttribute $attribute,
): int {
if (!$attribute instanceof UserId) {
throw new UnexpectedAttributeException(UserId::class, $attribute::class);
}
try {
$user = $this->tokenStorage->getToken()?->getUser();
if ($user === null) {
throw new UserNotAuthorizedException();
}
return $user->id();
} catch (Throwable $throwable) {
if ($parameter->isOptional()) {
return $parameter->defaultValue();
}
throw $throwable;
}
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.