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

Cluster::belongsTo()   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
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Kmeans;
4
5
use Kmeans\Interfaces\ClusterInterface;
6
use Kmeans\Interfaces\PointCollectionInterface;
7
use Kmeans\Interfaces\PointInterface;
8
use Kmeans\Interfaces\SpaceInterface;
9
10
class Cluster implements ClusterInterface
11
{
12
    private PointInterface $centroid;
13
    private PointCollectionInterface $points;
14
15
    public function __construct(PointInterface $centroid, PointCollectionInterface $points = null)
16
    {
17
        $this->points = $points ?? new PointCollection($centroid->getSpace());
18
        $this->setCentroid($centroid);
19
    }
20
21
    public function getSpace(): SpaceInterface
22
    {
23
        return $this->points->getSpace();
24
    }
25
26
    public function belongsTo(SpaceInterface $space): bool
27
    {
28
        return $this->getSpace()->isEqualTo($space);
29
    }
30
31
    public function getCentroid(): PointInterface
32
    {
33
        return $this->centroid;
34
    }
35
36
    public function setCentroid(PointInterface $point): void
37
    {
38
        if (! $point->belongsTo($this->getSpace())) {
39
            throw new \LogicException("Cannot set centroid: invalid point space");
40
        }
41
42
        $this->centroid = $point;
43
    }
44
45
    public function getPoints(): PointCollectionInterface
46
    {
47
        return $this->points;
48
    }
49
50
    public function attach(PointInterface $point): void
51
    {
52
        $this->points->attach($point);
53
    }
54
55
    public function detach(PointInterface $point): void
56
    {
57
        $this->points->detach($point);
58
    }
59
}
60