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
|
|
|
$doctrineType = \mb_strtoupper($doctrineType); |
67
|
|
|
|
68
|
|
|
if (\array_key_exists($doctrineType, DoctrineTypes::DOCTRINE_TYPES)) { |
69
|
|
|
return DoctrineTypes::DOCTRINE_TYPES[$doctrineType]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
throw new Exception('DOCTRINE_TYPE not found: '.$doctrineType); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @TODO |
77
|
|
|
* We store defaults values from properties. |
78
|
|
|
* if there is an default property which is not null|''|false we can use it? $this->defaultProperties |
79
|
|
|
*/ |
80
|
|
|
public function getDefaultFromProperty(ReflectionClass $reflectionClass): void |
81
|
|
|
{ |
82
|
|
|
$this->defaultProperties = $reflectionClass->getDefaultProperties(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @TODO get relations which cant be NULL |
87
|
|
|
*/ |
88
|
|
|
public function getPropertiesFromDoctrineRelations() |
89
|
|
|
{ |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getProperties(): array |
93
|
|
|
{ |
94
|
|
|
return $this->properties; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function setProperties(array $properties): void |
98
|
|
|
{ |
99
|
|
|
$this->properties = $properties; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|