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
|
10 |
|
public function __construct(string $entityClassName) |
19
|
|
|
{ |
20
|
10 |
|
$this->entityClassName = $entityClassName; |
21
|
10 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @return array |
25
|
|
|
* @throws \Doctrine\Common\Annotations\AnnotationException |
26
|
|
|
* @throws \ReflectionException |
27
|
|
|
*/ |
28
|
10 |
|
public function getSpec(): array |
29
|
|
|
{ |
30
|
10 |
|
$spec = []; |
31
|
10 |
|
$reflectionClassName = new \ReflectionClass($this->entityClassName); |
32
|
10 |
|
foreach ($reflectionClassName->getProperties() as $property) { |
33
|
|
|
/** @var Column $annotation */ |
34
|
10 |
|
$annotation = (new AnnotationReader()) |
35
|
10 |
|
->getPropertyAnnotation($property, Column::class); |
36
|
10 |
|
if (!$annotation) { |
37
|
|
|
continue; |
38
|
|
|
} |
39
|
10 |
|
$idAnnotation = (new AnnotationReader()) |
40
|
10 |
|
->getPropertyAnnotation($property, Id::class); |
41
|
10 |
|
$generated = (new AnnotationReader()) |
42
|
10 |
|
->getPropertyAnnotation($property, GeneratedValue::class); |
43
|
10 |
|
$spec[$property->getName()] = [ |
44
|
10 |
|
'required' => !$annotation->nullable, |
45
|
10 |
|
'type' => $annotation->type, |
46
|
|
|
'is_id' => $idAnnotation !== null, |
47
|
10 |
|
'is_unique' => $annotation->unique, |
48
|
|
|
'is_generated' => $generated !== null, |
49
|
|
|
]; |
50
|
|
|
} |
51
|
10 |
|
return $spec; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|