Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
6 / 6 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| Edge | |
100.00% |
6 / 6 |
|
100.00% |
3 / 3 |
3 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| getNodes | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| compare | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\Stdlib\Graph |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace phpOMS\Stdlib\Graph; |
| 16 | |
| 17 | /** |
| 18 | * Edge class. |
| 19 | * |
| 20 | * @package phpOMS\Stdlib\Graph |
| 21 | * @license OMS License 2.0 |
| 22 | * @link https://jingga.app |
| 23 | * @since 1.0.0 |
| 24 | */ |
| 25 | class Edge |
| 26 | { |
| 27 | /** |
| 28 | * Node1. |
| 29 | * |
| 30 | * In case of directed edges this is the from node/starting node. |
| 31 | * |
| 32 | * @var Node |
| 33 | * @since 1.0.0 |
| 34 | */ |
| 35 | public Node $node1; |
| 36 | |
| 37 | /** |
| 38 | * Node2. |
| 39 | * |
| 40 | * In case of directed edges this is the to node/end node. |
| 41 | * |
| 42 | * @var Node |
| 43 | * @since 1.0.0 |
| 44 | */ |
| 45 | public Node $node2; |
| 46 | |
| 47 | /** |
| 48 | * Is graph/edge directed |
| 49 | * |
| 50 | * @var bool |
| 51 | * @since 1.0.0 |
| 52 | */ |
| 53 | public bool $isDirected = false; |
| 54 | |
| 55 | /** |
| 56 | * Edge weight |
| 57 | * |
| 58 | * @var float |
| 59 | * @since 1.0.0 |
| 60 | */ |
| 61 | public float $weight = 1.0; |
| 62 | |
| 63 | /** |
| 64 | * Constructor. |
| 65 | * |
| 66 | * @param Node $node1 Graph node (start node in case of directed edge) |
| 67 | * @param Node $node2 Graph node (end node in case of directed edge) |
| 68 | * @param float $weight weight/cost of the edge |
| 69 | * @param bool $isDirected Is directed edge |
| 70 | * |
| 71 | * @since 1.0.0 |
| 72 | */ |
| 73 | public function __construct(Node $node1, Node $node2, float $weight = 1.0, bool $isDirected = false) |
| 74 | { |
| 75 | $this->node1 = $node1; |
| 76 | $this->node2 = $node2; |
| 77 | $this->weight = $weight; |
| 78 | $this->isDirected = $isDirected; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Get nodes of the edge. |
| 83 | * |
| 84 | * @return Node[] |
| 85 | * |
| 86 | * @since 1.0.0 |
| 87 | */ |
| 88 | public function getNodes() : array |
| 89 | { |
| 90 | return [$this->node1, $this->node2]; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Compare edge weights |
| 95 | * |
| 96 | * @param Edge $e1 Edge 1 |
| 97 | * @param Edge $e2 Edge 2 |
| 98 | * |
| 99 | * @return int |
| 100 | * |
| 101 | * @since 1.0.0 |
| 102 | */ |
| 103 | public static function compare(self $e1, self $e2) : int |
| 104 | { |
| 105 | return $e1->weight <=> $e2->weight; |
| 106 | } |
| 107 | } |