|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zenstruck\Foundry\Bundle\Extractor; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
6
|
|
|
use Exception; |
|
7
|
|
|
use ReflectionClass; |
|
8
|
|
|
|
|
9
|
|
|
class Property |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var array |
|
13
|
|
|
*/ |
|
14
|
|
|
private $properties = []; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
private $defaultProperties = []; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var EntityManagerInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
private $em; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct(EntityManagerInterface $em) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->em = $em; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @TODO Support for defaults is missing |
|
33
|
|
|
* @TODO Support for relations is missing |
|
34
|
|
|
* |
|
35
|
|
|
* We only want create Defaults for non NULL probs |
|
36
|
|
|
* Dont create Default for field id |
|
37
|
|
|
*/ |
|
38
|
|
|
public function getFakerMethodFromDoctrineFieldMappings(ReflectionClass $entity): array |
|
39
|
|
|
{ |
|
40
|
|
|
$classMetaData = $this->em->getClassMetadata($entity->getName()); |
|
41
|
|
|
$this->getDefaultFromProperty($entity); |
|
42
|
|
|
$identifierFieldNames = $classMetaData->getIdentifierFieldNames(); |
|
43
|
|
|
|
|
44
|
|
|
foreach ($classMetaData->fieldMappings as $property) { |
|
45
|
|
|
// IGNORE FIELD IF IDENTIFIER |
|
46
|
|
|
if (\in_array($property['fieldName'], $identifierFieldNames)) { |
|
47
|
|
|
continue; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
// CREATE FROM DOCTRINE TYPE IF PROP IS NOT NULLABLE |
|
51
|
|
|
if (!$property['nullable']) { |
|
52
|
|
|
$this->properties[$property['fieldName']] = $this->createFakerMethodFromDoctrineType($property['type']); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $this->properties; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return string |
|
61
|
|
|
* |
|
62
|
|
|
* @throws Exception |
|
63
|
|
|
*/ |
|
64
|
|
|
public function createFakerMethodFromDoctrineType(string $doctrineType) |
|
65
|
|
|
{ |
|
66
|
|
|
if (\array_key_exists($doctrineType, DoctrineTypes::DOCTRINE_TYPES)) { |
|
67
|
|
|
return DoctrineTypes::DOCTRINE_TYPES[$doctrineType]; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
throw new Exception('DOCTRINE_TYPE not found: '.$doctrineType); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @TODO |
|
75
|
|
|
* We store defaults values from properties. |
|
76
|
|
|
* if there is an default property which is not null|''|false we can use it? $this->defaultProperties |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getDefaultFromProperty(ReflectionClass $reflectionClass): void |
|
79
|
|
|
{ |
|
80
|
|
|
$this->defaultProperties = $reflectionClass->getDefaultProperties(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @TODO get relations which cant be NULL |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getPropertiesFromDoctrineRelations() |
|
87
|
|
|
{ |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function getProperties(): array |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->properties; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function setProperties(array $properties): void |
|
96
|
|
|
{ |
|
97
|
|
|
$this->properties = $properties; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|