Last active
April 29, 2021 07:11
-
-
Save erop/2e8dab99f141555271e31757b221fab5 to your computer and use it in GitHub Desktop.
Service for generating document schema
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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