Completed
Push — 4-psr3 ( 959fa0...cce1de )
by Frédéric G.
03:27
created

MultiEdge::getType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * @file
5
 * Grafizzi\Graph\MultiEdge: 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
/**
29
 * A MultiEdge is a graph object made up of several nodes linked by edges, with
30
 * only one set of attributes. In GraphViz format, this means something like:
31
 *
32
 * foo -> bar -> baz [ label=Quux ];
33
 *
34
 * It provides a simpler source representation for node chains than multiple
35
 * edges.
36
 */
37
class MultiEdge extends AbstractElement {
38
39
  /**
40
   * Array of Node objects
41
   *
42
   * @var array
43
   */
44
  public $fNodes;
45
46
  /**
47
   * @var boolean
48
   */
49
  public $fDirected = true;
50
51 7
  public function __construct(Container $dic, array $nodes, array $attributes = array()) {
52 7
    parent::__construct($dic);
53
    $nonNodes = array_filter($nodes, function ($node) {
54 7
      return !($node instanceof Node);
55 7
    });
56 7
    if (!empty($nonNodes)) {
57
      throw new ChildTypeException("Trying to add non-nodes to a multi-edge element.");
58
    }
59 7
    $this->fNodes = $nodes;
60
    $this->setName(implode('--', array_map(function (Node $node) {
61 7
      return $node->getName();
62 7
    }, $this->fNodes)));
63 7
    $this->setAttributes($attributes);
64 7
  }
65
66
  /**
67
   * @param bool $directed
68
   *
69
   * @return string
70
   */
71 5
  public function build($directed = null) {
72 5
    $type = $this->getType();
73 5
    $name = $this->getName();
74 5
    if (!isset($directed)) {
75 1
      $root = $this->getRoot();
76 1
      if ($root instanceof GraphInterface) {
77 1
        $directed = $root->getDirected();
78 1
      }
79 1
    }
80 5
    $this->logger->debug("Building $type $name, depth {$this->fDepth}.");
81 5
    $joiner = ($directed ? ' -> ' : ' -- ');
82 5
    $ret = str_repeat(' ', $this->fDepth * self::DEPTH_INDENT)
83
      . implode($joiner, array_map(function (Node $node) {
84 5
        return $node->getBuildName();
85 5
      }, $this->fNodes));
86
87 5
    $attributes = array_map(function (AttributeInterface $attribute) use ($directed) {
88 5
      return $attribute->build($directed);
89 5
    }, $this->fAttributes);
90 5
    $ret .= $this->buildAttributes($attributes, NULL, NULL);
91 5
    return $ret;
92
  }
93
94 1
  public static function getAllowedChildTypes() {
95 1
    return array();
96
  }
97
98 7
  public function getType() {
99 7
    $ret = 'multiedge';
100 7
    return $ret;
101
  }
102
103
}
104