AssociationDecorator.php 2.65 KB
Newer Older
1
2
<?php
/*
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
3
4
 * Copyright (C) 2016-2017 IRSTEA
 * All rights reserved.
5
6
7
8
9
10
11
 */

namespace Irstea\PlantUmlBundle\Doctrine;

use Doctrine\ORM\Mapping\ClassMetadata;
use Irstea\PlantUmlBundle\Model\Arrow\BaseArrow;
use Irstea\PlantUmlBundle\Model\ClassVisitorInterface;
12
use Irstea\PlantUmlBundle\Model\Node\Member\Member;
13
14
15
16
use Irstea\PlantUmlBundle\Model\NodeInterface;
use ReflectionClass;

/**
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
17
 * Description of RelationDecorator.
18
19
20
 */
class AssociationDecorator extends AbstractDoctrineDecorator
{
21
    use \Irstea\PlantUmlBundle\Model\Decorator\InheritableItemDecoratorTrait;
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
22

23
    protected function extractItems(ReflectionClass $class)
24
    {
25
26
27
28
29
30
        return $this->withMetadata(
            function ($metadata) {
                return $metadata->getAssociationMappings();
            },
            $class
        );
31
32
    }

33
    protected function decorateItem(ReflectionClass $class, NodeInterface $node, ClassVisitorInterface $visitor, $association)
34
35
36
37
38
    {
        if (!$association['isOwningSide']) {
            return;
        }

Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
39
        $target = $visitor->visitClass($association['targetEntity']);
40
        if ($target === false) {
41
            $type = $association['targetEntity'];
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
42
            if ($association['type'] & ClassMetadata::TO_MANY != 0) {
43
44
45
                $type .= '[]';
            }
            $node->addAttribute(new Member($association['fieldName'], $type));
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
46

47
48
49
            return;
        }

50
        $linkSource = '';
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
51
52
53
        $linkTarget = '>';
        if ($association['isCascadeRemove']) {
            $linkSource = 'o';
54
55
        }

Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
56
57
58
        $sourceCardinality = '';
        $targetCardinality = '';
        switch ($association['type']) {
59
            case ClassMetadata::ONE_TO_ONE:
60
61
                $sourceCardinality = '1';
                $targetCardinality = '1';
62
63
                break;
            case ClassMetadata::ONE_TO_MANY:
64
65
                $sourceCardinality = '1';
                $targetCardinality = '*';
66
67
                break;
            case ClassMetadata::MANY_TO_MANY:
68
69
                $sourceCardinality = '*';
                $targetCardinality = '*';
70
71
                break;
            case ClassMetadata::MANY_TO_ONE:
72
73
                $sourceCardinality = '*';
                $targetCardinality = '1';
74
75
76
77
                break;
        }

        $node->addArrow(
Raidelet Nicolas's avatar
Raidelet Nicolas committed
78
79
80
81
82
83
84
85
86
87
            new BaseArrow(
                $node,
                $target,
                '--',
                $association['fieldName'] . ' >',
                $linkSource,
                $linkTarget,
                $sourceCardinality,
                $targetCardinality
            )
88
89
        );
    }
90
91
92
93
94

    public function toConfig(array &$conf)
    {
        $conf['decorators'][] = 'associations';
    }
95
}