BaseNode.php 3.03 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
<?php

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

namespace Irstea\PlantUmlBundle\Model\Node;

11
12
use Irstea\PlantUmlBundle\Model\ArrowInterface;
use Irstea\PlantUmlBundle\Model\NamespaceInterface;
13
use Irstea\PlantUmlBundle\Model\NodeInterface;
14
use Irstea\PlantUmlBundle\Writer\WriterInterface;
15
16
17
18
19
20

/**
 * Description of Class
 *
 * @author Guillaume Perréal <guillaume.perreal@irstea.fr>
 */
21
class BaseNode implements NodeInterface
22
23
24
25
26
27
{
    /**
     * @var string
     */
    private $name;

28
29
30
31
32
    /**
     * @var string
     */
    private $alias;

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

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

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

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

53
    /**
54
     * @param NamespaceInterface $namespace
55
56
57
58
59
     * @param string $name
     * @param string $nodeType
     * @param array $classifiers
     * @param array $stereotypes
     */
60
    function __construct(NamespaceInterface $namespace, $name, $nodeType, array $classifiers = [], array $stereotypes = [])
61
    {
62
63
        $this->namespace = $namespace;

64
65
66
67
        $pos = strrpos($name, '\\');
        $this->name        = substr($name, $pos + 1);
        $this->alias       = str_replace('\\', '.', $name)."_node";

68
69
70
71
72
        $this->nodeType    = $nodeType;
        $this->classifiers = $classifiers;
        $this->stereotypes = $stereotypes;
    }

73
    public function outputTo(WriterInterface $writer)
74
    {
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
        $this
            ->outputClassifiersTo($writer)
            ->outputNodeTypeTo($writer);
        $writer->printf('"%s" as %s', $this->name, $this->alias);
        $this
            ->outputStereotypesTo($writer);
        $writer
            ->write(" {\n")
            ->indent();
        $this
            ->outputAttributesTo($writer)
            ->outputMethodsTo($writer);
        $writer
            ->dedent()
            ->write("}\n");
        return $this;
91
92
    }

93
    public function outputAliasTo(WriterInterface $writer)
94
    {
95
96
        $writer->write($this->alias);
        return $this;
97
98
    }

99
    protected function outputClassifiersTo(WriterInterface $writer)
100
101
    {
        foreach($this->classifiers as $classifier) {
102
            $writer->printf('%s ', $classifier);
103
        }
104
        return $this;
105
106
    }

107
    protected function outputNodeTypeTo(WriterInterface $writer)
108
    {
109
110
        $writer->printf('%s ', $this->nodeType);
        return $this;
111
112
    }

113
    protected function outputStereotypesTo(WriterInterface $writer)
114
    {
115
116
        foreach($this->stereotypes as $stereotype) {
            $writer->printf(' <<%s>>', $stereotype);
117
        }
118
        return $this;
119
120
    }

121
    protected function outputAttributesTo(WriterInterface $writer)
122
    {
123
        return $this;
124
125
    }

126
    protected function outputMethodsTo(WriterInterface $writer)
127
    {
128
        return $this;
129
    }
130
131
132
133
134
135

    public function addArrow(ArrowInterface $arrow)
    {
        $this->namespace->addArrow($arrow);
        return $this;
    }
136
}