Creator::searchPoint()   A
last analyzed

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
/**
4
 * @author ventaquil <[email protected]>
5
 * @licence MIT
6
 */
7
8
namespace PHPAlgorithms\Dijkstra;
9
10
use PHPAlgorithms\Dijkstra\Exceptions\CreatorException;
11
12
/**
13
 * Class Creator
14
 * @package PHPAlgorithms\Dijkstra
15
 */
16
class Creator {
17
    /**
18
     * @var Point[] $points Array of collected Point objects.
19
     */
20
    private $points = array();
21
22
    /**
23
     * Method adds Point to current object. Can set point's label.
24
     *
25
     * @param string|null $label Point's label.
26
     * @return Point Reference to created Point object.
27
     */
28
    public function addPoint($label = null)
29
    {
30
        $point = new Point($label);
31
32
        $this->points[] = $point;
33
34
        return $point;
35
    }
36
37
    /**
38
     * Method generates integer ids to collected points and drop them out.
39
     *
40
     * @return Point[] Array of Point objects.
41
     */
42
    public function dumpPoints()
43
    {
44
        foreach ($this->points as $index => $point) {
45
            $point->id = $index;
46
        }
47
48
        return $this->points;
49
    }
50
51
    /**
52
     * Method returns collected Point object by given $index value.
53
     *
54
     * @param integer $index Point's index.
55
     * @return Point|null Point object if element with $index value exists or null otherwise.
56
     */
57
    public function getPoint($index)
58
    {
59
        try {
60
            return $this->getPointOrFail($index);
61
        } catch (CreatorException $e) {
62
            return null;
63
        }
64
    }
65
66
    /**
67
     * Method search $points object parameter and try to find Point identifier.
68
     *
69
     * @param Point $point Point object which we want to find.
70
     * @return mixed Returns key of $point or false if key isn't exists.
71
     */
72
    public function getPointIndex(Point $point)
73
    {
74
        return array_search($point, $this->points);
75
    }
76
77
    /**
78
     * Method returns collected Point object by given $index value or throws exception if index does not exists.
79
     * 
80
     * @param integer $index Point's index.
81
     * @return Point Point object if element with $index value exists.
82
     * @throws CreatorException Method throws exception when point with $index doesn't exists.
83
     */
84
    public function getPointOrFail($index)
85
    {
86
        if (!isset($this->points[$index])) {
87
            throw new CreatorException("Point with index {$index} not exists");
88
        }
89
90
        return $this->points[$index];
91
    }
92
93
    /**
94
     * Method checks that sent Point object collected by current Creator object.
95
     * 
96
     * @param Point $point Point object which we want to check.
97
     * @return boolean True if $point isset in $points parameter or false otherwise.
98
     */
99
    public function searchPoint(Point $point)
100
    {
101
        return in_array($point, $this->points);
102
    }
103
}
104