BaseNode.php 4.05 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\Node;

9
10
use Irstea\PlantUmlBundle\Model\ArrowInterface;
use Irstea\PlantUmlBundle\Model\NamespaceInterface;
11
12
use Irstea\PlantUmlBundle\Model\Node\Member\AttributeInterface;
use Irstea\PlantUmlBundle\Model\Node\Member\MethodInterface;
13
use Irstea\PlantUmlBundle\Model\NodeInterface;
14
use Irstea\PlantUmlBundle\Writer\WriterInterface;
15
16

/**
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
17
 * Description of Class.
18
 */
19
class BaseNode implements NodeInterface
20
21
22
23
{
    /**
     * @var string
     */
24
    private $label;
25

26
27
28
    /**
     * @var string
     */
29
    private $id;
30

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
    /**
     * @var string
     */
    private $nodeType;

    /**
     * @var string[]
     */
    private $classifiers;

    /**
     * @var string[]
     */
    private $stereotypes;

Guillaume Perréal's avatar
Guillaume Perréal committed
46
    /**
47
     * @var NamespaceInterface
Guillaume Perréal's avatar
Guillaume Perréal committed
48
     */
49
    private $namespace;
Guillaume Perréal's avatar
Guillaume Perréal committed
50

51
52
53
54
55
56
57
58
59
60
    /**
     * @var AttributeInterface[]
     */
    private $attributes = [];

    /**
     * @var MethodInterface[]
     */
    private $methods = [];

61
    /**
62
     * @param NamespaceInterface $namespace
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
63
64
65
66
     * @param string             $name
     * @param string             $nodeType
     * @param array              $classifiers
     * @param array              $stereotypes
67
     */
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
68
    public function __construct(NamespaceInterface $namespace, $name, $nodeType, array $classifiers = [], array $stereotypes = [])
69
    {
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
70
71
72
        $this->namespace = $namespace;
        $this->id = $namespace->getNodeId($name);
        $this->label = $namespace->getNodeLabel($name);
73

Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
74
        $this->nodeType = $nodeType;
75
76
77
78
        $this->classifiers = $classifiers;
        $this->stereotypes = $stereotypes;
    }

79
80
81
    public function addClassifier($classifier)
    {
        $this->classifiers[] = $classifier;
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
82

83
84
85
86
87
88
        return $this;
    }

    public function addStereotype($stereotype)
    {
        $this->stereotypes[] = $stereotype;
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
89

90
91
92
        return $this;
    }

93
94
95
    public function addAttribute(AttributeInterface $attribute)
    {
        $this->attributes[] = $attribute;
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
96

97
98
99
100
101
102
        return $this;
    }

    public function addMethod(MethodInterface $method)
    {
        $this->methods[] = $method;
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
103

104
105
106
        return $this;
    }

107
108
109
    public function setLabel($label)
    {
        $this->label = $label;
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
110

111
112
113
        return $this;
    }

114
    public function writeTo(WriterInterface $writer)
115
    {
116
        $this
117
118
            ->writeClassifiersTo($writer)
            ->writeNodeTypeTo($writer);
119
        $writer->writeFormatted('"%s" as %s', $this->label, $this->id);
120
        $this
121
            ->writeStereotypesTo($writer);
122
123
124
125
        $writer
            ->write(" {\n")
            ->indent();
        $this
126
127
            ->writeAttributesTo($writer)
            ->writeMethodsTo($writer);
128
129
130
        $writer
            ->dedent()
            ->write("}\n");
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
131

132
        return $this;
133
134
    }

135
    public function writeAliasTo(WriterInterface $writer)
136
    {
137
        $writer->write($this->id);
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
138

139
        return $this;
140
141
    }

142
    protected function writeClassifiersTo(WriterInterface $writer)
143
    {
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
144
        foreach ($this->classifiers as $classifier) {
145
            $writer->writeFormatted('%s ', $classifier);
146
        }
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
147

148
        return $this;
149
150
    }

151
    protected function writeNodeTypeTo(WriterInterface $writer)
152
    {
153
        $writer->writeFormatted('%s ', $this->nodeType);
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
154

155
        return $this;
156
157
    }

158
    protected function writeStereotypesTo(WriterInterface $writer)
159
    {
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
160
        foreach ($this->stereotypes as $stereotype) {
161
            $writer->writeFormatted(' <<%s>>', $stereotype);
162
        }
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
163

164
        return $this;
165
166
    }

167
    protected function writeAttributesTo(WriterInterface $writer)
168
    {
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
169
        foreach ($this->attributes as $attribute) {
170
171
            $attribute->writeTo($writer);
        }
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
172

173
        return $this;
174
175
    }

176
    protected function writeMethodsTo(WriterInterface $writer)
177
    {
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
178
        foreach ($this->methods as $method) {
179
180
            $method->writeTo($writer);
        }
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
181

182
        return $this;
183
    }
184
185
186
187

    public function addArrow(ArrowInterface $arrow)
    {
        $this->namespace->addArrow($arrow);
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
188

189
190
        return $this;
    }
191
}