Passed
Push — master ( 1df370...dc6623 )
by Andrii
01:30
created

PointsList::valid()   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
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace KDTree\Structure;
6
7
use KDTree\{Exceptions\InvalidDimensionsCount,
8
    Exceptions\InvalidPointProvided,
9
    Exceptions\PointNotFound,
10
    Interfaces\PointInterface,
11
    Interfaces\PointsListInterface};
12
13
class PointsList implements PointsListInterface
14
{
15
    /**
16
     * @var int
17
     */
18
    private $dimensions;
19
20
    /**
21
     * @var array
22
     */
23
    private $container = [];
24
25
    /**
26
     * @param int $dimensions
27
     *
28
     * @throws InvalidDimensionsCount
29
     */
30
    public function __construct(int $dimensions)
31
    {
32
        if ($dimensions < 1) {
33
            throw new InvalidDimensionsCount();
34
        }
35
        $this->dimensions = $dimensions;
36
    }
37
38
    /**
39
     * @inheritDoc
40
     */
41
    public function next(): void
42
    {
43
        next($this->container);
44
    }
45
46
    /**
47
     * @inheritDoc
48
     */
49
    public function key(): int
50
    {
51
        return key($this->container);
0 ignored issues
show
Bug Best Practice introduced by
The expression return key($this->container) could return the type null|string which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
52
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57
    public function valid(): bool
58
    {
59
        return null !== $this->current();
60
    }
61
62
    /**
63
     * @inheritDoc
64
     */
65
    public function current(): ?PointInterface
66
    {
67
        return current($this->container) instanceof PointInterface ? current($this->container) : null;
68
    }
69
70
    /**
71
     * @inheritDoc
72
     */
73
    public function rewind(): void
74
    {
75
        reset($this->container);
76
    }
77
78
    /**
79
     * @inheritDoc
80
     */
81
    public function count(): int
82
    {
83
        return count($this->container);
84
    }
85
86
    /**
87
     * @return int
88
     */
89
    public function getDimensions(): int
90
    {
91
        return $this->dimensions;
92
    }
93
94
    /**
95
     * @param PointInterface $point
96
     *
97
     * @return PointsListInterface
98
     * @throws InvalidPointProvided
99
     */
100
    public function addPoint(PointInterface $point): PointsListInterface
101
    {
102
        if ($point->getDimensions() !== $this->dimensions) {
103
            throw new InvalidPointProvided();
104
        }
105
        $this->container[$this->prepareKey($point)] = $point;
106
107
        return $this;
108
    }
109
110
    /**
111
     * @param PointInterface $point
112
     *
113
     * @return PointsListInterface
114
     * @throws PointNotFound
115
     */
116
    public function removePoint(PointInterface $point): PointsListInterface
117
    {
118
        if (!$this->pointExists($point)) {
119
            throw new PointNotFound();
120
        }
121
        unset($this->container[$this->prepareKey($point)]);
122
123
        return $this;
124
    }
125
126
    /**
127
     * @param PointInterface $point
128
     *
129
     * @return bool
130
     */
131
    public function pointExists(PointInterface $point): bool
132
    {
133
        return isset($this->container[$this->prepareKey($point)]);
134
    }
135
136
    /**
137
     * @param PointInterface $point
138
     *
139
     * @return string
140
     */
141
    private function prepareKey(PointInterface $point): string
142
    {
143
        return implode('_', $point->getAxises());
144
    }
145
}
146