Completed
Push — master ( aba289...58e266 )
by Ventaquil
02:10
created

Connection::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
1
<?php
2
3
namespace PHPAlgorithms\GraphTools\Graph;
4
5
use JsonSerializable;
6
use PHPAlgorithms\GraphTools\Abstracts\BuildableAbstract;
7
8
/**
9
 * Class Connection
10
 * @package PHPAlgorithms\GraphTools\Graph
11
 * @method Node getFrom()
12
 * @method Node getTo()
13
 * @method float|null getWeight()
14
 * @property-read Node $from
15
 * @property-read Node $to
16
 * @property-read float|null $weight
17
 */
18
class Connection extends BuildableAbstract implements JsonSerializable
19
{
20
    /**
21
     * @var string[] $allowedGet
22
     */
23
    protected $allowedGet = array('from', 'to', 'weight');
24
25
    /**
26
     * @var Node $from
27
     */
28
    protected $from;
29
30
    /**
31
     * @var Node $to
32
     */
33
    protected $to;
34
35
    /**
36
     * @var float|null $weight
37
     */
38
    protected $weight;
39
40
    /**
41
     * Connection constructor.
42
     * @param Node $from
43
     * @param Node $to
44
     * @param float|null $weight
45
     */
46
    public function __construct(Node $from, Node $to, float $weight = null)
47
    {
48
        $this->from = $from;
49
50
        $this->to = $to;
51
52
        $this->weight = $weight;
53
    }
54
55
    /**
56
     * @return array
57
     */
58
    public function jsonSerialize() : array
59
    {
60
        $serialize = array();
61
62
        foreach ($this->allowedGet as $variable) {
63
            /**
64
             * @var string $variable
65
             */
66
67
            if (!is_null($variable)) {
68
                $serialize[$variable] = $this->{$variable};
69
            }
70
        }
71
72
        return $serialize;
73
    }
74
}