PHP code example of sunrise-studio / symfony-openapi
1. Go to this page and download the library: Download sunrise-studio/symfony-openapi 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 Symfony\Component\HttpKernel\Attribute\MapQueryParameter;
public function find(
#[MapQueryParameter] PetStatus $status,
#[MapQueryParameter] string ...$tags,
): JsonResponse {
// ...
}
use Symfony\Component\HttpKernel\Attribute\MapQueryString;
public function list(#[MapQueryString] PetSearchQuery $query): JsonResponse
{
// ...
}
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
public function create(#[MapRequestPayload(acceptFormat: 'json')] CreatePetRequest $request): PetView
{
// ...
}
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpKernel\Attribute\MapUploadedFile;
public function upload(#[MapUploadedFile(name: 'photo')] UploadedFile $file): JsonResponse
{
// ...
}
use Symfony\Component\HttpKernel\Attribute\MapDateTime;
public function history(#[MapDateTime(format: 'Y-m-d')] DateTimeImmutable $date): JsonResponse
{
// ...
}
#[Route('/api/pets/{id}', methods: ['GET'])]
public function show(int $id): PetView
{
// ...
}
use Symfony\Component\HttpKernel\Attribute\Serialize;
#[Route('/api/pets', methods: ['POST'], options: ['response_code' => 201])]
#[Serialize(code: 201)]
public function create(CreatePetRequest $request): PetView
{
// ...
}
#[Route('/api/pets/{id}', methods: ['DELETE'])]
public function delete(int $id): void
{
// ...
}
namespace App\EventListener;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Serializer\SerializerInterface;
#[AsEventListener(event: KernelEvents::VIEW)]
final readonly class JsonControllerResultListener
{
public function __construct(
private SerializerInterface $serializer,
) {
}
public function __invoke(ViewEvent $event): void
{
$result = $event->getControllerResult();
if ($result === null) {
$event->setResponse(new Response(status: 204));
return;
}
$event->setResponse(new JsonResponse(
$this->serializer->serialize($result, 'json'),
200,
json: true,
));
}
}
/** @var list<PetView> */
public array $pets;
use Sunrise\Symfony\OpenApi\Annotation\Operation;
use Sunrise\Symfony\OpenApi\Type;
#[Operation([
'responses' => [
200 => [
'description' => 'A list of pets.',
'content' => [
'application/json' => [
'schema' => [
'type' => 'array',
'items' => new Type(PetView::class),
],
],
],
],
],
])]
public function list(): JsonResponse
{
// ...
}
use App\View\ErrorView;
use Sunrise\Symfony\OpenApi\Annotation\Operation;
use Sunrise\Symfony\OpenApi\Type;
#[Operation([
'responses' => [
'default' => [
'description' => 'The operation was unsuccessful.',
'content' => [
'application/json' => [
'schema' => new Type(ErrorView::class),
],
],
],
],
])]
final readonly class PetController
{
}