GraphLink::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
namespace Smoren\Containers\Structs;
4
5
class GraphLink
6
{
7
    /**
8
     * @var GraphItem left item
9
     */
10
    protected GraphItem $leftItem;
11
    /**
12
     * @var GraphItem right item
13
     */
14
    protected GraphItem $rightItem;
15
    /**
16
     * @var string link type
17
     */
18
    protected string $type;
19
20
    /**
21
     * GraphItemLink constructor.
22
     * @param GraphItem $leftItem left item
23
     * @param GraphItem $rightItem right item
24
     * @param string $type link type
25
     */
26
    public function __construct(GraphItem $leftItem, GraphItem $rightItem, string $type)
27
    {
28
        $this->leftItem = $leftItem;
29
        $this->rightItem = $rightItem;
30
        $this->type = $type;
31
    }
32
33
    /**
34
     * Swaps left and right items
35
     * @param bool $clone need clone items
36
     * @return $this
37
     */
38
    public function swap(bool $clone = false): self
39
    {
40
        $lhs = $clone ? clone $this->leftItem : $this->leftItem;
41
        $rhs = $clone ? clone $this->rightItem : $this->rightItem;
42
43
        $this->leftItem = $rhs;
44
        $this->rightItem = $lhs;
45
46
        return $this;
47
    }
48
49
    /**
50
     * @return GraphItem
51
     */
52
    public function getLeftItem(): GraphItem
53
    {
54
        return $this->leftItem;
55
    }
56
57
    /**
58
     * @return GraphItem
59
     */
60
    public function getRightItem(): GraphItem
61
    {
62
        return $this->rightItem;
63
    }
64
65
    /**
66
     * @return string
67
     */
68
    public function getType(): string
69
    {
70
        return $this->type;
71
    }
72
73
    /**
74
     * Clones items
75
     * @return $this
76
     */
77
    public function cloneItems(): self
78
    {
79
        $this->leftItem = clone $this->leftItem;
80
        $this->rightItem = clone $this->rightItem;
81
82
        return $this;
83
    }
84
85
    /**
86
     * @return string
87
     */
88
    public function getId(): string
89
    {
90
        return "{$this->leftItem->getId()}_{$this->rightItem->getId()}";
91
    }
92
93
    /**
94
     * Representates as array
95
     * @param bool $itemIdsOnly
96
     * @return array
97
     */
98
    public function toArray(bool $itemIdsOnly = false): array
99
    {
100
        if($itemIdsOnly) {
101
            return [$this->leftItem->getId(), $this->rightItem->getId(), $this->type];
102
        }
103
104
        return [$this->leftItem->toArray(), $this->rightItem->toArray(), $this->type];
105
    }
106
}
107