Passed
Pull Request — master (#34)
by Benjamin
12:13
created

PointCollection   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 71
rs 10
c 0
b 0
f 0
wmc 12

10 Methods

Rating   Name   Duplication   Size   Complexity  
A attach() 0 9 2
A valid() 0 3 1
A count() 0 3 1
A __construct() 0 7 2
A current() 0 3 1
A detach() 0 3 1
A rewind() 0 3 1
A contains() 0 3 1
A next() 0 3 1
A key() 0 3 1
1
<?php
2
3
namespace Kmeans;
4
5
use Kmeans\Concerns\HasSpaceTrait;
6
use Kmeans\Interfaces\PointCollectionInterface;
7
use Kmeans\Interfaces\PointInterface;
8
use Kmeans\Interfaces\SpaceInterface;
9
10
class PointCollection implements PointCollectionInterface
11
{
12
    use HasSpaceTrait;
13
14
    /**
15
     * @var \SplObjectStorage<PointInterface, null>
16
     */
17
    protected \SplObjectStorage $points;
18
19
    /**
20
     * @param array<PointInterface> $points
21
     */
22
    public function __construct(SpaceInterface $space, array $points = [])
23
    {
24
        $this->setSpace($space);
25
        $this->points = new \SplObjectStorage();
26
27
        foreach ($points as $point) {
28
            $this->attach($point);
29
        }
30
    }
31
32
    public function contains(PointInterface $point): bool
33
    {
34
        return $this->points->contains($point);
35
    }
36
37
    public function attach(PointInterface $point): void
38
    {
39
        if (! $point->belongsTo($this->getSpace())) {
40
            throw new \InvalidArgumentException(
41
                "Cannot add point to collection: point doesn't belong to the same space as collection"
42
            );
43
        }
44
45
        $this->points->attach($point);
46
    }
47
48
    public function detach(PointInterface $point): void
49
    {
50
        $this->points->detach($point);
51
    }
52
53
    public function current(): PointInterface
54
    {
55
        return $this->points->current();
56
    }
57
58
    public function key(): int
59
    {
60
        return $this->points->key();
61
    }
62
63
    public function next(): void
64
    {
65
        $this->points->next();
66
    }
67
68
    public function rewind(): void
69
    {
70
        $this->points->rewind();
71
    }
72
73
    public function valid(): bool
74
    {
75
        return $this->points->valid();
76
    }
77
78
    public function count(): int
79
    {
80
        return count($this->points);
81
    }
82
}
83