Completed
Pull Request — master (#3)
by Hannes
02:16
created

LambdaInterpolation   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 56
ccs 0
cts 17
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A isValidClosure() 0 6 1
A interpolate() 0 6 1
1
<?php
2
3
/*
4
 * This file is part of the Runalyze DEM Reader.
5
 *
6
 * (c) RUNALYZE <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Runalyze\DEM\Interpolation;
13
14
use Runalyze\DEM\Exception\InvalidArgumentException;
15
16
class LambdaInterpolation implements InterpolationInterface
17
{
18
    use ArgumentsCheckTrait;
19
20
    /** @var \Closure */
21
    protected $Lambda;
22
23
    /**
24
     * LambdaInterpolation constructor.
25
     * @param  \Closure                 $interpolationFunction
26
     * @throws InvalidArgumentException
27
     */
28
    public function __construct(\Closure $interpolationFunction)
29
    {
30
        if (!$this->isValidClosure($interpolationFunction)) {
31
            throw new InvalidArgumentException('Provided closure does not fulfill the requirements.');
32
        }
33
34
        $this->Lambda = $interpolationFunction;
35
    }
36
37
    /**
38
     * @param  \Closure $function
39
     * @return bool
40
     */
41
    protected function isValidClosure(\Closure $function)
42
    {
43
        $Reflection = new \ReflectionFunction($function);
44
45
        return 3 === $Reflection->getNumberOfRequiredParameters();
46
    }
47
48
    /**
49
     * Interpolate.
50
     *
51
     * p0------------p1
52
     * |      |
53
     * |      y
54
     * |      |
55
     * |--x-- Z
56
     * |
57
     * p2------------p3
58
     *
59
     * @param  float                    $x                      x position of Z within bounding box, required: $x in [0.0, 1.0]
60
     * @param  float                    $y                      y position of Z within bounding box, required: $y in [0.0, 1.0]
61
     * @param  array                    $elevationOnBoundingBox elevation data on [p0, p1, p2, p3]
62
     * @return int                      estimated elevation on point Z
63
     * @throws InvalidArgumentException
64
     */
65
    public function interpolate($x, $y, array $elevationOnBoundingBox)
66
    {
67
        $this->checkArguments($x, $y, $elevationOnBoundingBox);
68
69
        return $this->Lambda->__invoke($x, $y, $elevationOnBoundingBox);
70
    }
71
}
72