Completed
Pull Request — master (#18)
by Chuck
03:41
created

Edge::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 2
1
<?php
2
/**
3
 * phpDocumentor
4
 *
5
 * PHP Version 5
6
 *
7
 * @author    Mike van Riel <[email protected]>
8
 * @copyright 2010-2011 Mike van Riel / Naenius (http://www.naenius.com)
9
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
10
 * @link      http://phpdoc.org
11
 */
12
13
namespace phpDocumentor\GraphViz;
14
15
/**
16
 * Class representing an edge (arrow, line).
17
 *
18
 * @author    Mike van Riel <[email protected]>
19
 * @copyright 2010-2011 Mike van Riel / Naenius (http://www.naenius.com)
20
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
21
 * @link      http://phpdoc.org
22
 */
23
class Edge
24
{
25
    /** @var \phpDocumentor\GraphViz\Node Node from where to link */
26
    protected $from = null;
27
28
    /** @var \phpDocumentor\GraphViz\Node Node where to to link */
29
    protected $to = null;
30
31
    /** @var \phpDocumentor\GraphViz\Attribute[] List of attributes for this edge */
32
    protected $attributes = [];
33
34
    /**
35
     * Creates a new Edge / Link between the given nodes.
36
     *
37
     * @param \phpDocumentor\GraphViz\Node $from Starting node to create an Edge from.
38
     * @param \phpDocumentor\GraphViz\Node $to   Destination node where to create and
39
     *  edge to.
40
     */
41
    public function __construct(Node $from, Node $to)
42
    {
43
        $this->from = $from;
44
        $this->to = $to;
45
    }
46
47
    /**
48
     * Factory method used to assist with fluent interface handling.
49
     *
50
     * See the examples for more details.
51
     *
52
     * @param \phpDocumentor\GraphViz\Node $from Starting node to create an Edge from.
53
     * @param \phpDocumentor\GraphViz\Node $to   Destination node where to create and
54
     *  edge to.
55
     *
56
     * @return \phpDocumentor\GraphViz\Edge
57
     */
58 1
    public static function create(Node $from, Node $to)
59
    {
60 1
        return new self($from, $to);
61
    }
62
63
    /**
64
     * Returns the source Node for this Edge.
65
     *
66
     * @return \phpDocumentor\GraphViz\Node
67
     */
68 1
    public function getFrom()
69
    {
70 1
        return $this->from;
71
    }
72
73
    /**
74
     * Returns the destination Node for this Edge.
75
     *
76
     * @return \phpDocumentor\GraphViz\Node
77
     */
78 1
    public function getTo()
79
    {
80 1
        return $this->to;
81
    }
82
83
    /**
84
     * Magic method to provide a getter/setter to add attributes on the edge.
85
     *
86
     * Using this method we make sure that we support any attribute without too
87
     * much hassle. If the name for this method does not start with get or set
88
     * we return null.
89
     *
90
     * Set methods return this graph (fluent interface) whilst get methods
91
     * return the attribute value.
92
     *
93
     * @param string  $name      name of the invoked method, expect it to be
94
     *  setX or getX.
95
     * @param mixed[] $arguments Arguments for the setter, only 1 is expected: value
96
     *
97
     * @return \phpDocumentor\GraphViz\Attribute|\phpDocumentor\GraphViz\Edge|null
98
     */
99 1
    public function __call($name, $arguments)
100
    {
101 1
        $key = strtolower(substr($name, 3));
102 1
        if (strtolower(substr($name, 0, 3)) === 'set') {
103 1
            $this->attributes[$key] = new Attribute($key, $arguments[0]);
104
105 1
            return $this;
106
        }
107
108 1
        if (strtolower(substr($name, 0, 3)) === 'get') {
109 1
            return $this->attributes[$key];
110
        }
111
112 1
        return null;
113
    }
114
115
    /**
116
     * Returns the edge definition as is requested by GraphViz.
117
     *
118
     * @return string
119
     */
120 1
    public function __toString()
121
    {
122 1
        $attributes = [];
123 1
        foreach ($this->attributes as $value) {
124 1
            $attributes[] = (string) $value;
125
        }
126
127 1
        $attributes = implode("\n", $attributes);
128
129 1
        $from_name = addslashes($this->getFrom()->getName());
130 1
        $to_name = addslashes($this->getTo()->getName());
131
132
        return <<<DOT
133
"${from_name}" -> "${to_name}" [
134
${attributes}
135
]
136
DOT;
137
    }
138
}
139