BaseArrow.php 2.72 KB
Newer Older
1
<?php declare(strict_types=1);
2
/*
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
3
4
 * Copyright (C) 2016-2017 IRSTEA
 * All rights reserved.
5
6
7
8
 */

namespace Irstea\PlantUmlBundle\Model\Arrow;

9
10
use Irstea\PlantUmlBundle\Model\ArrowInterface;
use Irstea\PlantUmlBundle\Model\NodeInterface;
11
use Irstea\PlantUmlBundle\Writer\WriterInterface;
12
13

/**
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
14
 * Description of Arrow.
15
 */
16
class BaseArrow implements ArrowInterface
17
18
{
    /**
19
     * @var NodeInterface
20
21
22
23
     */
    private $source;

    /**
24
     * @var NodeInterface
25
26
27
28
29
30
31
32
     */
    private $target;

    /**
     * @var string
     */
    private $link;

33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
    /**
     * @var string
     */
    private $linkSource;

    /**
     * @var string
     */
    private $linkTarget;

    /**
     * @var string
     */
    private $sourceCardinality;

    /**
     * @var string
     */
    private $targetCardinality;

    /**
     * @var int
     */
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
56
    private static $splitCount = 0;
57
58
59
60

    public function __construct(
        NodeInterface $source,
        NodeInterface $target,
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
61
        $link = '--',
62
63
64
65
66
67
        $label = null,
        $linkSource = '',
        $linkTarget = '',
        $sourceCardinality = '',
        $targetCardinality = ''
    ) {
68
69
        $this->source = $source;
        $this->target = $target;
70
71
72
73
74
75
        $this->link = $link;
        $this->label = $label;
        $this->linkSource = $linkSource;
        $this->linkTarget = $linkTarget;
        $this->sourceCardinality = $sourceCardinality;
        $this->targetCardinality = $targetCardinality;
76
77
    }

78
    public function writeTo(WriterInterface $writer)
79
    {
80
81
82
83
        $this->source->writeAliasTo($writer);
        $this->writeLinkTo($writer);
        $this->target->writeAliasTo($writer);
        $this->writeLabelTo($writer);
84
        $writer->write("\n");
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
85

86
        return $this;
87
88
    }

89
    protected function writeLabelTo(WriterInterface $writer)
90
91
    {
        if ($this->label) {
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
92
            $writer->writeFormatted(' : %s', $this->label);
93
        }
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
94

95
        return $this;
96
97
    }

98
    protected function writeLinkTo(WriterInterface $writer, $linkSource = null, $linkTarget = null)
99
    {
100
        $this->writeSourceCardinalityTo($writer);
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
101
        $writer->writeFormatted(' %s%s%s ', $linkSource ?: $this->linkSource, $this->link, $linkTarget ?: $this->linkTarget);
102
        $this->writeTargetCardinalityTo($writer);
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
103

104
105
106
107
108
109
110
111
        return $this;
    }

    protected function writeSourceCardinalityTo(WriterInterface $writer)
    {
        if ($this->sourceCardinality !== '') {
            $writer->writeFormatted(' "%s"', $this->sourceCardinality);
        }
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
112

113
114
115
116
117
118
119
120
        return $this;
    }

    protected function writeTargetCardinalityTo(WriterInterface $writer)
    {
        if ($this->targetCardinality !== '') {
            $writer->writeFormatted('"%s" ', $this->targetCardinality);
        }
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
121

122
        return $this;
123
124
    }
}