Graph::buildChildrenHelper()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
/**
4
 * @file
5
 * Grafizzi\Graph\Graph: a component of the Grafizzi library.
6
 *
7
 * (c) 2012 Frédéric G. MARAND <[email protected]>
8
 *
9
 * Grafizzi is free software: you can redistribute it and/or modify it under the
10
 * terms of the GNU Lesser General Public License as published by the Free
11
 * Software Foundation, either version 3 of the License, or (at your option) any
12
 * later version.
13
 *
14
 * Grafizzi is distributed in the hope that it will be useful, but WITHOUT ANY
15
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16
 * A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
17
 * details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public License
20
 * along with Grafizzi, in the COPYING.LESSER.txt file.  If not, see
21
 * <http://www.gnu.org/licenses/>
22
 */
23
24
namespace Grafizzi\Graph;
25
26
use Pimple\Container;
27
28
class Graph extends AbstractElement implements GraphInterface {
29
30
  /**
31
   * Helper to simplify construction of strict graphs.
32
   *
33
   * @return array
34
   */
35 1
  public static function strict() {
36 1
    return array('strict' => true);
37
  }
38
39
  /**
40
   * Generate non-strict graphs by default
41
   *
42
   * @var boolean
43
   */
44
  public $fStrict = false;
45
46
  /**
47
   * Generate digraphs by default.
48
   */
49
  public $fDirected = true;
50
51
  /**
52
   * @param \Pimple\Container $dic
53
   * @param string $name
54
   * @param array $attributes
55
   */
56 71
  public function __construct(Container $dic, $name = 'G', array $attributes = array()) {
57 71
    if (!isset($dic['directed'])) {
58 71
      $dic['directed'] = true;
59
    }
60 71
    parent::__construct($dic);
61 71
    $this->setName($name);
62 71
    if (!empty($attributes)) {
63 13
      if (isset($attributes['strict'])) {
64 6
        $this->fStrict = $attributes['strict'];
65 6
        unset($attributes['strict']);
66
      }
67 13
      $this->setAttributes($attributes);
68
    }
69 71
  }
70
71
  /**
72
   * @param bool|null $directed
73
   *
74
   * @return string
75
   */
76 33
  public function build($directed = null) {
77
    // Allow overriding the build directed attribute.
78 33
    if (isset($directed)) {
79 15
      $savedDirected = $this->getDirected();
80 15
      $this->setDirected($directed);
81
    }
82
    else {
83 33
      $savedDirected = true;
84
    }
85
86 33
    $actualDirected = $this->getDirected();
87 33
    $type = $this->getType();
88 33
    $buildName = $this->getBuildName();
89 33
    $elementIndent = str_repeat(' ', $this->fDepth * self::DEPTH_INDENT);
90 33
    $childIndent = str_repeat(' ', ($this->fDepth + 1) * self::DEPTH_INDENT);
91
92 33
    $strict = $this->fStrict ? 'strict ' : '';
93 33
    $ret = "$elementIndent$strict$type $buildName {\n";
94
95 33
    $ret_attributes = $this->buildAttributesHelper($this->fAttributes, $actualDirected, $childIndent);
96 33
    $ret .= $ret_attributes;
97
98 33
    $ret_children = $this->buildChildrenHelper($this->fChildren, $actualDirected);
99
100 33
    if (!empty($ret_attributes) && !empty($ret_children)) {
101 11
      $ret .= "\n";
102
    }
103 33
    $ret .= "$ret_children$elementIndent} /* /$type $buildName */\n";
104
105
    // Restore the directed attribute if it was changed for build.
106 33
    if (isset($directed)) {
107 15
      $this->setDirected($savedDirected);
108
    }
109 33
    return $ret;
110
  }
111
112
  /**
113
   * Helper for Graph::build(): build attributes.
114
   *
115
   * Unrelated with AbstractElement::buildAttributes().
116
   *
117
   * @param \Grafizzi\Graph\AttributeInterface[] $attributes
118
   * @param bool $directed
119
   * @param string $childIndent
120
   *
121
   * @return string
122
   */
123 33
  protected function buildAttributesHelper(array $attributes, $directed, $childIndent) {
124 33
    $ret = '';
125
    /** @var \Grafizzi\Graph\AttributeInterface $attribute */
126 33
    foreach ($attributes as $attribute) {
127 18
      if ($built = $attribute->build($directed)) {
128 16
        $ret .= "$childIndent$built;\n";
129
      }
130
    }
131 33
    return $ret;
132
  }
133
134
  /**
135
   * Helper for Graph::build(): build children.
136
   *
137
   * @param \Grafizzi\Graph\AbstractElement[] $children
138
   * @param bool $directed
139
   *
140
   * @return string
141
   */
142 33
  public function buildChildrenHelper(array $children, $directed) {
143 33
    $ret = '';
144
    /** @var \Grafizzi\Graph\AbstractElement $child */
145 33
    foreach ($children as $child) {
146 24
      $ret .= $child->build($directed);
147
    }
148 33
    return $ret;
149
  }
150
151 40
  public static function getAllowedChildTypes() {
152
    $ret = array(
153 40
      'cluster',
154
      'edge',
155
      'multiedge', // Grafizzi extension
156
      'node',
157
      'subgraph',
158
    );
159 40
    return $ret;
160
  }
161
162
  /**
163
   * @return bool
164
   */
165 71
  public function getDirected() {
166 71
    $ret = $this->fDirected;
167 71
    return $ret;
168
  }
169
170
  /**
171
   * @return string
172
   */
173 71
  public function getType() {
174 71
    $ret = $this->getDirected() ? 'digraph' : 'graph';
175 71
    return $ret;
176
  }
177
178
  /**
179
   * @param bool $directed
180
   */
181 31
  public function setDirected($directed) {
182 31
    $this->fDirected = $directed;
183 31
  }
184
}
185