Node::build()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 15
cts 15
cp 1
rs 9.584
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4
1
<?php
2
3
/**
4
 * @file
5
 * Grafizzi\Graph\Node: 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 Node extends AbstractElement {
29
30
  /**
31
   * Helper to simplify construction of implicit nodes.
32
   *
33
   * @return array
34
   */
35 10
  public static function implicit() {
36 10
    return array('implicit' => true);
37
  }
38
39
  /**
40
   * Node is implicit: it can be used in edge creations, but has no entry of its own.
41
   *
42
   * @var boolean
43
   */
44
  public $fImplicit = false;
45
46 45
  public function __construct(Container $dic, $name, array $attributes = array()) {
47 45
    parent::__construct($dic);
48 45
    if (isset($attributes['implicit'])) {
49 14
      $this->fImplicit = $attributes['implicit'];
50 14
      unset($attributes['implicit']);
51
    }
52 45
    $this->setAttributes($attributes);
53 45
    if (!isset($attributes['name'])) {
54 45
      $this->setName($name);
55
    }
56 45
  }
57
58
  /**
59
   * @see AbstractElement::build()
60
   */
61 27
  public function build($directed = null) {
62
    // Implicit nodes have no entry of their own.
63 27
    if ($this->fImplicit) {
64 5
       return '';
65
    }
66 24
    $type = $this->getType();
67 24
    $name = $this->getName();
68 24
    $this->logger->debug("Building $type $name, depth {$this->fDepth}.");
69
    $attributes = array_map(function (AttributeInterface $attribute) use ($directed) {
70 15
      return $attribute->build($directed);
71 24
    }, $this->fAttributes);
72 24
    $ret = str_repeat(' ', $this->fDepth * self::DEPTH_INDENT) . $this->escape($name);
73 24
    if (!empty($attributes)) {
74 15
      $builtAttributes = implode(', ', array_filter($attributes));
75 15
      if (!empty($builtAttributes)) {
76 13
        $ret .= " [ " . implode(', ', array_filter($attributes)) . " ]";
77
      }
78
    }
79 24
    $ret .= ";\n";
80 24
    return $ret;
81
  }
82
83 1
  public static function getAllowedChildTypes() {
84 1
    return array();
85
  }
86
87 45
  public function getType() {
88 45
    $ret = 'node';
89 45
    return $ret;
90
  }
91
}
92