Completed
Push — master ( 84d6ef...f4a207 )
by Ventaquil
02:51
created

Creator::randNewID()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 1
nop 0
1
<?php
2
namespace Algorithms\GraphTools;
3
4
class Creator
5
{
6
    protected $points = array(),
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
7
              $labels = array();
8
9
    public function __construct($points = null)
10
    {
11
        if (is_array($points)) {
12
            foreach ($points as $point) {
13
                $this->add($point);
14
            }
15
        } elseif(!is_null($points)) {
16
            throw new CreatorException('Creator contructor only accept points array');
17
        }
18
    }
19
20
    protected function pointNotExists($point)
21
    {
22
        Point::checkOrFail($point);
23
24
        foreach ($this->points as $p) {
25
            if ($p == $point) {
26
                return false;
27
            }
28
        }
29
30
        return true;
31
    }
32
33
    protected function addPoint($point)
34
    {
35
        if (!$this->pointNotExists($point)) {
36
            throw new CreatorException('Point is already stored in Creator object');
37
        }
38
39
        $this->points[] = $point;
40
    }
41
42
    public function add($point)
43
    {
44
        if (filter_var($point, FILTER_VALIDATE_INT) && is_numeric($point)) {
45
            $point = new Point($point);
46
        } elseif (!Point::check($point)) {
47
            throw new CreatorException('You can only add points to Creator object');
48
        }
49
50
        $this->addPoint($point);
51
52
        return $point;
53
    }
54
55
    public function get($id)
56
    {
57
        foreach ($this->points as $point)
58
        {
59
            if ($point->id == $id) {
60
                return $point;
61
            }
62
        }
63
64
        return null;
65
    }
66
67
    public function getOrFail($id)
68
    {
69
        $point = $this->get($id);
70
71
        if (!is_null($point)) {
72
            throw new CreatorException("Point with ID {$id} not found");
73
        }
74
75
        return $point;
76
    }
77
}
78