Graph::addStyle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
/**
4
 * Platine Framework
5
 *
6
 * Platine Framework is a lightweight, high-performance, simple and elegant
7
 * PHP Web framework
8
 *
9
 * This content is released under the MIT License (MIT)
10
 *
11
 * Copyright (c) 2020 Platine Framework
12
 * Copyright (c) 2015 JBZoo Content Construction Kit (CCK)
13
 *
14
 * Permission is hereby granted, free of charge, to any person obtaining a copy
15
 * of this software and associated documentation files (the "Software"), to deal
16
 * in the Software without restriction, including without limitation the rights
17
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18
 * copies of the Software, and to permit persons to whom the Software is
19
 * furnished to do so, subject to the following conditions:
20
 *
21
 * The above copyright notice and this permission notice shall be included in all
22
 * copies or substantial portions of the Software.
23
 *
24
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30
 * SOFTWARE.
31
 */
32
33
/**
34
 * @file Graph.php
35
 *
36
 * The Graph class
37
 *
38
 *  @package    Platine\Framework\Helper\Mermaid\Graph
39
 *  @author Platine Developers Team
40
 *  @copyright  Copyright (c) 2020
41
 *  @license    http://opensource.org/licenses/MIT  MIT License
42
 *  @link   https://www.platine-php.com
43
 *  @version 1.0.0
44
 *  @filesource
45
 */
46
declare(strict_types=1);
47
48
namespace Platine\Framework\Helper\Mermaid\Graph;
49
50
use Stringable;
51
52
/**
53
 * @class Graph
54
 * @package Platine\Framework\Helper\Mermaid\Graph
55
 */
56
class Graph implements Stringable
57
{
58
   /**
59
    * Constants
60
   */
61
    public const TOP_BOTTOM = 'TB';
62
    public const BOTTOM_TOP = 'BT';
63
    public const LEFT_RIGHT = 'LR';
64
    public const RIGHT_LEFT = 'RL';
65
66
   /**
67
    * Space for each sub graph
68
    */
69
    private const RENDER_SHIFT = 4;
70
71
   /**
72
    * The list of sub graph
73
    * @var array<Graph>
74
    */
75
    protected array $subGraphs = [];
76
77
   /**
78
    * The list of nodes
79
    * @var array<string, Node>
80
    */
81
    protected array $nodes = [];
82
83
   /**
84
    * The list of links
85
    * @var array<Link>
86
    */
87
    protected array $links = [];
88
89
   /**
90
    * The graph parameter
91
    * @var array<string, mixed>
92
    */
93
    protected array $params = [
94
       'title' => 'Graph',
95
       'direction' => self::LEFT_RIGHT,
96
    ];
97
98
   /**
99
    * Style list
100
    * @var array<string>
101
    */
102
    protected array $styles = [];
103
104
   /**
105
    * Create new instance
106
    * @param array<string, mixed> $params
107
    */
108
    public function __construct(array $params = [])
109
    {
110
        $this->setParams($params);
111
    }
112
113
   /**
114
    * Render the graph to string representation
115
    * @param bool $isMainGraph
116
    * @param int $shift
117
    * @return string
118
    */
119
    public function render(bool $isMainGraph = true, int $shift = 0): string
120
    {
121
        $spaces = str_repeat(' ', $shift);
122
        $spacesSub = str_repeat(' ', $shift + self::RENDER_SHIFT);
123
        /** @var array<string> */
124
        $result = [];
125
        if ($isMainGraph) {
126
            $result = [sprintf('graph %s;', $this->params['direction'])];
127
        } else {
128
            $result = [sprintf(
129
                '%ssubgraph %s',
130
                $spaces,
131
                Helper::escape($this->params['title'])
132
            )];
133
        }
134
135
        if (count($this->nodes) > 0) {
136
            $tmp = [];
137
            foreach ($this->nodes as $node) {
138
                $tmp[] = $spacesSub . $node;
139
            }
140
            $result = array_merge($result, $tmp);
141
            if ($isMainGraph) {
142
                $result[] = '';
143
            }
144
        }
145
146
        if (count($this->links) > 0) {
147
            $tmp = [];
148
            foreach ($this->links as $link) {
149
                $tmp[] = $spacesSub . $link;
150
            }
151
            $result = array_merge($result, $tmp);
152
            if ($isMainGraph) {
153
                $result[] = '';
154
            }
155
        }
156
157
        foreach ($this->subGraphs as $subGraph) {
158
            $result[] = $subGraph->render(false, $shift + 4);
159
        }
160
161
        if ($isMainGraph && count($this->styles) > 0) {
162
            foreach ($this->styles as $style) {
163
                $result[] = $spaces . $style . ';';
164
            }
165
        }
166
167
        if (!$isMainGraph) {
168
            $result[] = $spaces . 'end';
169
        }
170
171
        return implode(PHP_EOL, $result);
172
    }
173
174
   /**
175
    * Add new node
176
    * @param Node $node
177
    * @return $this
178
    */
179
    public function addNode(Node $node): self
180
    {
181
        $this->nodes[$node->getId()] = $node;
182
183
        return $this;
184
    }
185
186
   /**
187
    * Add new link
188
    * @param Link $link
189
    * @return $this
190
    */
191
    public function addLink(Link $link): self
192
    {
193
        $this->links[] = $link;
194
195
        return $this;
196
    }
197
198
   /**
199
    * Add new style
200
    * @param string $style
201
    * @return $this
202
    */
203
    public function addStyle(string $style): self
204
    {
205
        $this->styles[] = $style;
206
207
        return $this;
208
    }
209
210
   /**
211
    * Add new sub graph
212
    * @param Graph $subGraph
213
    * @return $this
214
    */
215
    public function addSubGraph(Graph $subGraph): self
216
    {
217
        $this->subGraphs[] = $subGraph;
218
219
        return $this;
220
    }
221
222
   /**
223
    * Return the string representation
224
    * @return string
225
    */
226
    public function __toString(): string
227
    {
228
        return $this->render();
229
    }
230
231
232
   /**
233
    * Return the parameters
234
    * @return array<string, mixed>
235
    */
236
    public function getParams(): array
237
    {
238
        return $this->params;
239
    }
240
241
   /**
242
    * Set the parameters
243
    * @param array<string, mixed> $params
244
    * @return $this
245
    */
246
    public function setParams(array $params)
247
    {
248
        $this->params = array_merge($this->params, $params);
249
        return $this;
250
    }
251
252
   /**
253
    * Return the list of sub graph
254
    * @return array<Graph>
255
    */
256
    public function getSubGraphs(): array
257
    {
258
        return $this->subGraphs;
259
    }
260
261
   /**
262
    * Return the list of node
263
    * @return array<string, Node>
264
    */
265
    public function getNodes(): array
266
    {
267
        return $this->nodes;
268
    }
269
270
   /**
271
    * Return the list of link
272
    * @return array<Link>
273
    */
274
    public function getLinks(): array
275
    {
276
        return $this->links;
277
    }
278
279
   /**
280
    * Return the list of style
281
    * @return array<string>
282
    */
283
    public function getStyles(): array
284
    {
285
        return $this->styles;
286
    }
287
}
288