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

LambdaInterpolation::isValidClosure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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