Skip to content

Instantly share code, notes, and snippets.

@erop
Last active April 29, 2021 07:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erop/2e8dab99f141555271e31757b221fab5 to your computer and use it in GitHub Desktop.
Save erop/2e8dab99f141555271e31757b221fab5 to your computer and use it in GitHub Desktop.
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
{
private iterable $documentTypes;
public function __construct(iterable $documentTypes)
{
$this->documentTypes = $documentTypes;
}
public function __invoke(): array
{
$schemas = [];
/** @var IDocument $documentType */
foreach ($this->documentTypes as $documentType) {
$schema = [];
$fields = [];
$reader = new AnnotationReader();
foreach ((new \ReflectionClass($documentType))->getProperties() as $property) {
$annotation = $reader->getPropertyAnnotation(
$property,
DocumentField::class
);
$fields[$property->getName()] = $annotation;
}
$schema['name'] = $documentType->getName();
$schema['type'] = $documentType->getType();
$schema['fields'] = $fields;
$schemas[] = $schema;
}
return $schemas;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment