1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Jnjxp Rinc |
4
|
|
|
* |
5
|
|
|
* PHP version 5 |
6
|
|
|
* |
7
|
|
|
* Copyright (C) 2016 Jake Johns |
8
|
|
|
* |
9
|
|
|
* This software may be modified and distributed under the terms |
10
|
|
|
* of the MIT license. See the LICENSE file for details. |
11
|
|
|
* |
12
|
|
|
* @category Increment |
13
|
|
|
* @package Jnjxp\Rinc |
14
|
|
|
* @author Jake Johns <[email protected]> |
15
|
|
|
* @copyright 2016 Jake Johns |
16
|
|
|
* @license http://jnj.mit-license.org/2016 MIT License |
17
|
|
|
* @link https://github.com/jnjxp/jnjxp.rinc |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Jnjxp\Rinc; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Increment |
24
|
|
|
* |
25
|
|
|
* @category Increment |
26
|
|
|
* @package Jnjxp\Rinc |
27
|
|
|
* @author Jake Johns <[email protected]> |
28
|
|
|
* @license http://jnj.mit-license.org/ MIT License |
29
|
|
|
* @link https://github.com/jnjxp/jnjxp.rinc |
30
|
|
|
*/ |
31
|
|
|
class Increment |
32
|
|
|
{ |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Increment |
36
|
|
|
* |
37
|
|
|
* @var float |
38
|
|
|
* |
39
|
|
|
* @access protected |
40
|
|
|
*/ |
41
|
|
|
protected $increment; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Rounder |
45
|
|
|
* |
46
|
|
|
* @var RoundTo |
47
|
|
|
* |
48
|
|
|
* @access protected |
49
|
|
|
*/ |
50
|
|
|
protected $rounder; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* __construct |
54
|
|
|
* |
55
|
|
|
* @param float $increment round to value |
56
|
|
|
* |
57
|
|
|
* @access public |
58
|
|
|
*/ |
59
|
1 |
|
public function __construct($increment) |
60
|
|
|
{ |
61
|
1 |
|
$this->increment = $increment; |
62
|
1 |
|
$this->rounder = new RoundTo; |
63
|
1 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Named constructor for rounding to eighth |
67
|
|
|
* |
68
|
|
|
* @return Increment |
69
|
|
|
* |
70
|
|
|
* @access public |
71
|
|
|
* @static |
72
|
|
|
*/ |
73
|
1 |
|
public static function eighth() |
74
|
|
|
{ |
75
|
1 |
|
return new self(1/8); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Named constructor for rounding to quarter |
80
|
|
|
* |
81
|
|
|
* @return Increment |
82
|
|
|
* |
83
|
|
|
* @access public |
84
|
|
|
* @static |
85
|
|
|
*/ |
86
|
1 |
|
public static function quarter() |
87
|
|
|
{ |
88
|
1 |
|
return new self(1/4); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Named constructor for rounding to half |
93
|
|
|
* |
94
|
|
|
* @return Increment |
95
|
|
|
* |
96
|
|
|
* @access public |
97
|
|
|
* @static |
98
|
|
|
*/ |
99
|
1 |
|
public static function half() |
100
|
|
|
{ |
101
|
1 |
|
return new self(1/2); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Round value to nearest increment |
106
|
|
|
* |
107
|
|
|
* @param float $value value to round |
108
|
|
|
* |
109
|
|
|
* @return float |
110
|
|
|
* |
111
|
|
|
* @access public |
112
|
|
|
*/ |
113
|
1 |
|
public function nearest($value) |
114
|
|
|
{ |
115
|
1 |
|
return $this->rounder->nearest($this->increment, $value); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Round value to next increment |
120
|
|
|
* |
121
|
|
|
* @param float $value value to round |
122
|
|
|
* |
123
|
|
|
* @return float |
124
|
|
|
* |
125
|
|
|
* @access public |
126
|
|
|
*/ |
127
|
1 |
|
public function next($value) |
128
|
|
|
{ |
129
|
1 |
|
return $this->rounder->next($this->increment, $value); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Round value to previous increment |
134
|
|
|
* |
135
|
|
|
* @param float $value value to round |
136
|
|
|
* |
137
|
|
|
* @return float |
138
|
|
|
* |
139
|
|
|
* @access public |
140
|
|
|
*/ |
141
|
1 |
|
public function previous($value) |
142
|
|
|
{ |
143
|
1 |
|
return $this->rounder->previous($this->increment, $value); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Proxy to self::nearest |
148
|
|
|
* |
149
|
|
|
* @param float $value value to round |
150
|
|
|
* |
151
|
|
|
* @return float |
152
|
|
|
* |
153
|
|
|
* @access public |
154
|
|
|
*/ |
155
|
1 |
|
public function __invoke($value) |
156
|
|
|
{ |
157
|
1 |
|
return $this->nearest($value); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
|