janalis /
doctrineviz
| 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 Graph implements GraphInterface |
||
| 21 | { |
||
| 22 | use Attributable; |
||
| 23 | |||
| 24 | /** @var VertexInterface[] */ |
||
| 25 | protected $vertices; |
||
| 26 | |||
| 27 | /** @var EdgeInterface[] */ |
||
| 28 | protected $edges; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @return string |
||
| 32 | */ |
||
| 33 | public function __toString() |
||
| 34 | { |
||
| 35 | return 'digraph g {'.PHP_EOL.(!count($this->getAttributes()) ? '' : implode(PHP_EOL, $this->indentAll($this->getAttributes())). |
||
| 36 | PHP_EOL).(!count($this->getVertices()) ? '' : implode(PHP_EOL, $this->indentAll($this->getVertices())). |
||
| 37 | PHP_EOL).(!count($this->getEdges()) ? '' : implode(PHP_EOL, $this->indentAll($this->getEdges())). |
||
| 38 | PHP_EOL).'}'; |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Create vertex. |
||
| 43 | * |
||
| 44 | * @param string $id |
||
| 45 | * |
||
| 46 | * @return VertexInterface |
||
| 47 | */ |
||
| 48 | public function createVertex(string $id): VertexInterface |
||
| 49 | { |
||
| 50 | return new Vertex($id, $this); |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Get vertices. |
||
| 55 | * |
||
| 56 | * @return VertexInterface[] |
||
| 57 | */ |
||
| 58 | public function getVertices(): array |
||
| 59 | { |
||
| 60 | if ($this->vertices) { |
||
|
0 ignored issues
–
show
|
|||
| 61 | ksort($this->vertices); |
||
| 62 | } |
||
| 63 | |||
| 64 | return $this->vertices ? array_values($this->vertices) : []; |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Set vertices. |
||
| 69 | * |
||
| 70 | * @param VertexInterface[] $vertices |
||
| 71 | */ |
||
| 72 | public function setVertices(array $vertices): void |
||
| 73 | { |
||
| 74 | foreach ($vertices as $vertex) { |
||
| 75 | $this->addVertex($vertex); |
||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Get vertex. |
||
| 81 | * |
||
| 82 | * @param string $id |
||
| 83 | * |
||
| 84 | * @return VertexInterface |
||
| 85 | */ |
||
| 86 | public function getVertex(string $id): ?VertexInterface |
||
| 87 | { |
||
| 88 | if (!array_key_exists($id, $this->vertices)) { |
||
| 89 | return null; |
||
| 90 | } |
||
| 91 | |||
| 92 | return $this->vertices[$id]; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Add vertex. |
||
| 97 | * |
||
| 98 | * @param VertexInterface $vertex |
||
| 99 | */ |
||
| 100 | public function addVertex(VertexInterface $vertex): void |
||
| 101 | { |
||
| 102 | $vertex->setGraph($this); |
||
| 103 | $this->vertices[$vertex->getId()] = $vertex; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Remove vertex. |
||
| 108 | * |
||
| 109 | * @param VertexInterface $vertex |
||
| 110 | */ |
||
| 111 | public function removeVertex(VertexInterface $vertex): void |
||
| 112 | { |
||
| 113 | unset($this->vertices[$vertex->getId()]); |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Get edges. |
||
| 118 | * |
||
| 119 | * @return EdgeInterface[] |
||
| 120 | */ |
||
| 121 | public function getEdges(): array |
||
| 122 | { |
||
| 123 | if ($this->edges) { |
||
|
0 ignored issues
–
show
The expression
$this->edges of type Janalis\Doctrineviz\Graphviz\EdgeInterface[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent. Consider making the comparison explicit by using Loading history...
|
|||
| 124 | ksort($this->edges); |
||
| 125 | } |
||
| 126 | |||
| 127 | return $this->edges ? array_values($this->edges) : []; |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Set edges. |
||
| 132 | * |
||
| 133 | * @param EdgeInterface[] $edges |
||
| 134 | */ |
||
| 135 | public function setEdges(array $edges): void |
||
| 136 | { |
||
| 137 | $this->edges = $edges; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Get edge. |
||
| 142 | * |
||
| 143 | * @param string $id |
||
| 144 | * |
||
| 145 | * @return EdgeInterface|null |
||
| 146 | */ |
||
| 147 | public function getEdge(string $id): ?EdgeInterface |
||
| 148 | { |
||
| 149 | if (!array_key_exists($id, $this->edges)) { |
||
| 150 | return null; |
||
| 151 | } |
||
| 152 | |||
| 153 | return $this->edges[$id]; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Add edge. |
||
| 158 | * |
||
| 159 | * @param EdgeInterface $edge |
||
| 160 | * |
||
| 161 | * @return EdgeInterface |
||
| 162 | */ |
||
| 163 | public function addEdge(EdgeInterface $edge): EdgeInterface |
||
| 164 | { |
||
| 165 | $this->edges[(string) $edge] = $edge; |
||
| 166 | |||
| 167 | return $edge; |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Remove edge. |
||
| 172 | * |
||
| 173 | * @param EdgeInterface $edge |
||
| 174 | */ |
||
| 175 | public function removeEdge(EdgeInterface $edge): void |
||
| 176 | { |
||
| 177 | unset($this->edges[(string) $edge]); |
||
| 178 | } |
||
| 179 | } |
||
| 180 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.