1. Go to this page and download the library: Download methorz/openapi-generator 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/ */
methorz / openapi-generator example snippets
// config/autoload/dependencies.global.php
use Methorz\OpenApi\Command\GenerateOpenApiCommand;
return [
'dependencies' => [
'factories' => [
GenerateOpenApiCommand::class => function ($container) {
return new GenerateOpenApiCommand($container);
},
],
],
];
namespace App\Handler;
use App\Request\CreateItemRequest;
use App\Response\ItemResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
final class CreateItemHandler implements RequestHandlerInterface
{
public function handle(
ServerRequestInterface $request,
CreateItemRequest $dto // ← Request DTO detected
): ItemResponse { // ← Response DTO detected
// Handler logic...
}
}
namespace App\Request;
use Symfony\Component\Validator\Constraints as Assert;
final readonly class CreateItemRequest
{
public function __construct(
#[Assert\NotBlank]
#[Assert\Length(min: 3, max: 100)]
public string $name,
#[Assert\NotBlank]
#[Assert\Length(min: 10, max: 500)]
public string $description,
#[Assert\Email]
public string $email,
) {}
}
enum StatusEnum: string
{
case DRAFT = 'draft';
case ACTIVE = 'active';
case ARCHIVED = 'archived';
}
final readonly class CreateItemRequest
{
public function __construct(
public StatusEnum $status,
) {}
}
final readonly class AddressDto
{
public function __construct(
#[Assert\NotBlank]
public string $street,
#[Assert\NotBlank]
public string $city,
public ?string $country = null,
) {}
}
final readonly class CreateUserRequest
{
public function __construct(
public string $name,
public AddressDto $address, // ← Nested DTO
public ?AddressDto $billingAddress = null, // ← Nullable nested DTO
) {}
}
/**
* @param array<int, AddressDto> $addresses
* @param array<string> $tags
*/
final readonly class CreateOrderRequest
{
public function __construct(
public string $orderId,
public array $addresses,
public array $tags,
) {}
}
final readonly class FlexibleRequest
{
public function __construct(
public string|int $identifier, // ← Union type
) {}
}