1 | <?php |
||
47 | class Graph |
||
48 | { |
||
49 | use Attributes; |
||
50 | |||
51 | /** @var string Name of this graph */ |
||
52 | protected $name = 'G'; |
||
53 | |||
54 | /** @var string Type of this graph; may be digraph, graph or subgraph */ |
||
55 | protected $type = 'digraph'; |
||
56 | |||
57 | /** @var bool If the graph is strict then multiple edges are not allowed between the same pairs of nodes */ |
||
58 | protected $strict = false; |
||
59 | |||
60 | /** @var Graph[] A list of subgraphs for this Graph */ |
||
61 | protected $graphs = []; |
||
62 | |||
63 | /** @var Node[] A list of nodes for this Graph */ |
||
64 | protected $nodes = []; |
||
65 | |||
66 | /** @var Edge[] A list of edges / arrows for this Graph */ |
||
67 | protected $edges = []; |
||
68 | |||
69 | /** @var string The path to execute dot from */ |
||
70 | protected $path = ''; |
||
71 | |||
72 | /** |
||
73 | * Factory method to instantiate a Graph so that you can use fluent coding |
||
74 | * to chain everything. |
||
75 | * |
||
76 | * @param string $name The name for this graph. |
||
77 | * @param bool $directional Whether this is a directed or undirected graph. |
||
78 | * |
||
79 | * @return Graph |
||
80 | */ |
||
81 | 1 | public static function create(string $name = 'G', bool $directional = true) : self |
|
90 | |||
91 | /** |
||
92 | * Sets the path for the execution. Only needed if it is not in the PATH env. |
||
93 | * |
||
94 | * @param string $path The path to execute dot from |
||
95 | */ |
||
96 | 1 | public function setPath(string $path) : self |
|
105 | |||
106 | /** |
||
107 | * Sets the name for this graph. |
||
108 | * |
||
109 | * If this is a subgraph you can prefix the name with _cluster_ to group all |
||
110 | * contained nodes and add a border. |
||
111 | * |
||
112 | * @param string $name The new name for this graph. |
||
113 | */ |
||
114 | 1 | public function setName(string $name) : self |
|
120 | |||
121 | /** |
||
122 | * Returns the name for this Graph. |
||
123 | */ |
||
124 | 1 | public function getName() : string |
|
128 | |||
129 | /** |
||
130 | * Sets the type for this graph. |
||
131 | * |
||
132 | * @param string $type Must be either "digraph", "graph" or "subgraph". |
||
133 | * |
||
134 | * @throws InvalidArgumentException If $type is not "digraph", "graph" or |
||
135 | * "subgraph". |
||
136 | */ |
||
137 | 2 | public function setType(string $type) : self |
|
150 | |||
151 | /** |
||
152 | * Returns the type of this Graph. |
||
153 | */ |
||
154 | 1 | public function getType() : string |
|
158 | |||
159 | /** |
||
160 | * Set if the Graph should be strict. If the graph is strict then |
||
161 | * multiple edges are not allowed between the same pairs of nodes |
||
162 | */ |
||
163 | 2 | public function setStrict(bool $isStrict) : self |
|
169 | |||
170 | 1 | public function isStrict() : bool |
|
174 | |||
175 | /** |
||
176 | * Magic method to provide a getter/setter to add attributes on the Graph. |
||
177 | * |
||
178 | * Using this method we make sure that we support any attribute without |
||
179 | * too much hassle. If the name for this method does not start with get |
||
180 | * or set we return null. |
||
181 | * |
||
182 | * Set methods return this graph (fluent interface) whilst get methods |
||
183 | * return the attribute value. |
||
184 | * |
||
185 | * @param string $name Name of the method including get/set |
||
186 | * @param mixed[] $arguments The arguments, should be 1: the value |
||
187 | * |
||
188 | * @return Attribute|Graph|null |
||
189 | * |
||
190 | * @throws AttributeNotFound |
||
191 | */ |
||
192 | 1 | public function __call(string $name, array $arguments) |
|
205 | |||
206 | /** |
||
207 | * Adds a subgraph to this graph; automatically changes the type to subgraph. |
||
208 | * |
||
209 | * Please note that an index is maintained using the name of the subgraph. |
||
210 | * Thus if you have 2 subgraphs with the same name that the first will be |
||
211 | * overwritten by the latter. |
||
212 | * |
||
213 | * @see Graph::create() |
||
214 | * |
||
215 | * @param Graph $graph The graph to add onto this graph as |
||
216 | * subgraph. |
||
217 | */ |
||
218 | 1 | public function addGraph(self $graph) : self |
|
225 | |||
226 | /** |
||
227 | * Checks whether a graph with a certain name already exists. |
||
228 | * |
||
229 | * @param string $name Name of the graph to find. |
||
230 | */ |
||
231 | 1 | public function hasGraph(string $name) : bool |
|
235 | |||
236 | /** |
||
237 | * Returns the subgraph with a given name. |
||
238 | * |
||
239 | * @param string $name Name of the requested graph. |
||
240 | */ |
||
241 | 1 | public function getGraph(string $name) : self |
|
245 | |||
246 | /** |
||
247 | * Sets a node in the $nodes array; uses the name of the node as index. |
||
248 | * |
||
249 | * Nodes can be retrieved by retrieving the property with the same name. |
||
250 | * Thus 'node1' can be retrieved by invoking: $graph->node1 |
||
251 | * |
||
252 | * @see Node::create() |
||
253 | * |
||
254 | * @param Node $node The node to set onto this Graph. |
||
255 | */ |
||
256 | 1 | public function setNode(Node $node) : self |
|
262 | |||
263 | /** |
||
264 | * Finds a node in this graph or any of its subgraphs. |
||
265 | * |
||
266 | * @param string $name Name of the node to find. |
||
267 | */ |
||
268 | 1 | public function findNode(string $name) : ?Node |
|
283 | |||
284 | /** |
||
285 | * Sets a node using a custom name. |
||
286 | * |
||
287 | * @see Graph::setNode() |
||
288 | * |
||
289 | * @param string $name Name of the node. |
||
290 | * @param Node $value Node to set on the given name. |
||
291 | */ |
||
292 | 1 | public function __set(string $name, Node $value) : void |
|
296 | 1 | ||
297 | /** |
||
298 | * Returns the requested node by its name. |
||
299 | * |
||
300 | * @see Graph::setNode() |
||
301 | * |
||
302 | * @param string $name The name of the node to retrieve. |
||
303 | */ |
||
304 | public function __get(string $name) : ?Node |
||
308 | 1 | ||
309 | /** |
||
310 | * Links two nodes to eachother and registers the Edge onto this graph. |
||
311 | * |
||
312 | * @see Edge::create() |
||
313 | * |
||
314 | * @param Edge $edge The link between two classes. |
||
315 | */ |
||
316 | public function link(Edge $edge) : self |
||
322 | 1 | ||
323 | /** |
||
324 | * Exports this graph to a generated image. |
||
325 | * |
||
326 | * This is the only method that actually requires GraphViz. |
||
327 | * |
||
328 | * @link http://www.graphviz.org/content/output-formats |
||
329 | * @uses GraphViz/dot |
||
330 | * |
||
331 | * @param string $type The type to export to; see the link above for a |
||
332 | * list of supported types. |
||
333 | * @param string $filename The path to write to. |
||
334 | * |
||
335 | * @throws Exception If an error occurred in GraphViz. |
||
336 | */ |
||
337 | public function export(string $type, string $filename) : self |
||
364 | 1 | ||
365 | /** |
||
366 | * Generates a DOT file for use with GraphViz. |
||
367 | * |
||
368 | * GraphViz is not used in this method; it is safe to call it even without |
||
369 | * GraphViz installed. |
||
370 | */ |
||
371 | public function __toString() : string |
||
395 | } |
||
396 |