Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
Range
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
3 / 3
6
100.00% covered (success)
100.00%
1 / 1
 setRange
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
3
 getRange
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMin
n/a
0 / 0
n/a
0 / 0
0
 getMax
n/a
0 / 0
n/a
0 / 0
0
 isInRange
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 * @author: Doug Wilbourne (dougwilbourne@gmail.com)
5 */
6
7declare(strict_types=1);
8
9namespace pvc\struct\range;
10
11use pvc\interfaces\struct\range\RangeInterface;
12
13/**
14 * @class Range
15 *
16 * The class provides methods for creating a range, getting a range as an array, checking if a payload is in the range.
17 *
18 * @template RangeElementDataType
19 * @implements RangeInterface<RangeElementDataType>
20 */
21abstract class Range implements RangeInterface
22{
23    /**
24     * @var RangeElementDataType|null
25     */
26    protected $min;
27
28    /**
29     * @var RangeElementDataType|null
30     */
31    protected $max;
32
33    /**
34     * @param  RangeElementDataType  $min
35     * @param  RangeElementDataType  $max
36     */
37    public function setRange($min, $max): void
38    {
39        $this->min = ($min < $max) ? $min : $max;
40        $this->max = ($min < $max) ? $max : $min;
41    }
42
43    /**
44     * getRange
45     *
46     * @return array<RangeElementDataType>
47     */
48    public function getRange(): array
49    {
50        return [$this->getMin(), $this->getMax()];
51    }
52
53    /**
54     * getMin
55     *
56     * @return RangeElementDataType
57     */
58    abstract protected function getMin(): mixed;
59
60    /**
61     * getMax
62     *
63     * @return RangeElementDataType
64     */
65    abstract protected function getMax(): mixed;
66
67    /**
68     * isInRange
69     *
70     * @param  RangeElementDataType  $x
71     *
72     * @return bool
73     */
74    public function isInRange($x): bool
75    {
76        return (($this->getMin() <= $x) && ($x <= $this->getMax()));
77    }
78}