AssociationDecorator.php 2.51 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
<?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;
14
use Irstea\PlantUmlBundle\Model\Node\Member\Member;
15
16
17
18
19
20
21
22
23
24
use Irstea\PlantUmlBundle\Model\NodeInterface;
use ReflectionClass;

/**
 * Description of RelationDecorator
 *
 * @author Guillaume Perréal <guillaume.perreal@irstea.fr>
 */
class AssociationDecorator extends AbstractDoctrineDecorator
{
25
26
27
    use \Irstea\PlantUmlBundle\Model\Decorator\InheritableItemDecoratorTrait;
    
    protected function extractItems(ReflectionClass $class)
28
    {
29
30
31
32
33
34
        return $this->withMetadata(
            function ($metadata) {
                return $metadata->getAssociationMappings();
            },
            $class
        );
35
36
    }

37
    protected function decorateItem(ReflectionClass $class, NodeInterface $node, ClassVisitorInterface $visitor, $association)
38
39
40
41
42
43
44
    {
        if (!$association['isOwningSide']) {
            return;
        }

        $target  = $visitor->visitClass($association['targetEntity']);
        if ($target === false) {
45
46
47
48
49
            $type = $association['targetEntity'];
            if ($association["type"] & ClassMetadata::TO_MANY != 0) {
                $type .= '[]';
            }
            $node->addAttribute(new Member($association['fieldName'], $type));
50
51
52
            return;
        }

53
54
        $linkSource = '';
        $linkTarget = ">";
55
        if ($association["isCascadeRemove"]) {
56
            $linkSource = "o";
57
58
        }

59
60
        $sourceCardinality = "";
        $targetCardinality = "";
61
62
        switch($association["type"]) {
            case ClassMetadata::ONE_TO_ONE:
63
64
                $sourceCardinality = '1';
                $targetCardinality = '1';
65
66
                break;
            case ClassMetadata::ONE_TO_MANY:
67
68
                $sourceCardinality = '1';
                $targetCardinality = '*';
69
70
                break;
            case ClassMetadata::MANY_TO_MANY:
71
72
                $sourceCardinality = '*';
                $targetCardinality = '*';
73
74
                break;
            case ClassMetadata::MANY_TO_ONE:
75
76
                $sourceCardinality = '*';
                $targetCardinality = '1';
77
78
79
80
                break;
        }

        $node->addArrow(
81
            new BaseArrow($node, $target, "--", $association["fieldName"]." >", $linkSource, $linkTarget, $sourceCardinality, $targetCardinality)
82
83
84
        );
    }
}