|
1
|
|
|
<?php |
|
2
|
|
|
namespace FilmTools\MRounder; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Callable: Returns a given number rounded to the desired multiple. |
|
6
|
|
|
* |
|
7
|
|
|
* PHP implementation by Nasser Hekmati on StackOverflow |
|
8
|
|
|
* @see https://stackoverflow.com/a/48643210/3143771 |
|
9
|
|
|
* @see https://support.office.com/en-us/article/mround-function-c299c3b0-15a5-426d-aa4b-d2d5b3baf427 |
|
10
|
|
|
*/ |
|
11
|
|
|
class MRounder |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Default rounding behaviour |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
const FLOOR="floor"; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Turn on CEILING functionality |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
const CEIL="ceil"; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Turn on FLOOR functionality |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
const ROUND="round"; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var numeric |
|
34
|
|
|
*/ |
|
35
|
|
|
public $multiple; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
public $mode; |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param numeric $multiple |
|
46
|
|
|
*/ |
|
47
|
120 |
|
public function __construct( $multiple = 1, $mode = null ) |
|
48
|
|
|
{ |
|
49
|
120 |
|
if (is_numeric($multiple)) |
|
50
|
114 |
|
$this->multiple = $multiple; |
|
|
|
|
|
|
51
|
|
|
else |
|
52
|
8 |
|
throw new MRoundInvalidArgumentException("Parameter 'multiple' must be numeric."); |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
112 |
|
if (in_array($mode, [ null, static::FLOOR, static::CEIL, static::ROUND ])) |
|
56
|
109 |
|
$this->mode = $mode ?: static::ROUND; |
|
57
|
|
|
else |
|
58
|
4 |
|
throw new MRoundInvalidArgumentException("Parameter 'mode' must be one of 'round', floor', or 'ceil'."); |
|
59
|
108 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Accepts a number or array with numbers. |
|
64
|
|
|
* @param numeric[] $number |
|
65
|
|
|
*/ |
|
66
|
108 |
|
public function __invoke( $number ) |
|
67
|
|
|
{ |
|
68
|
108 |
|
if (is_numeric($number)): |
|
69
|
104 |
|
if ($this->multiple == 0) |
|
70
|
35 |
|
return 0; |
|
71
|
|
|
|
|
72
|
92 |
|
switch ($this->mode): |
|
73
|
92 |
|
case static::ROUND: |
|
74
|
72 |
|
return mround( $number, $this->multiple ); |
|
75
|
|
|
break; |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
20 |
|
case static::FLOOR: |
|
78
|
8 |
|
return mfloor( $number, $this->multiple ); |
|
79
|
|
|
break; |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
12 |
|
case static::CEIL: |
|
82
|
8 |
|
return mceil( $number, $this->multiple ); |
|
83
|
|
|
break; |
|
|
|
|
|
|
84
|
|
|
|
|
85
|
1 |
|
default: |
|
86
|
4 |
|
throw new MRoundUnexpectedValueException("Unexpected round mode value"); |
|
87
|
|
|
|
|
88
|
1 |
|
endswitch; |
|
89
|
|
|
|
|
90
|
16 |
|
elseif (is_array( $number)): |
|
91
|
12 |
|
return array_map($this, $number); |
|
92
|
|
|
endif; |
|
93
|
|
|
|
|
94
|
4 |
|
throw new MRoundInvalidArgumentException("Number must be numeric or array of numbers."); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..