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

Point::validateDValues()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 6
nc 4
nop 1
crap 5
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