AssociationDecorator.php 2.16 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php

/*
 * © 2016 IRSTEA
 * Guillaume Perréal <guillaume.perreal@irstea.fr>
 * Tous droits réservés.
 */

namespace Irstea\PlantUmlBundle\Doctrine;

use Doctrine\ORM\Mapping\ClassMetadata;
use Irstea\PlantUmlBundle\Model\Arrow\BaseArrow;
use Irstea\PlantUmlBundle\Model\ClassVisitorInterface;
use Irstea\PlantUmlBundle\Model\NodeInterface;
use ReflectionClass;

/**
 * Description of RelationDecorator
 *
 * @author Guillaume Perréal <guillaume.perreal@irstea.fr>
 */
class AssociationDecorator extends AbstractDoctrineDecorator
{
    protected function decorateEntity(ClassMetadata $metadata, NodeInterface $node, ClassVisitorInterface $visitor)
    {
        $associations = $metadata->getAssociationMappings();
        if(empty($associations)) {
            return;
        }

        foreach($associations as $association) {
            $this->visitAssociation($node, $visitor, $association);
        }
    }

    protected function visitAssociation(NodeInterface $node, ClassVisitorInterface $visitor, array $association)
    {
        if (!$association['isOwningSide']) {
            return;
        }

        $target  = $visitor->visitClass($association['targetEntity']);
        if ($target === false) {
            return;
        }

        $link = "-->";
        if ($association["isCascadeRemove"]) {
            $link = "o".$link;
        }

        $leftLabel = "";
        $rightLabel = "";
        switch($association["type"]) {
            case ClassMetadata::ONE_TO_ONE:
                $leftLabel = '1';
                $rightLabel = '1';
                break;
            case ClassMetadata::ONE_TO_MANY:
                $leftLabel = '1';
                $rightLabel = '*';
                break;
            case ClassMetadata::MANY_TO_MANY:
                $leftLabel = '*';
                $rightLabel = '*';
                break;
            case ClassMetadata::MANY_TO_ONE:
                $leftLabel = '*';
                $rightLabel = '1';
                break;
        }
        $link = sprintf('"%s" %s "%s"', $leftLabel, $link, $rightLabel);

        $node->addArrow(
            new BaseArrow($node, $target, $link, $association["fieldName"]." >")
        );
    }
}