GraphTypeBuilder::allowSelfLoops()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Graphp\Graph\Builder;
4
5
use InvalidArgumentException;
6
use Graphp\Graph\DefaultGraphType;
7
use Graphp\Graph\GraphTypeInterface;
8
9
/**
10
 * class GraphTypeBuilder
11
 *
12
 * @package Graphp\Graph\Builder
13
 */
14
class GraphTypeBuilder
15
{
16
    private $directed;
17
    private $undirected;
18
    private $allowSelfLoops;
19
    private $allowMultipleEdges;
20
    private $weighted;
21
    private $allowCycles;
22
    private $modifiable;
23
24
    /**
25
     * Construct a new graph type
26
     *
27
     * @param GraphTypeInterface $type - the graph type
28
     * @param bool $directed - is graph directed
29
     * @param bool $undirected - is graph undirected
30
     */
31 42
    public function __construct(?GraphTypeInterface $type = null, ?bool $directed = null, ?bool $undirected = null)
32
    {
33 42
        if (!is_null($type)) {
34 1
            $this->directed = $type->isDirected() || $type->isMixed();
35 1
            $this->undirected = $type->isUndirected() || $type->isMixed();
36 1
            $this->allowSelfLoops = $type->isAllowingSelfLoops();
37 1
            $this->allowMultipleEdges = $type->isAllowingMultipleEdges();
38 1
            $this->weighted = $type->isWeighted();
39 1
            $this->allowCycles = $type->isAllowingCycles();
40 1
            $this->modifiable = $type->isModifiable();
41 42
        } elseif (!is_null($directed) && !is_null($undirected)) {
42 1
            if (!$directed && !$undirected) {
43
                throw new InvalidArgumentException("At least one of directed or undirected must be true");
44
            }
45 1
            $this->directed = $directed;
46 1
            $this->undirected = $undirected;
47 1
            $this->allowSelfLoops = true;
48 1
            $this->allowMultipleEdges = true;
49 1
            $this->weighted = false;
50 1
            $this->allowCycles = true;
51 1
            $this->modifiable = true;
52
        } else {
53 42
            $this->directed = false;
54 42
            $this->undirected = true;
55 42
            $this->allowSelfLoops = true;
56 42
            $this->allowMultipleEdges = true;
57 42
            $this->weighted = false;
58 42
            $this->allowCycles = true;
59 42
            $this->modifiable = true;
60
        }
61 42
    }
62
63
    /**
64
     * Set the type as directed.
65
     *
66
     * @return self
67
     */
68 32
    public function directed(): self
69
    {
70 32
        $this->directed = true;
71 32
        $this->undirected = false;
72 32
        return $this;
73
    }
74
75
    /**
76
     * Set the type as undirected.
77
     *
78
     * @return self
79
     */
80 11
    public function undirected(): self
81
    {
82 11
        $this->directed = false;
83 11
        $this->undirected = true;
84 11
        return $this;
85
    }
86
87
    /**
88
     * Set the type as mixed.
89
     *
90
     * @return self
91
     */
92 1
    public function mixed(): self
93
    {
94 1
        $this->directed = true;
95 1
        $this->undirected = true;
96 1
        return $this;
97
    }
98
99
    /**
100
     * Set whether to allow self-loops.
101
     *
102
     * @param bool $value - if to allow self loops
103
     *
104
     * @return self
105
     */
106 42
    public function allowSelfLoops(bool $value): self
107
    {
108 42
        $this->allowSelfLoops = $value;
109 42
        return $this;
110
    }
111
112
    /**
113
     * Set whether to allow multiple edges.
114
     *
115
     * @param bool $value - if to allow multiple edges
116
     *
117
     * @return self
118
     */
119 42
    public function allowMultipleEdges(bool $value): self
120
    {
121 42
        $this->allowMultipleEdges = $value;
122 42
        return $this;
123
    }
124
125
    /**
126
     * Set whether the graph is weighted.
127
     *
128
     * @param bool $value - if graph is weighted
129
     *
130
     * @return self
131
     */
132 42
    public function weighted(?bool $value = null): self
133
    {
134 42
        $this->weighted = $value ?? false;
135 42
        return $this;
136
    }
137
138
    /**
139
     * Set whether to allow cycles.
140
     *
141
     * @param bool $value - if to allow cycles
142
     *
143
     * @return self
144
     */
145 21
    public function allowCycles(bool $value): self
146
    {
147 21
        $this->allowCycles = $value;
148 21
        return $this;
149
    }
150
151
    /**
152
     * Set whether the graph is modifiable.
153
     *
154
     * @param bool $value - if graph is modifiable
155
     *
156
     * @return self
157
     */
158 1
    public function modifiable(bool $value): self
159
    {
160 1
        $this->modifiable = $value;
161 1
        return $this;
162
    }
163
164
    /**
165
     * Build the graph type
166
     *
167
     * @return GraphTypeInterface
168
     */
169 42
    public function build(): GraphTypeInterface
170
    {
171 42
        return new DefaultGraphType(
172 42
            $this->directed,
173 42
            $this->undirected,
174 42
            $this->allowSelfLoops,
175 42
            $this->allowMultipleEdges,
176 42
            $this->weighted,
177 42
            $this->allowCycles,
178 42
            $this->modifiable
179
        );
180
    }
181
}
182