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

Point::__set()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 8.8571
cc 5
eloc 9
nc 4
nop 2
1
<?php
2
namespace Algorithms\GraphTools;
3
4
class Point
5
{
6
    protected $id,
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
              $x,
8
              $y,
9
              $label = null;
10
11
    public function __construct($id, $label = null)
12
    {
13
        if (!(filter_var($id, FILTER_VALIDATE_INT) || is_numeric($value))) {
0 ignored issues
show
Bug introduced by
The variable $value does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
14
            throw new PointException('ID is must be a number');
15
        }
16
17
        $this->id = $id;
18
        $this->label = $label;
19
    }
20
21
    public static function create($id, $label = null)
22
    {
23
        return new self($id, $label);
24
    }
25
26
    public static function check($point)
27
    {
28
        return ($point instanceof self);
29
    }
30
31
    public static function checkOrFail($point)
32
    {
33
        if (!Point::check($point)) {
34
            throw new PointException('Sent object is not a Point');
35
        }
36
37
        return true;
38
    }
39
40
    public function __get($name)
41
    {
42
        return $this->{$name};
43
    }
44
45
    public function __set($name, $value)
46
    {
47
        if (in_array($name, array('x', 'y'))) {
48
            if (!(filter_var($value, FILTER_VALIDATE_INT) || is_numeric($value))) {
49
                throw new PointException("Parameter {$value} must be a number");
50
            }
51
52
            $this->{$name} = $value;
53
        } elseif ($name == 'id') {
54
            throw new PointException('You can\'t change point ID');
55
        } else {
56
            $this->{$name} = $value;
57
        }
58
    }
59
60
    public function toArray()
61
    {
62
        $point = array(
63
            'id' => $this->id,
64
        );
65
66
        foreach (array('label', 'x', 'y') as $name) {
67
            if (!is_null($this->{$name})) {
68
                $point[$name] = $this->{$name};
69
            }
70
        }
71
72
        return $point;
73
    }
74
75
    public function addConnection(Point $point, $distance)
76
    {
77
        return new ConnectionsContainer(
78
            array(
79
                new Connection($this, $point, $distance),
80
            )
81
        );
82
    }
83
}
84