|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace SlayerBirden\DFCodeGeneration\Generator\Tests\Api; |
|
5
|
|
|
|
|
6
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
|
7
|
|
|
use Doctrine\ORM\Mapping\Column; |
|
8
|
|
|
use Doctrine\ORM\Mapping\GeneratedValue; |
|
9
|
|
|
use Doctrine\ORM\Mapping\Id; |
|
10
|
|
|
|
|
11
|
|
|
final class ReflectionEntitySpecProvider implements EntitySpecProviderInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
private $entityClassName; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(string $entityClassName) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->entityClassName = $entityClassName; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @return array |
|
25
|
|
|
* @throws \Doctrine\Common\Annotations\AnnotationException |
|
26
|
|
|
* @throws \ReflectionException |
|
27
|
|
|
*/ |
|
28
|
|
|
public function getSpec(): array |
|
29
|
|
|
{ |
|
30
|
|
|
$spec = []; |
|
31
|
|
|
$reflectionClassName = new \ReflectionClass($this->entityClassName); |
|
32
|
|
|
foreach ($reflectionClassName->getProperties() as $property) { |
|
33
|
|
|
/** @var Column $annotation */ |
|
34
|
|
|
$annotation = (new AnnotationReader()) |
|
35
|
|
|
->getPropertyAnnotation($property, Column::class); |
|
36
|
|
|
if (!$annotation) { |
|
37
|
|
|
continue; |
|
38
|
|
|
} |
|
39
|
|
|
$idAnnotation = (new AnnotationReader()) |
|
40
|
|
|
->getPropertyAnnotation($property, Id::class); |
|
41
|
|
|
$generated = (new AnnotationReader()) |
|
42
|
|
|
->getPropertyAnnotation($property, GeneratedValue::class); |
|
43
|
|
|
$spec[$property->getName()] = [ |
|
44
|
|
|
'required' => !$annotation->nullable, |
|
45
|
|
|
'type' => $annotation->type, |
|
46
|
|
|
'is_id' => $idAnnotation !== null, |
|
47
|
|
|
'is_unique' => $annotation->unique, |
|
48
|
|
|
'is_generated' => $generated !== null, |
|
49
|
|
|
]; |
|
50
|
|
|
} |
|
51
|
|
|
return $spec; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|