Passed
Pull Request — master (#173)
by
unknown
02:49
created

Property::setProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 1
c 2
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
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 EntityManagerInterface
18
     */
19
    private $em;
20
21
    public function __construct(EntityManagerInterface $em)
22
    {
23
        $this->em = $em;
24
    }
25
26
    /**
27
     * @TODO Support for relations is missing
28
     */
29
    public function getFakerMethodFromDoctrineFieldMappings(ReflectionClass $entity): array
30
    {
31
        $classMetaData = $this->em->getClassMetadata($entity->getName());
32
        $identifierFieldNames = $classMetaData->getIdentifierFieldNames();
33
34
        foreach ($classMetaData->fieldMappings as $property) {
35
            // IGNORE FIELD IF IDENTIFIER
36
            if (\in_array($property['fieldName'], $identifierFieldNames)) {
37
                continue;
38
            }
39
40
            // CREATE FROM DOCTRINE TYPE IF PROP IS NOT NULLABLE
41
            if (!$property['nullable']) {
42
                $this->properties[$property['fieldName']] = $this->createFakerMethodFromDoctrineType($property['type']);
43
            }
44
        }
45
46
        return $this->properties;
47
    }
48
49
    /**
50
     * @throws Exception
51
     */
52
    public function createFakerMethodFromDoctrineType(string $doctrineType): string
53
    {
54
        $doctrineType = \mb_strtoupper($doctrineType);
55
56
        if (\array_key_exists($doctrineType, DoctrineTypes::DOCTRINE_TYPES)) {
57
            return DoctrineTypes::DOCTRINE_TYPES[$doctrineType];
58
        }
59
60
        return 'null, // @TODO add '.$doctrineType.' manually';
61
    }
62
63
    /**
64
     * @TODO get relations which cant be NULL
65
     */
66
    public function getPropertiesFromDoctrineRelations()
67
    {
68
    }
69
70
    public function getProperties(): array
71
    {
72
        return $this->properties;
73
    }
74
75
    public function setProperties(array $properties): void
76
    {
77
        $this->properties = $properties;
78
    }
79
}
80