PHP code example of kr0lik / laravel-dto-to-swagger
1. Go to this page and download the library: Download kr0lik/laravel-dto-to-swagger 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/ */
declare(strict_types=1);
namespace App\Http\Controllers;
/**
* This controller demonstrates how to define routes and generate Swagger documentation
* using annotations and DTOs in a Laravel application.
*/
class TextController extends Controller
{
/**
* Path in swagger will be generated automatic.
*
* @param RequestDto $requestDto The request will be generated automatic.
* @param int|string|null $multipleVar Path parameter be added automatic.
* @param array|null $arrayOptionalVar Optional be added automatic.
*
* @return ResponseDto The response will be generated automatic.
*/
public function postAction(RequestDto $requestDto, int|string|null $multipleVar, ?array $arrayOptionalVar = []): ResponseDto
{
return new ResponseDto(1, 'string', new DateTimeImmutable());
}
}
declare(strict_types=1);
namespace App\Dto\Response;
use Kr0lik\DtoToSwagger\Contract\JsonResponseInterface;
final class ResponseDto extends Data implements JsonResponseInterface
{
public function __construct(
/** Some Description */
readonly int $int,
readonly DateTimeImmutable $dateTime,
) {
}
}
declare(strict_types=1);
namespace App\Http\Controllers;
use OpenApi\Attributes\Response;
use OpenApi\Attributes\Tag;
/**
* This controller demonstrates how to define routes and generate Swagger documentation
* using annotations and DTOs in a Laravel application.
*/
class TextController extends Controller
{
/**
* @Tag("tagFromAttribute")
* @Response(
* response=300,
* description="response-from-attribute"
* )
*/
public function postAction(RequestDto $requestDto, int|string|null $multipleVar, ?array $arrayOptionalVar = []): ResponseDto
{
return new ResponseDto(1, 'string', new DateTimeImmutable());
}
}
declare(strict_types=1);
namespace App\Dto\Request;
use App\Enum\StringEnum;
use DateTimeImmutable;
use Illuminate\Support\Collection;
use Kr0lik\DtoToSwagger\Attribute\Context;
use Kr0lik\DtoToSwagger\Attribute\Name;
use Kr0lik\DtoToSwagger\Contract\JsonRequestInterface;
use OpenApi\Attributes\Parameter;
use OpenApi\Attributes\Property;
use stdClass;
use Symfony\Component\HttpFoundation\File\UploadedFile;
final class RequestDto implements JsonRequestInterface
{
/**
* @param string[] $arrayOfString
* @param stdClass[] $arrayOfObject
* @param array<int[]> $arrayWithSubArrayOfInt
* @param SubDto[] $arrayOfDto
* @param Collection<array-key, string> $collectionOfString
*/
public function __construct(
/** Some Description1 */
#[Parameter(in: 'query')]
readonly int $int,
#[Property(description: 'some description', example: ['string', 'string2'])]
readonly string $string,
#[Parameter(name: 'X-FLOAT', in: 'header')]
readonly float $float,
#[Name('datetime')]
#[Context(pattern: 'Y-m-d H:i:s')]
readonly DateTimeImmutable $dateTimeImmutable,
readonly array $arrayOfString,
/** Some Description2 */
readonly array $arrayOfObject,
readonly array $arrayWithSubArrayOfInt,
readonly SubDto $subDto,
readonly array $arrayOfDto,
readonly object $objectNullable,
readonly StringEnum $enum,
readonly UploadedFile $uploadedFile,
readonly Collection $collectionOfString,
) {
}
}
declare(strict_types=1);
namespace App\Dto\Request;
use Kr0lik\DtoToSwagger\Attribute\Nested;
use Kr0lik\DtoToSwagger\Contract\QueryRequestInterface;
use OpenApi\Attributes\Parameter;
class QueryRequest implements QueryRequestInterface
{
public function __construct(
readonly int $page,
#[Parameter(name: 'per-page')]
readonly int $perPage = 20,
#[Nested]
readonly SortDto $sort,
) {
}
}
declare(strict_types=1);
namespace App\Dto\Request;
use Kr0lik\DtoToSwagger\Contract\HeaderRequestInterface;
use OpenApi\Attributes\Parameter;
final class HeadRequestDto implements HeaderRequestInterface
{
public function __construct(
#[Parameter(name: 'X-DEVICE-ID')]
readonly string $deviceId,
) {
}
}
declare(strict_types=1);
namespace App\Dto\Response;
use DateTimeImmutable;
use Kr0lik\DtoToSwagger\Attribute\Context;
use Kr0lik\DtoToSwagger\Attribute\Wrap;
use Kr0lik\DtoToSwagger\Contract\JsonResponseInterface;
use OpenApi\Attributes\Property;
use Spatie\LaravelData\Attributes\MapOutputName;
use Spatie\LaravelData\Attributes\WithTransformer;
use Spatie\LaravelData\Data;
use Spatie\LaravelData\Transformers\DateTimeInterfaceTransformer;
#[Wrap(ref: '#/components/schemas/JsonResponse', to: 'data', properties: [
'success' => ['default' => true],
'message' => ['default' => 'success'],
'errors' => ['default' => null],
])]
final class ResponseDto extends Data implements JsonResponseInterface
{
public function __construct(
/** Some Description */
readonly int $int,
#[Property(description: 'some description', example: ['string', 'string2'])]
readonly string $string,
#[MapOutputName('date')]
#[WithTransformer(DateTimeInterfaceTransformer::class, format: 'Y-m-d')]
readonly DateTimeImmutable $dateTime,
) {
}
}