Link.php 1.39 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php

namespace Drupal\d3_sankey\Model;

/**
 * An object representing a single, raw Sankey link.
 */
class Link {

  /**
   * The array index of the node to connect from.
   *
   * @var int
   */
  public $source;

  /**
   * The array index of the node to connect to.
   *
   * @var int
   */
  public $target;

  /**
   * A number representing how big the link is.
   *
   * @var numeric
   */
  public $value;

  /**
   * D3SankeyLink constructor.
   *
   * @param int $source
   *   The array index of the node to connect from.
   * @param int $target
   *   The array index of the node to connect to.
   * @param int|float $value
   *   A number representing how big the link is.
   */
  public function __construct($source, $target, $value = 1) {
    $this->source = (int) $source;
    $this->target = (int) $target;
    $this->value = is_numeric($value) ? $value + 0 : 1;
  }

  /**
   * Get an associated-array version of this link.
   *
   * @return array
   *   An associative array containing:
   *   - source: The array index of the node to connect from.
   *   - target: The array index of the node to connect to.
   *   - value (optional): A number representing how big the link is.
   */
  public function toAssocArray() {
    $answer = array('source' => $this->source, 'target' => $this->target);

    if (!is_null($this->value)) {
      $answer['value'] = $this->value;
    }

    return $answer;
  }

}