Completed
Branch master (eb975e)
by Basarab
06:37 queued 04:53
created

Point   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 59
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getNthDimension() 0 7 3
B validateDValues() 0 12 5
A getDimensionsCount() 0 4 1
1
<?php
2
3
namespace Hexogen\KDTree;
4
5
use Hexogen\KDTree\Exception\ValidationException;
6
7
class Point implements PointInterface
8
{
9
    private $dValues;
10
    private $length;
11
12
    /**
13
     * Item constructor.
14
     * @param array $dValues
15
     */
16 36
    public function __construct(array $dValues)
17
    {
18 36
        $this->length = count($dValues);
19 36
        $this->validateDValues($dValues);
20
21 36
        $this->dValues = $dValues;
22 36
    }
23
24
    /**
25
     * get nth dimension value from vector
26
     *
27
     * @param int $d
28
     * @return float
29
     */
30 48
    public function getNthDimension(int $d): float
31
    {
32 48
        if ($d < 0 || $d >= $this->length) {
33 3
            throw new \OutOfRangeException('$d = ' . $d . '  should be between 0 and number of ' . $this->length);
34
        }
35 45
        return (float)$this->dValues[$d];
36
    }
37
38
39
    /**
40
     * validate multi dimension vector
41
     *
42
     * @param array $dValues
43
     * @throws ValidationException
44
     */
45 36
    private function validateDValues(array $dValues)
46
    {
47 36
        if ($this->length == 0) {
48 3
            throw new ValidationException('$dValues should be not empty');
49
        }
50
51 36
        for ($i = 0; $i < $this->length; $i++) {
52 36
            if (!isset($dValues[$i]) || !is_numeric($dValues[$i])) {
53 3
                throw new ValidationException('$dValues is not a simple array list');
54
            }
55
        }
56 36
    }
57
58
    /**
59
     * @return int number of dimensions in array
60
     */
61 48
    public function getDimensionsCount(): int
62
    {
63 48
        return $this->length;
64
    }
65
}
66