Skip to content

Instantly share code, notes, and snippets.

View erop's full-sized avatar

Egor Ushakov erop

View GitHub Profile
@erop
erop / DocumentSchemaController.php
Created April 27, 2021 19:32
Controller for exposing document schemas
<?php
declare(strict_types=1);
namespace App\Controller;
use App\Service\DocumentSchemaService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
@erop
erop / DocumentSchemaService.php
Last active April 29, 2021 07:11
Service for generating document schema
<?php
declare(strict_types=1);
namespace App\Service;
use App\Annotation\DocumentField;
use App\DocumentType\IDocument;
use Doctrine\Common\Annotations\AnnotationReader;
final class DocumentSchemaService
@erop
erop / services.yaml
Created April 27, 2021 19:24
Setup autoconfiguration for IDocument interface
services:
#...
_instanceof:
App\DocumentType\IDocument:
tags: ['app.idocument']
#...
App\Service\DocumentSchemaService:
arguments:
- !tagged_iterator app.idocument
@erop
erop / Passport.php
Created April 27, 2021 18:23
Class Passport annotated with @DocumentField
<?php
declare(strict_types=1);
namespace App\DocumentType;
use App\Annotation\DocumentField;
final class Passport implements IDocument
{
/**
@erop
erop / DocumentField.php
Last active April 27, 2021 18:21
DocumentField annotation
<?php
namespace App\Annotation;
use Doctrine\Common\Annotations\Annotation;
use Doctrine\Common\Annotations\Annotation\Target;
/**
* @Annotation
* @Target("PROPERTY")
POST http://localhost:8000/api/documents
Content-Type: application/json
{
"payload": {
"type": "passport",
"firstName": "Ivan",
"lastName": "Ivanov",
"citizenship": "Russia",
"series": "XC",
<?php
declare(strict_types=1);
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\Entity;
/**
* @Entity
@erop
erop / example.json
Created July 7, 2020 19:12
DiscriminatorMap example
{
"type": "Person",
"firstName": "Ivan",
"lastName": "Ivanov"
}
{
"type": "Company",
"name": "ACME Consulting",
"city": "Moscow"
}
// REQUEST
{
"year":2017,
"vin":"134513879",
"plate":"A123AA777",
"powerHp":"146.1",
"model":"/api/vehicle_models/000b69fa-a335-49d6-b7be-49fbe570376a",
"ownerPerson": {
"firstName":"Egor"
}
<?php
declare(strict_types=1);
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use DateTimeImmutable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;