Vertex::getAttributes()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 2
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
/*
4
 * This file is part of the doctrineviz package
5
 *
6
 * Copyright (c) 2017 Pierre Hennequart
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * Feel free to edit as you please, and have fun.
12
 *
13
 * @author Pierre Hennequart <[email protected]>
14
 */
15
16
declare(strict_types=1);
17
18
namespace Janalis\Doctrineviz\Graphviz;
19
20
class Vertex extends Element implements VertexInterface
21
{
22
    use Attributable;
23
    use Edgeable;
24
25
    /** @var RecordInterface[] */
26
    protected $records = [];
27
28
    /**
29
     * @return string
30
     */
31
    public function __toString()
32
    {
33
        return "$this->id [".PHP_EOL.
34
            implode(PHP_EOL, $this->indentAll($this->getAttributes())).
35
            PHP_EOL.']';
36
    }
37
38
    /**
39
     * Get attributes.
40
     *
41
     * @return AttributeInterface[]
42
     */
43
    public function getAttributes(): array
44
    {
45
        $this->createAttribute('label', count($this->records) ? mb_convert_case($this->id, MB_CASE_UPPER).'|'.implode('|', $this->records) : $this->id);
46
47
        return $this->attributes ? array_values($this->attributes) : [];
48
    }
49
50
    /**
51
     * Vertex constructor.
52
     *
53
     * @param string         $id
54
     * @param GraphInterface $graph
55
     */
56
    public function __construct(string $id = null, GraphInterface $graph = null)
57
    {
58
        $this->id = $id;
59
        if ($graph) {
60
            $graph->addVertex($this);
61
        }
62
    }
63
64
    /**
65
     * Get records.
66
     *
67
     * @return RecordInterface[]
68
     */
69
    public function getRecords(): array
70
    {
71
        return $this->records ? array_values($this->records) : [];
72
    }
73
74
    /**
75
     * Set records.
76
     *
77
     * @param RecordInterface[] $records
78
     */
79
    public function setRecords(array $records): void
80
    {
81
        foreach ($records as $record) {
82
            $this->addRecord($record);
83
        }
84
    }
85
86
    /**
87
     * Add record.
88
     *
89
     * @param RecordInterface $record
90
     */
91
    public function addRecord(RecordInterface $record): void
92
    {
93
        $record->setGraph($this->graph);
94
        $record->setVertex($this);
95
        $this->records[$record->getId()] = $record;
96
    }
97
98
    /**
99
     * Remove record.
100
     *
101
     * @param RecordInterface $record
102
     */
103
    public function removeRecord(RecordInterface $record): void
104
    {
105
        unset($this->records[$record->getId()]);
106
    }
107
108
    /**
109
     * Create record.
110
     *
111
     * @param string $id
112
     *
113
     * @return RecordInterface
114
     */
115
    public function createRecord(string $id): RecordInterface
116
    {
117
        return new Record($id, $this);
118
    }
119
120
    /**
121
     * Delete record.
122
     *
123
     * @param string $id
124
     */
125
    public function deleteRecord(string $id): void
126
    {
127
        unset($this->records[$id]);
128
    }
129
130
    /**
131
     * Get record.
132
     *
133
     * @param string $id
134
     *
135
     * @return RecordInterface
136
     */
137
    public function getRecord(string $id): ?RecordInterface
138
    {
139
        if (!array_key_exists($id, $this->records)) {
140
            return null;
141
        }
142
143
        return $this->records[$id];
144
    }
145
}
146