Passed
Push — master ( 4e36c0...26f6ea )
by David
01:34
created

Place::isWalkable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GGGGino\WarehousePath\Entity;
4
5
class Place
6
{
7
    /**
8
     * An identifier to recognize the node
9
     *
10
     * @var string
11
     */
12
    protected $name;
13
14
    /**
15
     * Left neighbor
16
     *
17
     * @var Place
18
     */
19
    protected $leftRef = null;
20
21
    /**
22
     * Right neighbor
23
     *
24
     * @var Place
25
     */
26
    protected $rightRef = null;
27
28
    /**
29
     * Top neighbor
30
     *
31
     * @var Place
32
     */
33
    protected $topRef = null;
34
35
    /**
36
     * Bottom neighbor
37
     *
38
     * @var Place
39
     */
40
    protected $bottomRef = null;
41
42
    /**
43
     * Property used when I build the matrix.
44
     * It is useful because it gives me the breadcrumb of the path
45
     *
46
     * @var Place
47
     */
48
    protected $walkingCameFrom = null;
49
50
    /**
51
     * Show if a Place is already visited
52
     *
53
     * @var bool
54
     */
55
    protected $visited = false;
56
57
    /**
58
     * Ideally this property is used to calculate the distance from a given point.
59
     *
60
     * @var int
61
     */
62
    protected $currentWeight = 0;
63
64
    /**
65
     * @var PlaceType
66
     */
67
    protected $placeType;
68
69
    /**
70
     * Place constructor.
71
     * @param $placeType
72
     * @param string $name
73
     */
74
    public function __construct($placeType, $name = "")
75
    {
76
        $this->name = $name;
77
        $this->placeType = $placeType;
78
        $this->currentWeight = $this->placeType->getOriginalWeight();
79
    }
80
81
    public function __toString()
82
    {
83
        return $this->getName() . "-" . $this->getOriginalWeight();
84
    }
85
86
    /**
87
     * Describe if the place will be walkable or not
88
     *
89
     * @return boolean
90
     */
91
    public function isWalkable()
92
    {
93
        return $this->placeType->isWalkable();
94
    }
95
96
    /**
97
     * @param Place $leftRef
98
     * @return Place
99
     */
100
    public function setLeftRef(Place &$leftRef): Place
101
    {
102
        if( $this->leftRef )
103
            return $this;
104
105
        $this->leftRef = $leftRef;
106
        $leftRef->setRightRef($this);
107
108
        return $this;
109
    }
110
111
    /**
112
     * @param Place $rightRef
113
     * @return Place
114
     */
115
    public function setRightRef(Place &$rightRef): Place
116
    {
117
        if( $this->rightRef )
118
            return $this;
119
120
        $this->rightRef = $rightRef;
121
        $rightRef->setLeftRef($this);
122
123
        return $this;
124
    }
125
126
    /**
127
     * @param Place $topRef
128
     * @return Place
129
     */
130
    public function setTopRef(Place &$topRef): Place
131
    {
132
        if( $this->topRef )
133
            return $this;
134
135
        $this->topRef = $topRef;
136
        $topRef->setBottomRef($this);
137
138
        return $this;
139
    }
140
141
    /**
142
     * @param Place $bottomRef
143
     * @return Place
144
     */
145
    public function setBottomRef(Place &$bottomRef): Place
146
    {
147
        if( $this->bottomRef )
148
            return $this;
149
150
        $this->bottomRef = $bottomRef;
151
        $bottomRef->setTopRef($this);
152
        return $this;
153
    }
154
155
    /**
156
     * When walk the tree, when it arrives in this node, increase the value
157
     *
158
     * @param int $i
159
     */
160
    public function increaseCurrentWeight(int $i)
161
    {
162
        $this->currentWeight += $i;
163
    }
164
165
    /**
166
     * Set the currentWeight at the originalWeight
167
     */
168
    public function resetCurrentWeight()
169
    {
170
        $this->currentWeight = $this->getOriginalWeight();
171
    }
172
173
    /**
174
     * @return int
175
     */
176
    public function getCurrentWeight()
177
    {
178
        return $this->currentWeight;
179
    }
180
181
    /**
182
     * @param int $currentWeight
183
     * @return Place
184
     */
185
    public function setCurrentWeight($currentWeight)
186
    {
187
        $this->currentWeight = $currentWeight;
188
        return $this;
189
    }
190
191
    /**
192
     * @return Place[]
193
     */
194
    public function getNeighbors()
195
    {
196
        return [
197
            $this->topRef,
198
            $this->rightRef,
199
            $this->bottomRef,
200
            $this->leftRef
201
        ];
202
    }
203
204
    /**
205
     * @return Place[]
206
     */
207
    public function getWalkableNeighbors()
208
    {
209
        return array_filter($this->getNeighbors(), function($place) {
210
            if( !$place )
211
                return false;
212
213
            return $place->isWalkable();
214
        });
215
    }
216
217
    /**
218
     * @param bool $visited
219
     * @return Place
220
     */
221
    public function setVisited($visited)
222
    {
223
        $this->visited = $visited;
224
        return $this;
225
    }
226
227
    /**
228
     * @return bool
229
     */
230
    public function isVisited(): bool
231
    {
232
        return $this->visited;
233
    }
234
235
    /**
236
     * @return string
237
     */
238
    public function getName()
239
    {
240
        return $this->name;
241
    }
242
243
    /**
244
     * @param string $name
245
     * @return Place
246
     */
247
    public function setName($name)
248
    {
249
        $this->name = $name;
250
        return $this;
251
    }
252
253
    /**
254
     * @return Place
255
     */
256
    public function getWalkingCameFrom()
257
    {
258
        return $this->walkingCameFrom;
259
    }
260
261
    /**
262
     * @param Place $walkingCameFrom
263
     * @return Place
264
     */
265
    public function setWalkingCameFrom(Place &$walkingCameFrom)
266
    {
267
        $this->walkingCameFrom = $walkingCameFrom;
268
        return $this;
269
    }
270
271
    /**
272
     * @return int
273
     */
274
    public function getOriginalWeight()
275
    {
276
        return $this->placeType->getOriginalWeight();
277
    }
278
279
    /**
280
     * @return Place
281
     */
282
    public function reset()
283
    {
284
        $this->currentWeight = $this->getOriginalWeight();
285
        $this->walkingCameFrom = null;
286
        $this->visited = false;
287
288
        return $this;
289
    }
290
291
    /**
292
     * @return Place
293
     */
294
    public function getLeftRef()
295
    {
296
        return $this->leftRef;
297
    }
298
299
    /**
300
     * @return Place
301
     */
302
    public function getRightRef()
303
    {
304
        return $this->rightRef;
305
    }
306
307
    /**
308
     * @return Place
309
     */
310
    public function getTopRef()
311
    {
312
        return $this->topRef;
313
    }
314
315
    /**
316
     * @return Place
317
     */
318
    public function getBottomRef()
319
    {
320
        return $this->bottomRef;
321
    }
322
}