Edge::getFrom()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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