Completed
Push — master ( 195fbe...724c19 )
by Ventaquil
02:47
created

Creator::searchPoint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace PHPAlgorithms\Dijkstra;
4
5
use PHPAlgorithms\Dijkstra\Exceptions\CreatorException;
6
7
class Creator {
8
    private $points = array();
9
10
    public function addPoint($label = null)
11
    {
12
        $point = new Point($label);
13
14
        $this->points[] = $point;
15
16
        return $point;
17
    }
18
19
    public function dumpPoints()
20
    {
21
        foreach ($this->points as $index => $point) {
22
            $point->id = $index;
23
        }
24
25
        return $this->points;
26
    }
27
28
    public function getPoint($index)
29
    {
30
        try {
31
            return $this->getPointOrFail($index);
32
        } catch (CreatorException $e) {
33
            return null;
34
        }
35
    }
36
37
    public function getPointIndex(Point $point)
38
    {
39
        return array_search($point, $this->points);
40
    }
41
42
    public function getPointOrFail($index)
43
    {
44
        if (!isset($this->points[$index])) {
45
            throw new CreatorException("Point with index {$index} not exists");
46
        }
47
48
        return $this->points[$index];
49
    }
50
51
    public function searchPoint(Point $point)
52
    {
53
        return in_array($point, $this->points);
54
    }
55
}
56