1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dontdrinkandroot\RestBundle\Metadata\Driver; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use Doctrine\ORM\Mapping\MappingException; |
7
|
|
|
use Dontdrinkandroot\RestBundle\Metadata\ClassMetadata; |
8
|
|
|
use Dontdrinkandroot\RestBundle\Metadata\PropertyMetadata; |
9
|
|
|
use Metadata\Driver\DriverInterface; |
10
|
|
|
|
11
|
|
|
class DoctrineDriver implements DriverInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var EntityManagerInterface |
15
|
|
|
*/ |
16
|
|
|
private $entityManager; |
17
|
|
|
|
18
|
39 |
|
function __construct(EntityManagerInterface $entityManager) |
|
|
|
|
19
|
|
|
{ |
20
|
39 |
|
$this->entityManager = $entityManager; |
21
|
39 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
*/ |
26
|
39 |
|
public function loadMetadataForClass(\ReflectionClass $class) |
27
|
|
|
{ |
28
|
39 |
|
$ddrRestClassMetadata = new ClassMetadata($class->getName()); |
29
|
|
|
try { |
30
|
39 |
|
$doctrineClassMetadata = $this->entityManager->getClassMetadata($class->getName()); |
31
|
1 |
|
} catch (MappingException $e) { |
32
|
1 |
|
return $ddrRestClassMetadata; |
33
|
|
|
} |
34
|
|
|
|
35
|
39 |
|
foreach ($doctrineClassMetadata->embeddedClasses as $fieldName => $embeddedClass) { |
|
|
|
|
36
|
|
|
$ddrRestPropertyMetadata = new PropertyMetadata($doctrineClassMetadata->getName(), $fieldName); |
37
|
|
|
$ddrRestPropertyMetadata->setType($embeddedClass['class']); |
38
|
|
|
$ddrRestClassMetadata->addPropertyMetadata($ddrRestPropertyMetadata); |
39
|
|
|
} |
40
|
|
|
|
41
|
19 |
|
foreach ($doctrineClassMetadata->fieldMappings as $fieldMapping) { |
|
|
|
|
42
|
19 |
|
if (!array_key_exists('declared', $fieldMapping) && !array_key_exists('declaredField', $fieldMapping)) { |
43
|
19 |
|
$ddrRestPropertyMetadata = new PropertyMetadata( |
44
|
19 |
|
$doctrineClassMetadata->getName(), |
45
|
19 |
|
$fieldMapping['fieldName'] |
46
|
|
|
); |
47
|
19 |
|
$ddrRestPropertyMetadata->setType($fieldMapping['type']); |
48
|
19 |
|
$ddrRestClassMetadata->addPropertyMetadata($ddrRestPropertyMetadata); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
19 |
|
foreach ($doctrineClassMetadata->associationMappings as $associationMapping) { |
|
|
|
|
53
|
13 |
|
$ddrRestPropertyMetadata = new PropertyMetadata( |
54
|
13 |
|
$doctrineClassMetadata->getName(), |
55
|
13 |
|
$associationMapping['fieldName'] |
56
|
|
|
); |
57
|
13 |
|
$ddrRestPropertyMetadata->setAssociation(true); |
58
|
13 |
|
$ddrRestPropertyMetadata->setTargetClass($associationMapping['targetEntity']); |
59
|
13 |
|
$ddrRestPropertyMetadata->setCollection( |
60
|
13 |
|
$doctrineClassMetadata->isCollectionValuedAssociation($associationMapping['fieldName']) |
61
|
|
|
); |
62
|
13 |
|
$ddrRestClassMetadata->addPropertyMetadata($ddrRestPropertyMetadata); |
63
|
|
|
} |
64
|
|
|
|
65
|
19 |
|
return $ddrRestClassMetadata; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.