|
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
|
|
|
trait Attributable |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var Attribute[] */ |
|
23
|
|
|
protected $attributes = []; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Indent all. |
|
27
|
|
|
* |
|
28
|
|
|
* @param array $strings |
|
29
|
|
|
* @param int $spaces |
|
30
|
|
|
* |
|
31
|
|
|
* @return array |
|
32
|
|
|
*/ |
|
33
|
|
|
protected function indentAll(array $strings = null, int $spaces = 2): array |
|
34
|
|
|
{ |
|
35
|
|
|
$strings = $strings ?: []; |
|
36
|
|
|
foreach ($strings as $key => $string) { |
|
37
|
|
|
$strings[$key] = $this->indent((string) $string, $spaces); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return $strings; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Indent. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $string |
|
47
|
|
|
* @param int $spaces |
|
48
|
|
|
* |
|
49
|
|
|
* @return string |
|
50
|
|
|
*/ |
|
51
|
|
|
protected function indent(string $string, int $spaces = 2): string |
|
52
|
|
|
{ |
|
53
|
|
|
$pad = str_repeat(' ', $spaces); |
|
54
|
|
|
|
|
55
|
|
|
return $pad.implode(PHP_EOL."$pad", explode(PHP_EOL, $string)); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Get attributes. |
|
60
|
|
|
* |
|
61
|
|
|
* @return AttributeInterface[] |
|
62
|
|
|
*/ |
|
63
|
|
|
public function getAttributes(): array |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->attributes ? array_values($this->attributes) : []; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Set attributes. |
|
70
|
|
|
* |
|
71
|
|
|
* @param AttributeInterface[] $attributes |
|
72
|
|
|
*/ |
|
73
|
|
|
public function setAttributes(array $attributes): void |
|
74
|
|
|
{ |
|
75
|
|
|
foreach ($attributes as $attribute) { |
|
76
|
|
|
$this->attributes[$attribute->getId()] = $attribute; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Get attribute. |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $id |
|
84
|
|
|
* |
|
85
|
|
|
* @return AttributeInterface|null |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getAttribute(string $id): ?AttributeInterface |
|
88
|
|
|
{ |
|
89
|
|
|
if (!array_key_exists($id, $this->attributes)) { |
|
90
|
|
|
return null; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $this->attributes[$id]; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Create attribute. |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $id |
|
100
|
|
|
* @param string $value |
|
101
|
|
|
* |
|
102
|
|
|
* @return AttributeInterface |
|
103
|
|
|
*/ |
|
104
|
|
|
public function createAttribute(string $id, string $value): AttributeInterface |
|
105
|
|
|
{ |
|
106
|
|
|
$attribute = new Attribute($id, $value); |
|
107
|
|
|
$this->attributes[$id] = $attribute; |
|
108
|
|
|
|
|
109
|
|
|
return $attribute; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Delete attribute. |
|
114
|
|
|
* |
|
115
|
|
|
* @param string $id |
|
116
|
|
|
*/ |
|
117
|
|
|
public function deleteAttribute(string $id): void |
|
118
|
|
|
{ |
|
119
|
|
|
unset($this->attributes[$id]); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Add attribute. |
|
124
|
|
|
* |
|
125
|
|
|
* @param AttributeInterface $attribute |
|
126
|
|
|
*/ |
|
127
|
|
|
public function addAttribute(AttributeInterface $attribute): void |
|
128
|
|
|
{ |
|
129
|
|
|
$this->attributes[$attribute->getId()] = $attribute; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Remove attribute. |
|
134
|
|
|
* |
|
135
|
|
|
* @param AttributeInterface $attribute |
|
136
|
|
|
*/ |
|
137
|
|
|
public function removeAttribute(AttributeInterface $attribute): void |
|
138
|
|
|
{ |
|
139
|
|
|
unset($this->attributes[$attribute->getId()]); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|